This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int L = 20;
const int B = 2500;
int n, q;
vector<pair<int, int>> g[N];
int tin[N], tout[N];
int cur = 1;
string que[N];
int x[N], y[N];
int time_dfs = 0;
int fst[N], lv[N], f[N];
int bl[N];
bool on[N];
int rev[N];
struct trie {
struct node {
int nxt[2];
node() {
memset(nxt, -1, sizeof(nxt));
}
} t[30 * (B + 1) + 10];
int cur = 1;
void add(int u) {
int p = 1;
for (int i = 29; i >= 0; i--) {
int j = (u >> i & 1);
if (t[p].nxt[j] == -1) {
t[p].nxt[j] = ++cur;
}
p = t[p].nxt[j];
}
}
int get(int u) {
int p = 1;
int res = 0;
for (int i = 29; i >= 0; i--) {
int j = (u >> i & 1);
if (t[p].nxt[1 ^ j] != -1) {
res += (1 << j);
p = t[p].nxt[1 ^ j];
} else if (t[p].nxt[j] != -1) {
p = t[p].nxt[j];
} else {
return -1;
}
}
return res;
}
} tr[N / B + 10];
void dfs(int u) {
tin[u] = ++time_dfs;
rev[tin[u]] = u;
for (pair<int, int> &p : g[u]) {
lv[p.first] = lv[u] + 1;
f[p.first] = p.second ^ f[u];
dfs(p.first);
}
tout[u] = time_dfs;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> q;
for (int i = 1; i <= q; i++) {
cin >> que[i] >> x[i] >> y[i];
if (que[i] == "Add") {
cur++;
g[x[i]].emplace_back(cur, y[i]);
}
}
n = cur;
dfs(1);
for (int i = 1; i <= n; i++) {
bl[i] = (i + B - 1) / B;
}
int cur = 1;
on[1] = true;
for (int i = 1; i <= q; i++) {
if (que[i] == "Add") {
cur++;
on[cur] = true;
tr[bl[tin[cur]]].add(f[cur]);
} else if (que[i] == "Query") {
int in = tin[y[i]];
int out = tout[y[i]];
int res = 0;
if (bl[in] == bl[out]) {
for (int j = in; j <= out; j++) {
if (on[rev[j]]) {
res = max(res, f[x[i]] ^ f[rev[j]]);
}
}
} else {
for (int j = in; j <= min(n, B * bl[in]); j++) {
if (on[rev[j]]) {
res = max(res, f[x[i]] ^ f[rev[j]]);
}
}
for (int j = bl[in] + 1; j <= bl[out] - 1; j++) {
res = max(res, tr[j].get(f[x[i]]));
}
for (int j = B * (bl[out] - 1) + 1; j <= out; j++) {
if (on[rev[j]]) {
res = max(res, f[x[i]] ^ f[rev[j]]);
}
}
}
cout << res << '\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... |