Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ld long double
- #define int long long
- #define pii pair<int,int>
- #define X first
- #define Y second
- #define mp make_pair
- #define MOD 1000000007ll
- #define inf 1000000000000000ll
- using namespace std;
- inline int binexp(int a,int b,int m)
- {
- int res=1;
- a%=m;
- while(b)
- {
- if(b&1)
- {
- res=(res*1ll*a)%m;
- }
- a=(a*1ll*a)%m;
- b>>=1;
- }
- return res;
- }
- const int N=1e5+5;
- int a[N];
- struct TrieNode
- {
- int val;
- TrieNode *child[2];
- TrieNode()
- {
- child[0]=child[1]=NULL;
- val=0;
- }
- };
- TrieNode *trie[N];
- inline TrieNode *insert(TrieNode *root,TrieNode *prev,int num,int level=31)
- {
- if(level==-1)
- return root;
- bool f=num&(1ll<<level);
- if(root==NULL)
- root=new TrieNode();
- if(prev)
- {
- root->child[!f]=prev->child[!f];
- }
- root->child[f]=new TrieNode();
- root->child[f]->val=1;
- if(prev and prev->child[f])
- {
- root->child[f]->val+=prev->child[f]->val;
- }
- root->child[f]=insert(root->child[f],(prev!=NULL)?prev->child[f]:prev,num,level-1);
- return root;
- }
- inline int getcount(TrieNode *root,int num,int target)
- {
- int ans=0;
- for(int i=31;i>=0;i--)
- {
- bool f1=target&(1ll<<i),f2=num&(1ll<<i);
- if(f1==1)
- {
- if(root->child[!f2]==NULL)
- return ans;
- else
- root=root->child[!f2];
- }
- else
- {
- if(root->child[!f2])
- {
- ans+=root->child[!f2]->val;
- }
- if(root->child[f2]!=NULL)
- {
- root=root->child[f2];
- }
- else
- {
- return ans;
- }
- }
- }
- return ans+root->val;
- }
- signed main()
- {
- ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
- #ifndef ONLINE_JUDGE
- freopen("in11.txt","r",stdin);
- freopen("out11.txt","w",stdout);
- #endif
- int n,q;
- cin>>n>>q;
- for(int i=1;i<=n;i++)
- cin>>a[i];
- for(int i=1;i<=n;i++)
- {
- trie[i]=new TrieNode();
- trie[i]=insert(trie[i],trie[i-1],a[i]);
- }
- while(q--)
- {
- int l,r,x,y;
- cin>>l>>r>>x>>y;
- int ans=getcount(trie[r],x,y);
- if(l!=1)
- ans-=getcount(trie[l-1],x,y);
- cout<<ans<<"\n";
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment