Submission #459633

#TimeUsernameProblemLanguageResultExecution timeMemory
459633AntekbKlasika (COCI20_klasika)C++14
0 / 110
847 ms151244 KiB
#include<bits/stdc++.h> #define st first #define nd second using namespace std; const int N = (1<<18), M=2e7; int pre[N], post[N], xo[N]; vector<int> E[N]; int L[M], R[M]; int wsk; int root[N+N]; void dfs(int v){ pre[v]=wsk++; for(int u:E[v])dfs(u); post[v]=wsk; } const int K=31; void ins(int r, int x){ //cout<<"+ "<<r<<" "<<x<<"\n"; for(int i=K-1; i>=0; i--){ if(x&(1<<i)){ if(!L[r])L[r]=wsk++; r=L[r]; } else{ if(!R[r])R[r]=wsk++; r=R[r]; } } } int find(int r, int x){ //cout<<"? "<<r<<" "<<x<<"\n"; int ans=0; for(int i=K-1; i>=0; i--){ if((x&(1<<i))==0){ if(!L[r])r=R[r]; else r=L[r], ans+=(1<<i); } else{ if(!R[r])r=L[r]; else r=R[r], ans+=(1<<i); } } //cout<<"! "<<ans<<"\n"; return ans; } int main(){ int q; cin>>q; int n=1; vector<pair<int, pair<int, int> > > Q(q); for(int i=0; i<q; i++){ string s; cin>>s; cin>>Q[i].nd.st>>Q[i].nd.nd; if(s!="Add"){ Q[i].st=1; } else{ E[Q[i].nd.st].push_back(++n); xo[n]=xo[Q[i].nd.st]^Q[i].nd.nd; //cout<<n<<" "<<Q[i].nd.st<<"" Q[i].nd.st=n; } } dfs(1); wsk=2*N; for(int i=1; i<2*N; i++)root[i]=i; for(int i=0; i<q; i++){ if(Q[i].st){ int l=pre[Q[i].nd.nd], r=post[Q[i].nd.nd]; //cout<<l<<" "<<r<<"\n"; int u=xo[Q[i].nd.st], ans=0; for(l+=N, r+=N; l<r; l>>=1, r>>=1){ if(l&1){ ans=max(ans, find(root[l++], u)); } if(r&1){ ans=max(ans, find(root[--r], u)); } } cout<<ans<<"\n"; } else{ int t=pre[Q[i].nd.st], u=xo[Q[i].nd.st]; t+=N; while(t){ ins(root[t], u); t>>=1; } } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...