#include <bits/stdc++.h>
using namespace std;
struct graph {
int n, timer;
vector<vector<pair<int, int>>> adj;
vector<int> dist, tin, tout;
graph(int n_) : n(n_), timer(0), adj(n), dist(n), tin(n), tout(n) {}
void add_edge(int parent, int node, int weight) {
adj[parent].push_back({node, weight});
}
void dfs(int u = 0) {
tin[u] = timer++;
for (auto [v, w] : adj[u]) {
dist[v] = dist[u] ^ w;
dfs(v);
}
tout[u] = timer++;
}
};
struct trie_node {
array<unique_ptr<trie_node>, 2> children;
set<int> s;
void insert(int x, int tin, int pos = 30) {
if (pos < 0) return;
int bit = (x >> pos) & 1;
if (children[bit] == nullptr) {
children[bit] = make_unique<trie_node>();
}
children[bit]->s.insert(tin);
children[bit]->insert(x, tin, pos-1);
}
bool check(int bit, int tin, int tout) {
if (children[bit] == nullptr) return false;
const auto &cs = children[bit]->s;
auto lower_it = cs.lower_bound(tin);
auto upper_it = cs.upper_bound(tout);
return lower_it != upper_it;
}
int max_xor(int x, int tin, int tout, int pos = 30) {
if (pos < 0) return 0;
int ret = 0;
int bit = (x >> pos) & 1;
if (check(bit^1, tin, tout)) {
ret = children[bit^1]->max_xor(x, tin, tout, pos-1);
ret |= (1 << pos);
} else {
ret = children[bit]->max_xor(x, tin, tout, pos-1);
}
return ret;
}
};
int main() {
cin.tie(0)->sync_with_stdio(0);
int Q;
cin >> Q;
vector<tuple<int, int, int>> qs(Q);
int cnt = 1;
for (auto &[t, a, b] : qs) {
string s;
cin >> s >> a >> b;
if (s == "Add") {
t = 1;
++cnt;
--a;
} else {
t = 2;
--a;
--b;
}
}
graph g(cnt);
int id = 0;
for (auto &[t, a, b] : qs) {
if (t == 1) {
++id;
g.add_edge(a, id, b);
b = id;
}
}
g.dfs();
trie_node trie;
trie.insert(0, g.tin[0]);
for (auto [t, a, b] : qs) {
if (t == 1) {
trie.insert(g.dist[b], g.tin[b]);
} else {
auto ans = trie.max_xor(g.dist[a], g.tin[b], g.tout[b]);
cout << ans << '\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
520 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
700 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
2 ms |
820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
520 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
700 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
2 ms |
820 KB |
Output is correct |
13 |
Correct |
3 ms |
1748 KB |
Output is correct |
14 |
Correct |
6 ms |
3028 KB |
Output is correct |
15 |
Correct |
9 ms |
4304 KB |
Output is correct |
16 |
Correct |
11 ms |
5460 KB |
Output is correct |
17 |
Correct |
6 ms |
1652 KB |
Output is correct |
18 |
Correct |
6 ms |
2900 KB |
Output is correct |
19 |
Correct |
12 ms |
4088 KB |
Output is correct |
20 |
Correct |
15 ms |
5300 KB |
Output is correct |
21 |
Correct |
6 ms |
1620 KB |
Output is correct |
22 |
Correct |
8 ms |
2900 KB |
Output is correct |
23 |
Correct |
8 ms |
4172 KB |
Output is correct |
24 |
Correct |
10 ms |
5280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
855 ms |
115604 KB |
Output is correct |
2 |
Correct |
1361 ms |
221536 KB |
Output is correct |
3 |
Correct |
1791 ms |
322772 KB |
Output is correct |
4 |
Correct |
2336 ms |
424716 KB |
Output is correct |
5 |
Correct |
791 ms |
114080 KB |
Output is correct |
6 |
Correct |
1300 ms |
218220 KB |
Output is correct |
7 |
Correct |
1879 ms |
318028 KB |
Output is correct |
8 |
Correct |
2408 ms |
417832 KB |
Output is correct |
9 |
Correct |
770 ms |
113644 KB |
Output is correct |
10 |
Correct |
1338 ms |
219044 KB |
Output is correct |
11 |
Correct |
1947 ms |
320180 KB |
Output is correct |
12 |
Correct |
2406 ms |
419664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
520 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
700 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
2 ms |
820 KB |
Output is correct |
13 |
Correct |
3 ms |
1748 KB |
Output is correct |
14 |
Correct |
6 ms |
3028 KB |
Output is correct |
15 |
Correct |
9 ms |
4304 KB |
Output is correct |
16 |
Correct |
11 ms |
5460 KB |
Output is correct |
17 |
Correct |
6 ms |
1652 KB |
Output is correct |
18 |
Correct |
6 ms |
2900 KB |
Output is correct |
19 |
Correct |
12 ms |
4088 KB |
Output is correct |
20 |
Correct |
15 ms |
5300 KB |
Output is correct |
21 |
Correct |
6 ms |
1620 KB |
Output is correct |
22 |
Correct |
8 ms |
2900 KB |
Output is correct |
23 |
Correct |
8 ms |
4172 KB |
Output is correct |
24 |
Correct |
10 ms |
5280 KB |
Output is correct |
25 |
Correct |
855 ms |
115604 KB |
Output is correct |
26 |
Correct |
1361 ms |
221536 KB |
Output is correct |
27 |
Correct |
1791 ms |
322772 KB |
Output is correct |
28 |
Correct |
2336 ms |
424716 KB |
Output is correct |
29 |
Correct |
791 ms |
114080 KB |
Output is correct |
30 |
Correct |
1300 ms |
218220 KB |
Output is correct |
31 |
Correct |
1879 ms |
318028 KB |
Output is correct |
32 |
Correct |
2408 ms |
417832 KB |
Output is correct |
33 |
Correct |
770 ms |
113644 KB |
Output is correct |
34 |
Correct |
1338 ms |
219044 KB |
Output is correct |
35 |
Correct |
1947 ms |
320180 KB |
Output is correct |
36 |
Correct |
2406 ms |
419664 KB |
Output is correct |
37 |
Correct |
1321 ms |
119500 KB |
Output is correct |
38 |
Correct |
1914 ms |
224604 KB |
Output is correct |
39 |
Correct |
2304 ms |
328892 KB |
Output is correct |
40 |
Correct |
2789 ms |
428992 KB |
Output is correct |
41 |
Correct |
1390 ms |
117440 KB |
Output is correct |
42 |
Correct |
1919 ms |
221276 KB |
Output is correct |
43 |
Correct |
2306 ms |
321596 KB |
Output is correct |
44 |
Correct |
2554 ms |
420804 KB |
Output is correct |
45 |
Correct |
1440 ms |
116928 KB |
Output is correct |
46 |
Correct |
2112 ms |
222352 KB |
Output is correct |
47 |
Correct |
2344 ms |
322384 KB |
Output is correct |
48 |
Correct |
2618 ms |
423428 KB |
Output is correct |