Submission #340040

#TimeUsernameProblemLanguageResultExecution timeMemory
340040sinamhdvKlasika (COCI20_klasika)C++11
110 / 110
3054 ms439760 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int mod = 1000 * 1000 * 1000 + 7; const int INF = 1000 * 1000 * 1000; const ll LINF = (ll)INF * INF; #ifdef DEBUG #define dbg(x) cout << #x << " = " << (x) << endl << flush; #define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; } #else #define dbg(x) ; #define dbgr(s, f) ; #endif #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define msub(a, b) ((mod + ((a) - (b)) % mod) % mod) #define mdiv(a, b) ((a) * poww((b), mod - 2) % mod) #define all(x) (x).begin(), (x).end() #define pb push_back #define mp make_pair #define fr first #define sc second #define endl '\n' struct QR { char op; int x, y; }; #define MAXN 200100 #define LOGN 31 int n, q; int dp[MAXN]; vector<pii> adj[MAXN]; QR qr[MAXN]; int sttime[MAXN], fntime[MAXN], timer; void dfs(int v, int x) { sttime[v] = timer++; dp[v] = x; for (pii p : adj[v]) { dfs(p.fr, x ^ p.sc); } fntime[v] = timer; } inline bool hasrng(const set<int> &ts, int l, int r) { return ts.lower_bound(l) != ts.upper_bound(r); } struct node { node *cld[2]; set<int> ts; }; node trie; void add(int i, int x) { node *v = &trie; for (int bpos = LOGN - 1; bpos >= 0; bpos--) { int c = (x & (1 << bpos)) >> bpos; if (!v -> cld[c]) { v -> cld[c] = new node; memset(v -> cld[c] -> cld, 0, 2 * sizeof(node*)); } (v -> ts).insert(i); v = v -> cld[c]; } (v -> ts).insert(i); } int query(int l, int r, int x) { int res = 0; node *v = &trie; for (int bpos = LOGN - 1; bpos >= 0; bpos--) { int b = (x & (1 << bpos)) >> bpos; if (v -> cld[1 - b] && hasrng(v -> cld[1 - b] -> ts, l, r)) { res |= (1 << bpos); v = v -> cld[1 - b]; } else { v = v -> cld[b]; } } return res; } int32_t main(void) { fast_io; n = 1; cin >> q; FOR(i, 0, q) { char op[10]; int x, y; cin >> op >> x >> y; qr[i] = {op[0], x, y}; if (op[0] == 'A') { n++; adj[x].pb(mp(n, y)); } } dfs(1, 0); int cnt = 1; add(sttime[1], dp[1]); FOR(i, 0, q) { if (qr[i].op == 'A') { cnt++; add(sttime[cnt], dp[cnt]); } else { cout << query(sttime[qr[i].y], fntime[qr[i].y] - 1, dp[qr[i].x]) << endl; } } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...