Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- #define endl "\n"
- using namespace std;
- using ll = long long;
- using ld = long double;
- using pii = pair<int, int>;
- constexpr int N = 2e5 + 5;
- using or_tree = __gnu_pbds::tree<int,
- __gnu_pbds::null_type,
- less<int>,
- __gnu_pbds::rb_tree_tag,
- __gnu_pbds::tree_order_statistics_node_update>;
- struct SegTree {
- vector<or_tree> t;
- int get_ans(int i, int l, int r) {
- auto& tr = t[i];
- auto le = tr.order_of_key(l);
- auto ri = tr.order_of_key(r + 1);
- return ri - le;
- }
- void build(int l, int r, int i, vector<int>& v) {
- if (l + 1 == r) {
- t[i].insert(v[l]);
- return;
- }
- int mid = (l + r) >> 1;
- build(l, mid, (i << 1) + 1, v);
- build(mid, r, (i << 1) + 2, v);
- for (int j = l; j < r; j++)
- t[i].insert(v[j]);
- }
- SegTree(vector<int>& v) {
- t.resize(v.size() << 2);
- build(0, v.size(), 0, v);
- }
- void upd(int pos, int val, int del, int pos_del, int i, int l, int r) {
- if (l > pos_del || r <= pos_del) {
- t[i].erase(del);
- t[i].insert(val);
- }
- if (l + 1 == r)
- return;
- int mid = (l + r) >> 1;
- if (pos < mid)
- upd(pos, val, del, pos_del, (i << 1) + 1, l, mid);
- else
- upd(pos, val, del, pos_del, (i << 1) + 2, mid, r);
- }
- int get(int l, int r, int vl, int vr, int i, int cl, int cr) {
- if (l >= r)
- return 0;
- if (l == cl && r == cr)
- return get_ans(i, vl, vr);
- int mid = (cl + cr) >> 1;
- return
- get(l, min(mid, r), vl, vr, (i << 1) + 1, cl, mid) +
- get(max(l, mid), r, vl, vr, (i << 1) + 2, mid, cr);
- }
- };
- int n, m, type, la, ra, lb, rb;
- vector<int> a, b, reva, revb;
- void Solve() {
- cin >> n >> m;
- a.resize(n);
- b.resize(n);
- reva.resize(n + 1);
- revb.resize(n + 1);
- for (int i = 0; i < n; i++)
- cin >> a[i], reva[a[i]] = i;
- for (int i = 0; i < n; i++)
- cin >> b[i], revb[b[i]] = i;
- vector<int> vec;
- vec.resize(n);
- for (int i = 0; i < n; i++)
- vec[i] = revb[a[i]];
- SegTree tr(vec);
- while(m--) {
- cin >> type >> la >> ra;
- if (type == 1) {
- cin >> lb >> rb;
- cout << tr.get(la - 1, ra, lb - 1, rb - 1, 0, 0, n) << endl;
- }
- else {
- la--, ra--;
- tr.upd(reva[b[la]], revb[b[ra]], revb[b[la]], reva[b[ra]], 0, 0, n);
- tr.upd(reva[b[ra]], revb[b[la]], revb[b[ra]], reva[b[la]], 0, 0, n);
- swap(revb[b[la]], revb[b[ra]]);
- swap(b[la], b[ra]);
- }
- }
- }
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- // cin >> t;
- // while(t--)
- //auto start = chrono::high_resolution_clock::now();
- Solve();
- //auto end = chrono::high_resolution_clock::now();
- //cout << endl << (chrono::duration_cast<chrono::duration<double>>(end - start)).count();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement