sk__j

Author_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.52 KB | None | 0 0
  1.     #include <bits/stdc++.h>
  2.      
  3.     #define ld long double
  4.     #define int long long
  5.     #define pii pair<int,int>
  6.     #define X first
  7.     #define Y second
  8.     #define mp make_pair
  9.     #define MOD 1000000007ll
  10.     #define inf 1000000000000000ll
  11.      
  12.     using namespace std;
  13.      
  14.     inline int binexp(int a,int b,int m)
  15.     {
  16.         int res=1;
  17.         a%=m;
  18.         while(b)
  19.         {
  20.             if(b&1)
  21.             {
  22.                 res=(res*1ll*a)%m;
  23.             }
  24.             a=(a*1ll*a)%m;
  25.             b>>=1;
  26.         }
  27.         return res;
  28.     }
  29.     const int N=1e5+5;
  30.      
  31.     int a[N];
  32.      
  33.     struct TrieNode
  34.     {
  35.         int val;
  36.         TrieNode *child[2];
  37.         TrieNode()
  38.         {
  39.             child[0]=child[1]=NULL;
  40.             val=0;
  41.         }
  42.     };
  43.      
  44.     TrieNode *trie[N];
  45.      
  46.     inline TrieNode *insert(TrieNode *root,TrieNode *prev,int num,int level=31)
  47.     {
  48.         if(level==-1)
  49.             return root;
  50.         bool f=num&(1ll<<level);
  51.         if(root==NULL)
  52.             root=new TrieNode();
  53.         if(prev)
  54.         {
  55.             root->child[!f]=prev->child[!f];
  56.         }
  57.         root->child[f]=new TrieNode();
  58.         root->child[f]->val=1;
  59.         if(prev and prev->child[f])
  60.         {
  61.             root->child[f]->val+=prev->child[f]->val;
  62.         }
  63.         root->child[f]=insert(root->child[f],(prev!=NULL)?prev->child[f]:prev,num,level-1);
  64.         return root;
  65.     }
  66.      
  67.     inline int getcount(TrieNode *root,int num,int target)
  68.     {
  69.         int ans=0;
  70.         for(int i=31;i>=0;i--)
  71.         {
  72.             bool f1=target&(1ll<<i),f2=num&(1ll<<i);
  73.             if(f1==1)
  74.             {
  75.                 if(root->child[!f2]==NULL)
  76.                     return ans;
  77.                 else
  78.                     root=root->child[!f2];
  79.             }
  80.             else
  81.             {
  82.                 if(root->child[!f2])
  83.                 {
  84.                     ans+=root->child[!f2]->val;
  85.                 }
  86.                 if(root->child[f2]!=NULL)
  87.                 {
  88.                     root=root->child[f2];
  89.                 }
  90.                 else
  91.                 {
  92.                     return ans;
  93.                 }
  94.             }
  95.         }
  96.         return ans+root->val;
  97.     }
  98.      
  99.     signed main()
  100.     {
  101.         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  102.         #ifndef ONLINE_JUDGE
  103.         freopen("in11.txt","r",stdin);
  104.         freopen("out11.txt","w",stdout);
  105.         #endif
  106.         int n,q;
  107.         cin>>n>>q;
  108.         for(int i=1;i<=n;i++)
  109.             cin>>a[i];
  110.         for(int i=1;i<=n;i++)
  111.         {
  112.             trie[i]=new TrieNode();
  113.             trie[i]=insert(trie[i],trie[i-1],a[i]);
  114.         }
  115.         while(q--)
  116.         {
  117.             int l,r,x,y;
  118.             cin>>l>>r>>x>>y;
  119.             int ans=getcount(trie[r],x,y);
  120.             if(l!=1)
  121.                 ans-=getcount(trie[l-1],x,y);
  122.             cout<<ans<<"\n";
  123.         }
  124.         return 0;
  125.     }
Add Comment
Please, Sign In to add comment