Advertisement
arujbansal

Untitled

Apr 16th, 2021
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <map>
  5. #include <set>
  6. #include <array>
  7. #include <stack>
  8. #include <queue>
  9. #include <random>
  10. #include <numeric>
  11. #include <functional>
  12. #include <chrono>
  13. #include <utility>
  14. #include <iomanip>
  15. #include <assert.h>
  16.  
  17. using namespace std;
  18.  
  19. void dbg_out() { cerr << endl; }
  20. template<typename Head, typename... Tail>
  21. void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
  22. #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
  23.  
  24. #define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
  25. #define rng_seed(x) mt19937 rng(x)
  26. #define all(x) (x).begin(), (x).end()
  27. #define sz(x) (int) (x).size()
  28. // #define int long long
  29.  
  30. const int MXN = 1e5 + 5, INF = 1e9 + 5;
  31. int N, Q;
  32. int A[3 * MXN], tree_pos[3 * MXN], tree_cnt[3 * MXN][33];
  33. int l_free, r_free;
  34.  
  35. void modify_cnt(int pos, int val, int delta) {
  36.     int bits_new = __builtin_popcount(val);
  37.     int bits_cur = __builtin_popcount(A[pos]);
  38.  
  39.     for (int i = pos; i <= 3 * N; i += i & -i) {
  40.         tree_cnt[i][bits_new]++;
  41.         tree_cnt[i][bits_cur] -= (A[pos] > -1);
  42.         tree_pos[i] += delta;
  43.     }
  44. }
  45.  
  46. void shift(int idx) {
  47.     modify_cnt(idx, 0, -1);
  48.  
  49.     if (A[idx] & 1) {
  50.         modify_cnt(r_free++, A[idx], 1);
  51.         A[r_free - 1] = A[idx];
  52.     } else {
  53.         modify_cnt(l_free--, A[idx], 1);
  54.         A[l_free + 1] = A[idx];
  55.     }
  56.  
  57.     A[idx] = -1;
  58. }
  59.  
  60. int get_kth(int k) {
  61.     int pos = 0, jump = 1;
  62.  
  63.     while (jump * 2 <= 3 * N)
  64.         jump *= 2;
  65.  
  66.     while (jump) {
  67.         if (pos + jump <= 3 * N && tree_pos[pos + jump] < k) {
  68.             k -= tree_pos[pos + jump];
  69.             pos += jump;
  70.         }
  71.  
  72.         jump /= 2;
  73.     }
  74.  
  75.     return pos + 1;
  76. }
  77.  
  78. int query(int pos, int bits) {
  79.     int res = 0;
  80.  
  81.     for (int i = pos; i > 0; i -= i & -i)
  82.         res += tree_cnt[i][bits];
  83.  
  84.     return res;
  85. }
  86.  
  87. int query(int l, int r, int bits) {
  88.     return query(r, bits) - query(l - 1, bits);
  89. }
  90.  
  91. void solve() {
  92.     cin >> N >> Q;
  93.  
  94.     for (int i = 0; i <= 3 * N; i++) {
  95.         A[i] = -1;
  96.         tree_pos[i] = 0;
  97.  
  98.         for (int j = 0; j <= 32; j++)
  99.             tree_cnt[i][j] = 0;
  100.     }
  101.  
  102.     l_free = N, r_free = 2 * N + 1;
  103.  
  104.     for (int i = N + 1; i <= 2 * N; i++) {
  105.         int val;
  106.         cin >> val;
  107.  
  108.         modify_cnt(i, val, 1);
  109.         A[i] = val;
  110.     }
  111.  
  112.     while (Q--) {
  113.         int type;
  114.         cin >> type;
  115.  
  116.         if (type == 1) {
  117.             int X;
  118.             cin >> X;
  119.  
  120.             int pos = get_kth(X);
  121.             shift(pos);
  122.         } else if (type == 2) {
  123.             int X, P;
  124.             cin >> X >> P;
  125.  
  126.             int pos = get_kth(X);
  127.             modify_cnt(pos, P, 0);
  128.             A[pos] = P;
  129.         } else {
  130.             int L, R, K;
  131.             cin >> L >> R >> K;
  132.  
  133.             L = get_kth(L), R = get_kth(R);
  134.             cout << query(L, R, K) << "\n";
  135.         }
  136.     }  
  137. }      
  138.  
  139. signed main() {
  140.     ios_base::sync_with_stdio(false);
  141.     cin.tie(nullptr);
  142.  
  143.     int TC = 1;
  144.     cin >> TC;
  145.     while (TC--) solve();
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement