Mike4235

I love counting

Nov 16th, 2021
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.39 KB | None | 0 0
  1. /*#pragma GCC optimize("Ofast")
  2. #pragma GCC optimize("unroll-loops")
  3. #pragma GCC target("avx,avx2,fma")*/
  4.  
  5. #include <bits/stdc++.h>
  6.  
  7. #define for1(i,a,b) for (int i = a; i <= b; i++)
  8. #define for2(i,a,b) for (int i = a; i >= b; i--)
  9. // #define int long long
  10.  
  11. #define sz(a) (int)a.size()
  12. #define pii pair<int,int>
  13. #define pb push_back
  14.  
  15. /*
  16. __builtin_popcountll(x) : Number of 1-bit
  17. __builtin_ctzll(x) : Number of trailing 0
  18. */
  19.  
  20. const long double PI = 3.1415926535897932384626433832795;
  21. const int INF = 1000000000;
  22. const int MOD = 1000000007;
  23. const int MOD2 = 1000000009;
  24. const long double EPS = 1e-6;
  25.  
  26. using namespace std;
  27.  
  28. struct Query{
  29.     int l, r, a, b, id;
  30.     bool operator< (Query const& other) const{
  31.         return r < other.r;
  32.     }
  33. };
  34.  
  35. int n, q, a, b, cur;
  36. int c[100005], res[100005], lst[100005], root[400005];
  37. int child[28900005][2], val[28900005];
  38. vector<Query> v;
  39.  
  40. int init() {
  41.     child[cur][0] = child[cur][1] = 0;
  42.     val[cur] = 0;
  43.     return cur++;
  44. }
  45.  
  46. void add(int pos, int x) {
  47.     for2(i,16,0) {
  48.         bool t = x & (1 << i);
  49.         if (!child[pos][t]) child[pos][t] = init();
  50.         pos = child[pos][t];
  51.         val[pos]++;
  52.     }
  53. }
  54.  
  55. void rev(int pos, int x) {
  56.     for2(i,16,0) {
  57.         bool t = x & (1 << i);
  58.         pos = child[pos][t];
  59.         val[pos]--;
  60.     }
  61. }
  62.  
  63. int solve(int pos) {
  64.     int res = 0;
  65.     for2(i,16,0) {
  66.         bool t1 = a & (1 << i), t2 = b & (1 << i);
  67.         if (!t2) {
  68.             if (child[pos][t1]) pos = child[pos][t1];
  69.             else break;
  70.         }
  71.         else {
  72.             if (child[pos][t1]) res += val[child[pos][t1]];
  73.             if (child[pos][t1 ^ 1]) pos = child[pos][t1 ^ 1];
  74.             else break;
  75.         }
  76.  
  77.         if (i == 0) {
  78.             res += val[pos];
  79.             break;
  80.         }
  81.     }
  82.     return res;
  83. }
  84.  
  85. void build(int id, int l, int r) {
  86.     root[id] = init();
  87.     if (l == r) return;
  88.  
  89.     int m = (l + r) >> 1;
  90.     build(id * 2, l, m);
  91.     build(id * 2 + 1, m + 1, r);
  92. }
  93.  
  94. void update(int id, int l, int r, int pos, bool t) {
  95.     if (!t) rev(root[id], c[pos]);
  96.     else add(root[id], c[pos]);
  97.  
  98.     if (l == r) return;
  99.     int m = (l + r) >> 1;
  100.     if (pos <= m) update(id * 2, l, m, pos, t);
  101.     else update(id * 2 + 1, m + 1, r, pos, t);
  102. }
  103.  
  104. int query(int id, int l, int r, int tl, int tr) {
  105.     if (l > tr || r < tl) return 0;
  106.     if (l >= tl && r <= tr) {
  107.         return solve(root[id]);
  108.     }
  109.  
  110.     int m = (l + r) >> 1;
  111.     return query(id * 2, l, m, tl, tr) + query(id * 2 + 1, m + 1, r, tl, tr);
  112. }
  113.  
  114. signed main() {
  115.    
  116.     ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  117.  
  118.     // freopen("cf.inp", "r", stdin);
  119.     // freopen("cf.out", "w", stdout);
  120.  
  121.     cin >> n;
  122.     for1(i,1,n) cin >> c[i];
  123.     build(1, 1, n);
  124.  
  125.     cin >> q;
  126.        for1(i,1,q) {
  127.            int l, r, a, b;
  128.            cin >> l >> r >> a >> b;
  129.            v.push_back({l, r, a, b, i});
  130.        }
  131.  
  132.        sort(v.begin(), v.end());
  133.  
  134.        int j = 0;
  135.        for1(i,1,n) {
  136.            if (lst[c[i]]) {
  137.                update(1, 1, n, lst[c[i]], 0);
  138.            }
  139.            update(1, 1, n, i, 1);
  140.            lst[c[i]] = i;
  141.  
  142.            while (j < q && v[j].r == i) {
  143.                a = v[j].a; b = v[j].b;
  144.                res[v[j].id] = query(1, 1, n, v[j].l, v[j].r);
  145.                j++;
  146.            }
  147.        }
  148.  
  149.        for1(i,1,q) cout << res[i] << "\n";
  150.    
  151. }
Advertisement
Add Comment
Please, Sign In to add comment