Submission #459657

#TimeUsernameProblemLanguageResultExecution timeMemory
459657AntekbKlasika (COCI20_klasika)C++14
110 / 110
1792 ms286864 KiB
#include<bits/stdc++.h> #define st first #define nd second using namespace std; const int N = (1<<16), M=6e7; int pre[5*N], post[5*N], xo[5*N]; vector<int> E[5*N]; int L[M], R[M]; int wsk; //int root[5*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=5*N; //for(int i=1; i<5*N; i++)root[i]=i; for(int i=N; i>0; i/=4)ins(i, 0); 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>>=2, r>>=2){ while((l&3) && l<r){ ans=max(ans, find(l++, u)); } while((r&3) && l<r){ ans=max(ans, find(--r, u)); } } cout<<ans<<"\n"; } else{ int t=pre[Q[i].nd.st], u=xo[Q[i].nd.st]; t+=N; while(t){ ins(t, u); t>>=2; } } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...