sk__j

Tester_Catch_Me_If_You_Can

Nov 4th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1.     #include <stdio.h>
  2.     #include <iostream>
  3.     #include <map>
  4.     #include <vector>
  5.     #include <utility>
  6.     #include <cmath>
  7.     #include <cstring>
  8.     #include <algorithm>
  9.     using namespace std;
  10.      
  11.     typedef long long int ll;
  12.     typedef pair<ll, ll> ipair;
  13.     #define mod 1000000007
  14.     #define pb push_back
  15.     #define mp make_pair
  16.     #define ff first
  17.     #define ss second
  18.     #define sz size()
  19.     #define ln length()
  20.     #define rep(i,n) for(i=0;i<n;i++)
  21.     #define fu(i,a,n) for(i=a;i<=n;i++)
  22.     #define fd(i,n,a) for(i=n;i>=a;i--)
  23.     #define all(a)  a.begin(),a.end()
  24.     #define gi(n) scanf("%d",&n)
  25.     #define gl(n) scanf("%lld",&n)
  26.     #define pi(n) printf("%d",n)
  27.     #define pl(n) printf("%lld",n);
  28.     #define pp printf(" ")
  29.     #define pn printf("\n")
  30.     #define LN 31
  31.     #define MAX 100005
  32.      
  33.      
  34.     struct trie {
  35.         trie *child[2];
  36.         ll cnt;
  37.         trie(ll c, trie *a, trie *b) {
  38.             child[0] = a;
  39.             child[1] = b;
  40.             cnt = c;
  41.         }
  42.     };
  43.     trie *null = new trie(0, NULL, NULL);
  44.     ll arr[MAX];
  45.     trie * root[MAX];
  46.     trie *insert(trie *proot, ll val) {
  47.         trie *nroot = new trie(0, null, null);
  48.         trie *cur = nroot;
  49.         ll i;
  50.         fd(i, LN - 1, 0) {
  51.             int ch = (val >> i)&1;
  52.             cur->child[ch] = new trie(0, null, null);
  53.             cur->child[1^ch] = proot->child[1^ch];
  54.             cur = cur->child[ch];
  55.             proot = proot->child[ch];
  56.             cur->cnt = proot->cnt + 1;
  57.         }
  58.         return nroot;
  59.     }
  60.     ll queryRes(trie *a, trie *b, ll x, ll y) {
  61.         ll ans = 0;
  62.         ll i;
  63.         fd(i, LN - 1, 0) {
  64.             int ch = (x >> i)&1;
  65.             int req = (y >> i)&1;
  66.             if(!req) {
  67.                 ans = ans + a->child[1^ch]->cnt - b->child[1^ch]->cnt;
  68.                 a = a->child[ch];
  69.                 b = b->child[ch];
  70.             }
  71.             else {
  72.                 a = a->child[1^ch];
  73.                 b = b->child[1^ch];
  74.             }
  75.         }
  76.         ans = ans + a->cnt - b->cnt;
  77.         return ans;
  78.     }
  79.      
  80.      
  81.     int main() {
  82.         null->child[0] = null->child[1] = null;
  83.         root[0] = null;
  84.         int t;
  85.         t = 1;
  86.         while(t--) {
  87.             ll n, q;
  88.             gl(n);
  89.             gl(q);
  90.             ll i, k;
  91.             fu(i, 1, n) {
  92.                 gl(k);
  93.                 root[i] = insert(root[i - 1], k);
  94.             }
  95.             while(q--) {
  96.                 ll l, r, x, y;
  97.                 gl(l);
  98.                 gl(r);
  99.                 gl(x);
  100.                 gl(y);
  101.                 ll ans = queryRes(root[r], root[l - 1], x, y);
  102.                 pl(ans);
  103.                 pn;
  104.             }
  105.         }
  106.         return 0;
  107.     }
Add Comment
Please, Sign In to add comment