Advertisement
limimage

HZ

Apr 23rd, 2020
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4.  
  5. #define endl "\n"
  6. using namespace std;
  7. using ll = long long;
  8. using ld = long double;
  9. using pii = pair<int, int>;
  10.  
  11. constexpr int N = 2e5 + 5;
  12. using or_tree = __gnu_pbds::tree<int,
  13. __gnu_pbds::null_type,
  14. less<int>,
  15. __gnu_pbds::rb_tree_tag,
  16. __gnu_pbds::tree_order_statistics_node_update>;
  17.  
  18.  
  19. struct SegTree {
  20. vector<or_tree> t;
  21.  
  22. int get_ans(int i, int l, int r) {
  23. auto& tr = t[i];
  24. auto le = tr.order_of_key(l);
  25. auto ri = tr.order_of_key(r + 1);
  26. return ri - le;
  27. }
  28.  
  29. void build(int l, int r, int i, vector<int>& v) {
  30. if (l + 1 == r) {
  31. t[i].insert(v[l]);
  32. return;
  33. }
  34. int mid = (l + r) >> 1;
  35. build(l, mid, (i << 1) + 1, v);
  36. build(mid, r, (i << 1) + 2, v);
  37. for (int j = l; j < r; j++)
  38. t[i].insert(v[j]);
  39. }
  40.  
  41. SegTree(vector<int>& v) {
  42. t.resize(v.size() << 2);
  43. build(0, v.size(), 0, v);
  44. }
  45.  
  46. void upd(int pos, int val, int del, int pos_del, int i, int l, int r) {
  47. if (l > pos_del || r <= pos_del) {
  48. t[i].erase(del);
  49. t[i].insert(val);
  50. }
  51. if (l + 1 == r)
  52. return;
  53. int mid = (l + r) >> 1;
  54. if (pos < mid)
  55. upd(pos, val, del, pos_del, (i << 1) + 1, l, mid);
  56. else
  57. upd(pos, val, del, pos_del, (i << 1) + 2, mid, r);
  58. }
  59.  
  60. int get(int l, int r, int vl, int vr, int i, int cl, int cr) {
  61. if (l >= r)
  62. return 0;
  63. if (l == cl && r == cr)
  64. return get_ans(i, vl, vr);
  65. int mid = (cl + cr) >> 1;
  66. return
  67. get(l, min(mid, r), vl, vr, (i << 1) + 1, cl, mid) +
  68. get(max(l, mid), r, vl, vr, (i << 1) + 2, mid, cr);
  69. }
  70. };
  71.  
  72. int n, m, type, la, ra, lb, rb;
  73. vector<int> a, b, reva, revb;
  74.  
  75. void Solve() {
  76. cin >> n >> m;
  77. a.resize(n);
  78. b.resize(n);
  79. reva.resize(n + 1);
  80. revb.resize(n + 1);
  81. for (int i = 0; i < n; i++)
  82. cin >> a[i], reva[a[i]] = i;
  83. for (int i = 0; i < n; i++)
  84. cin >> b[i], revb[b[i]] = i;
  85. vector<int> vec;
  86. vec.resize(n);
  87. for (int i = 0; i < n; i++)
  88. vec[i] = revb[a[i]];
  89. SegTree tr(vec);
  90. while(m--) {
  91. cin >> type >> la >> ra;
  92. if (type == 1) {
  93. cin >> lb >> rb;
  94. cout << tr.get(la - 1, ra, lb - 1, rb - 1, 0, 0, n) << endl;
  95. }
  96. else {
  97. la--, ra--;
  98. tr.upd(reva[b[la]], revb[b[ra]], revb[b[la]], reva[b[ra]], 0, 0, n);
  99. tr.upd(reva[b[ra]], revb[b[la]], revb[b[ra]], reva[b[la]], 0, 0, n);
  100. swap(revb[b[la]], revb[b[ra]]);
  101. swap(b[la], b[ra]);
  102. }
  103. }
  104. }
  105.  
  106. int main() {
  107. ios::sync_with_stdio(false);
  108. cin.tie(nullptr);
  109. cout.tie(nullptr);
  110. // cin >> t;
  111. // while(t--)
  112. //auto start = chrono::high_resolution_clock::now();
  113. Solve();
  114. //auto end = chrono::high_resolution_clock::now();
  115. //cout << endl << (chrono::duration_cast<chrono::duration<double>>(end - start)).count();
  116. return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement