Submission #891758

#TimeUsernameProblemLanguageResultExecution timeMemory
891758FucsdeptraiKlasika (COCI20_klasika)C++17
33 / 110
5054 ms28544 KiB
#include<bits/stdc++.h> using namespace std; using ll = long long; const int N = 2e5 + 9; const int Log = 30; struct Node { Node *c[2]; int mn; Node() { c[0] = c[1] = NULL; mn = N; } }; int n, m, q, Timer; int d[N], res[N]; vector<pair<int, int>> adj[N], vec[N]; vector<pair<pair<int, int>, int>> que[N]; Node *Trie; void Add(int x, int y) { Node *p = Trie; for(int i = Log; i >= 0;i --) { bool w = (1<<i)&x; if(p->c[w] == NULL) p->c[w] = new Node; p = p->c[w]; p->mn = min(p->mn, y); } } void Remove(Node *p) { if(p->c[0] != NULL) Remove(p->c[0]); if(p->c[1] != NULL) Remove(p->c[1]); delete p; } int Get(int x, int y) { Node *p = Trie; for(int i = Log; i >= 0;i --) { bool w = (1<<i)&x; if(p->c[w^1] != NULL and p->c[w^1]->mn <= y) { x ^= ((w^1)<<i); p = p->c[w^1]; } else if(p->c[w] != NULL and p->c[w]->mn <= y) { x ^= (w<<i); p = p->c[w]; } else { return 0; } } return x; } void Dfs(int u = 1, int du = 0) { d[u] = du; for(auto [v, w] : adj[u]) { Dfs(v, du^w); } } void Merge(int u, int v) { if(vec[u].size() < vec[v].size()) swap(vec[u], vec[v]); for(auto x : vec[v]) vec[u].push_back(x); } void Dfs1(int u = 1) { vec[u].push_back({d[u], u}); for(auto [v, w] : adj[u]) { Dfs1(v); Merge(u, v); } Trie = new Node(); for(auto [x, y] : vec[u]) { Add(x, y); } for(auto [xy, id] : que[u]) { auto [x, y] = xy; res[id] = Get(d[x], y); } Remove(Trie); } void Solve() { cin>>q; ++n; while(q--) { string Type; cin>>Type; int x, y; cin >> x >> y; if(Type == "Add") { adj[x].push_back({++n, y}); } else { que[y].push_back({{x, n}, ++m}); } } Timer = 0; Dfs(); Dfs1(); for(int i = 1;i <= m; i++) { cout << res[i] << '\n'; } } signed main() { // ios_base::sync_with_stdio(0); // cin.tie(0); int TC = 1; // cin>>TC; while(TC--) { Solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...