Submission #198074

#TimeUsernameProblemLanguageResultExecution timeMemory
198074AryaKnightKlasika (COCI20_klasika)C++14
110 / 110
4437 ms464956 KiB
#include<bits/stdc++.h> using namespace std; #define all(a) a.begin(),a.end() #define F first #define S second #define pb push_back #define ll long long #define vi vector<int> #define pi pair<int,int> #define mp make_pair #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #endif const int mod=1e9+7; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int mul(int a,int b) { return ((a)*1ll*(b))%mod; } void add(int &a,int b) { a+=b; if(a>=mod)a-=mod; } int sub(int a,int b){ a-=b; if(a<0){ a+=mod; } return a; } int powz(int a,int b) { int res=1; while(b) { if(b&1){ res=mul(res,a); } b/=2; a=mul(a,a); } return res; } template <typename A, typename B> istream& operator>>(istream& input,pair<A,B>& x) { input>>x.F>>x.S; return input; } template <typename A> istream& operator>>(istream& input,vector<A>& x) { for(auto& i:x) input>>i; return input; } template<typename A> ostream& operator<<(ostream& output,vector<A>& x) { for(auto& i:x) output<<i<<' '; return output; } const int N=200005,N2=1000000; struct Node{ Node *zero,*one; set<int>st; Node(){ zero=one=NULL; } Node(Node *left,Node *right){ zero=left; right=one; } }; void trie_add(Node *node,int val,int ind,int bits=30){ node->st.insert(ind); if(bits==-1){ return; } if(val&(1ll<<bits)){ if(node->one==NULL){ node->one=new Node(); } trie_add(node->one,val,ind,bits-1); } else{ if(node->zero==NULL){ node->zero=new Node(); } trie_add(node->zero,val,ind,bits-1); } } int trie_query(Node *node,int val,int fst,int lst,int bits=30){ if(bits==-1){ return 0; } if(val&(1ll<<bits)){ if(node->zero==NULL){ return trie_query(node->one,val,fst,lst,bits-1); } auto it=node->zero->st.lower_bound(fst); if(it==node->zero->st.end()||(*it)>=lst){ return trie_query(node->one,val,fst,lst,bits-1); } else{ return (1ll<<bits)+trie_query(node->zero,val,fst,lst,bits-1); } } else{ if(node->one==NULL){ return trie_query(node->zero,val,fst,lst,bits-1); } auto it=node->one->st.lower_bound(fst); if(it==node->one->st.end()||(*it)>=lst){ return trie_query(node->zero,val,fst,lst,bits-1); } else{ return (1ll<<bits)+trie_query(node->one,val,fst,lst,bits-1); } } } vector<int>adj[N]; int in[N],out[N],tt=0,prec[N]; map<pi,int>xr; void dfs(int u,int p,int val=0){ in[u]=tt++; prec[u]=val; for(auto i:adj[u]){ if(i!=p){ dfs(i,u,xr[{i,u}]^val); } } out[u]=tt; } void solve(){ int q; cin>>q; int n=0; vector<pi>queries(q); vector<bool>add(q); for(int i=0;i<q;i++){ string s; cin>>s; if(s[0]=='A'){ int x,y; cin>>x>>y; x--; adj[x].pb(++n); xr[{x,n}]=y; xr[{n,x}]=y; queries[i]={n,-1}; adj[n].pb(x); add[i]=true; } else{ int x,y; cin>>x>>y; x--;y--; queries[i]={x,y}; } } Node root; trie_add(&root,0,0); dfs(0,-1,0); for(int i=0;i<q;i++){ if(add[i]){ trie_add(&root,prec[queries[i].F],in[queries[i].F]); } else{ cout<<trie_query(&root,prec[queries[i].F],in[queries[i].S],out[queries[i].S])<<"\n"; } } } signed main(){ ios_base::sync_with_stdio(false); cin.tie(0); int tc=1; //~cin>>tc; for(int _=0;_<tc;_++){ //~ cout<<"Case #"<<_+1<<": "; solve(); if(_!=tc-1) cout<<"\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...