Submission #459656

#TimeUsernameProblemLanguageResultExecution timeMemory
459656AntekbKlasika (COCI20_klasika)C++14
33 / 110
5060 ms14572 KiB
#include<bits/stdc++.h> #define st first #define nd second using namespace std; const int N = (1<<18), M=6e7; 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; const int L=500; //for(int i=N; i>0; i>>=1)ins(i, 0); ins(L+1, 0); ins(1, 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>>=1, r>>=1){ if(l&1){ ans=max(ans, find(root[l++], u)); } if(r&1){ ans=max(ans, find(root[--r], u)); } }*/ while(l<r){ ans=max(ans, find(l+L+1, u)); l++; } while(l<r && r%L){ ans=max(ans, find((--r)+L+1, u)); } l/=L; r/=L; while(l<r){ ans=max(ans, find(l+1, u)); l++; } cout<<ans<<"\n"; } else{ int t=pre[Q[i].nd.st], u=xo[Q[i].nd.st]; ins(t+L+1, u); ins(t/L+1, u); } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...