#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
69456 KB |
Output is correct |
2 |
Correct |
12 ms |
66832 KB |
Output is correct |
3 |
Correct |
12 ms |
67408 KB |
Output is correct |
4 |
Correct |
13 ms |
66640 KB |
Output is correct |
5 |
Correct |
20 ms |
66640 KB |
Output is correct |
6 |
Correct |
19 ms |
66640 KB |
Output is correct |
7 |
Correct |
20 ms |
67408 KB |
Output is correct |
8 |
Correct |
21 ms |
66640 KB |
Output is correct |
9 |
Correct |
18 ms |
66656 KB |
Output is correct |
10 |
Correct |
18 ms |
66640 KB |
Output is correct |
11 |
Correct |
24 ms |
66640 KB |
Output is correct |
12 |
Correct |
17 ms |
66640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
69456 KB |
Output is correct |
2 |
Correct |
12 ms |
66832 KB |
Output is correct |
3 |
Correct |
12 ms |
67408 KB |
Output is correct |
4 |
Correct |
13 ms |
66640 KB |
Output is correct |
5 |
Correct |
20 ms |
66640 KB |
Output is correct |
6 |
Correct |
19 ms |
66640 KB |
Output is correct |
7 |
Correct |
20 ms |
67408 KB |
Output is correct |
8 |
Correct |
21 ms |
66640 KB |
Output is correct |
9 |
Correct |
18 ms |
66656 KB |
Output is correct |
10 |
Correct |
18 ms |
66640 KB |
Output is correct |
11 |
Correct |
24 ms |
66640 KB |
Output is correct |
12 |
Correct |
17 ms |
66640 KB |
Output is correct |
13 |
Correct |
22 ms |
66640 KB |
Output is correct |
14 |
Correct |
22 ms |
66640 KB |
Output is correct |
15 |
Correct |
23 ms |
66896 KB |
Output is correct |
16 |
Correct |
19 ms |
67064 KB |
Output is correct |
17 |
Correct |
22 ms |
66640 KB |
Output is correct |
18 |
Correct |
19 ms |
66640 KB |
Output is correct |
19 |
Correct |
18 ms |
66808 KB |
Output is correct |
20 |
Correct |
21 ms |
66640 KB |
Output is correct |
21 |
Correct |
16 ms |
66808 KB |
Output is correct |
22 |
Correct |
17 ms |
66640 KB |
Output is correct |
23 |
Correct |
16 ms |
66640 KB |
Output is correct |
24 |
Correct |
16 ms |
66844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
985 ms |
72920 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
69456 KB |
Output is correct |
2 |
Correct |
12 ms |
66832 KB |
Output is correct |
3 |
Correct |
12 ms |
67408 KB |
Output is correct |
4 |
Correct |
13 ms |
66640 KB |
Output is correct |
5 |
Correct |
20 ms |
66640 KB |
Output is correct |
6 |
Correct |
19 ms |
66640 KB |
Output is correct |
7 |
Correct |
20 ms |
67408 KB |
Output is correct |
8 |
Correct |
21 ms |
66640 KB |
Output is correct |
9 |
Correct |
18 ms |
66656 KB |
Output is correct |
10 |
Correct |
18 ms |
66640 KB |
Output is correct |
11 |
Correct |
24 ms |
66640 KB |
Output is correct |
12 |
Correct |
17 ms |
66640 KB |
Output is correct |
13 |
Correct |
22 ms |
66640 KB |
Output is correct |
14 |
Correct |
22 ms |
66640 KB |
Output is correct |
15 |
Correct |
23 ms |
66896 KB |
Output is correct |
16 |
Correct |
19 ms |
67064 KB |
Output is correct |
17 |
Correct |
22 ms |
66640 KB |
Output is correct |
18 |
Correct |
19 ms |
66640 KB |
Output is correct |
19 |
Correct |
18 ms |
66808 KB |
Output is correct |
20 |
Correct |
21 ms |
66640 KB |
Output is correct |
21 |
Correct |
16 ms |
66808 KB |
Output is correct |
22 |
Correct |
17 ms |
66640 KB |
Output is correct |
23 |
Correct |
16 ms |
66640 KB |
Output is correct |
24 |
Correct |
16 ms |
66844 KB |
Output is correct |
25 |
Incorrect |
985 ms |
72920 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |