#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 << i);
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 |
14 ms |
69456 KB |
Output is correct |
2 |
Correct |
14 ms |
69464 KB |
Output is correct |
3 |
Correct |
14 ms |
69404 KB |
Output is correct |
4 |
Correct |
15 ms |
69628 KB |
Output is correct |
5 |
Correct |
15 ms |
69456 KB |
Output is correct |
6 |
Correct |
14 ms |
69456 KB |
Output is correct |
7 |
Correct |
16 ms |
69456 KB |
Output is correct |
8 |
Correct |
13 ms |
69456 KB |
Output is correct |
9 |
Correct |
13 ms |
69436 KB |
Output is correct |
10 |
Correct |
14 ms |
69456 KB |
Output is correct |
11 |
Correct |
16 ms |
69348 KB |
Output is correct |
12 |
Correct |
14 ms |
69412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
69456 KB |
Output is correct |
2 |
Correct |
14 ms |
69464 KB |
Output is correct |
3 |
Correct |
14 ms |
69404 KB |
Output is correct |
4 |
Correct |
15 ms |
69628 KB |
Output is correct |
5 |
Correct |
15 ms |
69456 KB |
Output is correct |
6 |
Correct |
14 ms |
69456 KB |
Output is correct |
7 |
Correct |
16 ms |
69456 KB |
Output is correct |
8 |
Correct |
13 ms |
69456 KB |
Output is correct |
9 |
Correct |
13 ms |
69436 KB |
Output is correct |
10 |
Correct |
14 ms |
69456 KB |
Output is correct |
11 |
Correct |
16 ms |
69348 KB |
Output is correct |
12 |
Correct |
14 ms |
69412 KB |
Output is correct |
13 |
Correct |
15 ms |
69456 KB |
Output is correct |
14 |
Correct |
14 ms |
69408 KB |
Output is correct |
15 |
Correct |
15 ms |
69456 KB |
Output is correct |
16 |
Correct |
15 ms |
69456 KB |
Output is correct |
17 |
Correct |
15 ms |
69456 KB |
Output is correct |
18 |
Correct |
15 ms |
69456 KB |
Output is correct |
19 |
Correct |
15 ms |
69456 KB |
Output is correct |
20 |
Correct |
15 ms |
69456 KB |
Output is correct |
21 |
Correct |
21 ms |
69456 KB |
Output is correct |
22 |
Correct |
15 ms |
69456 KB |
Output is correct |
23 |
Correct |
17 ms |
66852 KB |
Output is correct |
24 |
Correct |
14 ms |
69416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
978 ms |
73288 KB |
Output is correct |
2 |
Correct |
1088 ms |
80840 KB |
Output is correct |
3 |
Correct |
1002 ms |
84416 KB |
Output is correct |
4 |
Correct |
691 ms |
87644 KB |
Output is correct |
5 |
Correct |
1500 ms |
75272 KB |
Output is correct |
6 |
Correct |
1358 ms |
76052 KB |
Output is correct |
7 |
Correct |
1577 ms |
77128 KB |
Output is correct |
8 |
Correct |
973 ms |
77908 KB |
Output is correct |
9 |
Correct |
1031 ms |
75592 KB |
Output is correct |
10 |
Correct |
858 ms |
77128 KB |
Output is correct |
11 |
Correct |
849 ms |
78428 KB |
Output is correct |
12 |
Correct |
542 ms |
79944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
69456 KB |
Output is correct |
2 |
Correct |
14 ms |
69464 KB |
Output is correct |
3 |
Correct |
14 ms |
69404 KB |
Output is correct |
4 |
Correct |
15 ms |
69628 KB |
Output is correct |
5 |
Correct |
15 ms |
69456 KB |
Output is correct |
6 |
Correct |
14 ms |
69456 KB |
Output is correct |
7 |
Correct |
16 ms |
69456 KB |
Output is correct |
8 |
Correct |
13 ms |
69456 KB |
Output is correct |
9 |
Correct |
13 ms |
69436 KB |
Output is correct |
10 |
Correct |
14 ms |
69456 KB |
Output is correct |
11 |
Correct |
16 ms |
69348 KB |
Output is correct |
12 |
Correct |
14 ms |
69412 KB |
Output is correct |
13 |
Correct |
15 ms |
69456 KB |
Output is correct |
14 |
Correct |
14 ms |
69408 KB |
Output is correct |
15 |
Correct |
15 ms |
69456 KB |
Output is correct |
16 |
Correct |
15 ms |
69456 KB |
Output is correct |
17 |
Correct |
15 ms |
69456 KB |
Output is correct |
18 |
Correct |
15 ms |
69456 KB |
Output is correct |
19 |
Correct |
15 ms |
69456 KB |
Output is correct |
20 |
Correct |
15 ms |
69456 KB |
Output is correct |
21 |
Correct |
21 ms |
69456 KB |
Output is correct |
22 |
Correct |
15 ms |
69456 KB |
Output is correct |
23 |
Correct |
17 ms |
66852 KB |
Output is correct |
24 |
Correct |
14 ms |
69416 KB |
Output is correct |
25 |
Correct |
978 ms |
73288 KB |
Output is correct |
26 |
Correct |
1088 ms |
80840 KB |
Output is correct |
27 |
Correct |
1002 ms |
84416 KB |
Output is correct |
28 |
Correct |
691 ms |
87644 KB |
Output is correct |
29 |
Correct |
1500 ms |
75272 KB |
Output is correct |
30 |
Correct |
1358 ms |
76052 KB |
Output is correct |
31 |
Correct |
1577 ms |
77128 KB |
Output is correct |
32 |
Correct |
973 ms |
77908 KB |
Output is correct |
33 |
Correct |
1031 ms |
75592 KB |
Output is correct |
34 |
Correct |
858 ms |
77128 KB |
Output is correct |
35 |
Correct |
849 ms |
78428 KB |
Output is correct |
36 |
Correct |
542 ms |
79944 KB |
Output is correct |
37 |
Correct |
731 ms |
78120 KB |
Output is correct |
38 |
Correct |
661 ms |
81432 KB |
Output is correct |
39 |
Correct |
480 ms |
84552 KB |
Output is correct |
40 |
Correct |
429 ms |
87624 KB |
Output is correct |
41 |
Correct |
92 ms |
75592 KB |
Output is correct |
42 |
Correct |
97 ms |
76616 KB |
Output is correct |
43 |
Correct |
112 ms |
77200 KB |
Output is correct |
44 |
Correct |
126 ms |
78152 KB |
Output is correct |
45 |
Correct |
177 ms |
76104 KB |
Output is correct |
46 |
Correct |
177 ms |
77276 KB |
Output is correct |
47 |
Correct |
160 ms |
78632 KB |
Output is correct |
48 |
Correct |
131 ms |
78920 KB |
Output is correct |