This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O2,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
struct Node {
Node* nxt[2];
set<int> s;
};
inline Node* create() {
Node* res = new Node();
for (int i = 0; i < 2; ++ i)
res->nxt[i] = NULL;
res->s.clear();
return res;
}
int n = 1;
int numQuery;
pair<bool, pair<int, int>> qry[200005];
vector<int> adj[200005];
int in[200005], out[200005], timer = 0, Xor[200005];
Node* root = create();
inline int getbit(int x, int i) {
return x >> i & 1;
}
inline void dfs(int u, int par = -1) {
in[u] = ++ timer;
for (int& v : adj[u])
if (v != par) dfs(v, u);
out[u] = timer;
}
inline void trie_insert(int x, int pos) {
Node* cur = root;
for (int i = 29; i >= 0; -- i) {
int c = getbit(x, i);
if (cur->nxt[c] == NULL)
cur->nxt[c] = create();
cur = cur->nxt[c];
cur->s.emplace(pos);
}
}
inline int trie_get(int x, int u) {
int res = 0;
Node* cur = root;
for (int i = 29; i >= 0; -- i) {
int lf = 0, rt = 1;
if (getbit(x, i)) swap(lf, rt);
if (cur->nxt[rt] == NULL) {
cur = cur->nxt[lf];
} else {
auto it = cur->nxt[rt]->s.lower_bound(in[u]);
if (it != cur->nxt[rt]->s.end() && *it <= out[u]) {
res |= (1 << i);
cur = cur->nxt[rt];
} else {
cur = cur->nxt[lf];
}
}
}
return res;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> numQuery;
for (int i = 1; i <= numQuery; ++ i) {
string op;
cin >> op >> qry[i].S.F >> qry[i].S.S;
if (op == "Add") {
qry[i].F = 0;
int u = qry[i].S.F;
int w = qry[i].S.S;
int v = ++ n;
Xor[v] = Xor[u] ^ w;
adj[u].emplace_back(v);
qry[i].S.F = v;
} else qry[i].F = 1;
}
dfs(1);
trie_insert(0, in[1]);
for (int i = 1; i <= numQuery; ++ i) {
if (!qry[i].F) {
trie_insert(Xor[qry[i].S.F], in[qry[i].S.F]);
} else {
cout << trie_get(Xor[qry[i].S.F], qry[i].S.S) << "\n";
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |