Submission #889429

#TimeUsernameProblemLanguageResultExecution timeMemory
889429dimashhhKlasika (COCI20_klasika)C++17
0 / 110
649 ms524288 KiB
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 1, MOD = 998244353,b = 30; typedef long long ll; vector<pair<int,int>> ans; struct node{ node *next[2]; int col = 0; node(){ col = 0; for(int i = 0;i < 2;i++){ next[i] = nullptr; } } }; using pnode = node *; void add(pnode &x,int val,int delta){ if(!x) x = new node(); pnode f = x; for(int i = b;i >= 0;i--){ int nv = ((val >> i) & 1); // cout << nv << "x\n"; if(!x->next[nv]){ x->next[nv] = new node(); } x = x->next[nv]; x->col+=delta; } x = f; } int get(int val,pnode x){ int res = 0; if(!x) return 0; for(int i = b;i >= 0;i--){ int nv = ((val >> i) & 1); nv = (!nv); // cout << (x->next[nv] ? 1 : 0) << ' ' << (x->next[!nv] ? 1 : 0) << '\n'; if(x->next[nv] && x->next[nv]->col){ res += (1ll << i); x = x->next[nv]; }else{ x = x->next[(!nv)]; } } return res; } pnode T[N * 4]; int n,q,tadd[N],d[N],s[N]; vector<pair<int,int>> g[N],qr[N]; void dfs(int v){ s[v] = 1; for(auto [to,w]:g[v]){ d[to] = (d[v] ^ w); dfs(to); s[v] += s[to]; } } void upd(int pos,int val,int d,int v = 1,int tl = 1,int tr = N){ add(T[v],val,d); // if(tl == tr && tl == 1){ // cout << val << "x\n"; // } // cout << v << " " << tl << ' ' << tr << "f\n"; if(tl == tr) return; int tm = (tl + tr) >> 1; if(pos <= tm) upd(pos,val,d,v+v,tl,tm); else upd(pos,val,d,v+v+1,tm+1,tr); } void add_sub(int v,int dd){ upd(tadd[v],d[v],dd); // cout << tadd[v] << ' ' << d[v] << ' ' << dd << "q\n"; for(auto [to,w]:g[v]){ add_sub(to,dd); } } int get(int l,int r,int x,int v = 1,int tl = 1,int tr = N){ if(l > r || tl > r || l > tr) return 0; if(tl >= l && tr <= r){ // cout << v << " " << tl << ' ' << tr << '\n'; return get(x,T[v]); }else{ int tm = (tl + tr) >> 1; return max(get(l,r,x,v+v,tl,tm),get(l,r,x,v+v+1,tm+1,tr)); } } void _dfs(int v,bool keep = true){ int b = -1; for(auto [to,w]:g[v]){ if(b == -1 || s[to] > s[b]){ b = to; } } for(auto [to,w]:g[v]){ if(to != b){ _dfs(to,false); } } if(b != -1){ _dfs(b,1); } for(auto [to,w]:g[v]){ if(to == b) continue; add_sub(to,1); } upd(tadd[v],d[v],1); // cout << tadd[v] << ' ' << d[v] << ' ' << 1 << "q\n"; // cout << b << '\n'; for(auto [x,y]:qr[v]){ // cout << 1 << ' ' << y - 1 << ' ' << x << ' ' << d[x] << " " << get(1,y - 1,d[x]) << '\n'; ans.push_back({y,get(1,y - 1,d[x])}); // exit(0); } if(!keep) add_sub(v,-1); } void test(){ cin >> q; n = 1; tadd[1] = 1; for(int i = 1;i <= q;i++){ string tp; cin >> tp; if(tp[0] == 'A'){ n++; int x,y; cin >> x >> y; g[x].push_back({n,y}); tadd[n] = i + 1; }else{ int a,b; cin >> a >> b; qr[b].push_back({a,i + 1}); } } dfs(1); _dfs(1); sort(ans.begin(),ans.end()); for(auto [x,y]:ans){ cout << y << '\n'; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int T = 1; // cin >> T; while (T--) test(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...