#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;
struct Query {
bool add;
int a, b;
} queries[200001];
vector<int> graph[200001];
int to_root[200001], tin[200001], tout[200001], timer = 0;
void dfs(int node = 1) {
tin[node] = ++timer;
for (int i : graph[node]) dfs(i);
tout[node] = timer;
}
struct TrieNode {
TrieNode *children[2];
set<int> ids;
TrieNode() {
children[0] = nullptr;
children[1] = nullptr;
}
};
void insert(TrieNode *root, int val, int node) {
TrieNode *pCrawl = root;
for (int i = 30; ~i; i--) {
int idx = ((val & (1 << i)) != 0);
if (!pCrawl->children[idx]) pCrawl->children[idx] = new TrieNode();
pCrawl = pCrawl->children[idx];
pCrawl->ids.insert(tin[node]);
}
}
int query(TrieNode *root, int val, int subtree) {
int ans = 0;
TrieNode *pCrawl = root;
for (int i = 30; ~i; i--) {
int idx = ((val & (1 << i)) == 0);
if (pCrawl->children[idx]) {
auto lb = pCrawl->children[idx]->ids.lower_bound(tin[subtree]);
if (lb != pCrawl->children[idx]->ids.end() && *lb <= tout[subtree]) {
ans |= (1 << i);
pCrawl = pCrawl->children[idx];
} else pCrawl = pCrawl->children[1 - idx];
} else pCrawl = pCrawl->children[1 - idx];
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int q;
cin >> q;
int n = 1;
FOR(i, 0, q) {
string type;
int a, b;
cin >> type >> a >> b;
queries[i] = {(type == "Add"), a, b};
if (type == "Add") graph[a].push_back(++n);
}
dfs();
TrieNode *root = new TrieNode();
n = 1;
insert(root, 0, 1);
FOR(i, 0, q) {
if (queries[i].add) {
to_root[++n] = to_root[queries[i].a] ^ queries[i].b;
insert(root, to_root[n], n);
} else {
cout << query(root, to_root[queries[i].a], queries[i].b) << endl;
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5244 KB |
Output is correct |
2 |
Correct |
8 ms |
5368 KB |
Output is correct |
3 |
Correct |
9 ms |
5496 KB |
Output is correct |
4 |
Correct |
8 ms |
5500 KB |
Output is correct |
5 |
Correct |
8 ms |
5240 KB |
Output is correct |
6 |
Correct |
9 ms |
5496 KB |
Output is correct |
7 |
Correct |
9 ms |
5496 KB |
Output is correct |
8 |
Correct |
9 ms |
5628 KB |
Output is correct |
9 |
Correct |
8 ms |
5244 KB |
Output is correct |
10 |
Correct |
8 ms |
5368 KB |
Output is correct |
11 |
Correct |
10 ms |
5496 KB |
Output is correct |
12 |
Correct |
9 ms |
5624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5244 KB |
Output is correct |
2 |
Correct |
8 ms |
5368 KB |
Output is correct |
3 |
Correct |
9 ms |
5496 KB |
Output is correct |
4 |
Correct |
8 ms |
5500 KB |
Output is correct |
5 |
Correct |
8 ms |
5240 KB |
Output is correct |
6 |
Correct |
9 ms |
5496 KB |
Output is correct |
7 |
Correct |
9 ms |
5496 KB |
Output is correct |
8 |
Correct |
9 ms |
5628 KB |
Output is correct |
9 |
Correct |
8 ms |
5244 KB |
Output is correct |
10 |
Correct |
8 ms |
5368 KB |
Output is correct |
11 |
Correct |
10 ms |
5496 KB |
Output is correct |
12 |
Correct |
9 ms |
5624 KB |
Output is correct |
13 |
Correct |
15 ms |
6520 KB |
Output is correct |
14 |
Correct |
15 ms |
7800 KB |
Output is correct |
15 |
Correct |
16 ms |
8952 KB |
Output is correct |
16 |
Correct |
16 ms |
10104 KB |
Output is correct |
17 |
Correct |
14 ms |
6392 KB |
Output is correct |
18 |
Correct |
15 ms |
7672 KB |
Output is correct |
19 |
Correct |
16 ms |
8952 KB |
Output is correct |
20 |
Correct |
17 ms |
9976 KB |
Output is correct |
21 |
Correct |
14 ms |
6392 KB |
Output is correct |
22 |
Correct |
17 ms |
7672 KB |
Output is correct |
23 |
Correct |
16 ms |
8956 KB |
Output is correct |
24 |
Correct |
16 ms |
9976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1142 ms |
122360 KB |
Output is correct |
2 |
Correct |
1782 ms |
227356 KB |
Output is correct |
3 |
Correct |
2446 ms |
328064 KB |
Output is correct |
4 |
Correct |
3065 ms |
429060 KB |
Output is correct |
5 |
Correct |
1221 ms |
120824 KB |
Output is correct |
6 |
Correct |
1850 ms |
223992 KB |
Output is correct |
7 |
Correct |
2369 ms |
323192 KB |
Output is correct |
8 |
Correct |
2963 ms |
422276 KB |
Output is correct |
9 |
Correct |
1173 ms |
120208 KB |
Output is correct |
10 |
Correct |
1747 ms |
225016 KB |
Output is correct |
11 |
Correct |
2321 ms |
325368 KB |
Output is correct |
12 |
Correct |
2878 ms |
424212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5244 KB |
Output is correct |
2 |
Correct |
8 ms |
5368 KB |
Output is correct |
3 |
Correct |
9 ms |
5496 KB |
Output is correct |
4 |
Correct |
8 ms |
5500 KB |
Output is correct |
5 |
Correct |
8 ms |
5240 KB |
Output is correct |
6 |
Correct |
9 ms |
5496 KB |
Output is correct |
7 |
Correct |
9 ms |
5496 KB |
Output is correct |
8 |
Correct |
9 ms |
5628 KB |
Output is correct |
9 |
Correct |
8 ms |
5244 KB |
Output is correct |
10 |
Correct |
8 ms |
5368 KB |
Output is correct |
11 |
Correct |
10 ms |
5496 KB |
Output is correct |
12 |
Correct |
9 ms |
5624 KB |
Output is correct |
13 |
Correct |
15 ms |
6520 KB |
Output is correct |
14 |
Correct |
15 ms |
7800 KB |
Output is correct |
15 |
Correct |
16 ms |
8952 KB |
Output is correct |
16 |
Correct |
16 ms |
10104 KB |
Output is correct |
17 |
Correct |
14 ms |
6392 KB |
Output is correct |
18 |
Correct |
15 ms |
7672 KB |
Output is correct |
19 |
Correct |
16 ms |
8952 KB |
Output is correct |
20 |
Correct |
17 ms |
9976 KB |
Output is correct |
21 |
Correct |
14 ms |
6392 KB |
Output is correct |
22 |
Correct |
17 ms |
7672 KB |
Output is correct |
23 |
Correct |
16 ms |
8956 KB |
Output is correct |
24 |
Correct |
16 ms |
9976 KB |
Output is correct |
25 |
Correct |
1142 ms |
122360 KB |
Output is correct |
26 |
Correct |
1782 ms |
227356 KB |
Output is correct |
27 |
Correct |
2446 ms |
328064 KB |
Output is correct |
28 |
Correct |
3065 ms |
429060 KB |
Output is correct |
29 |
Correct |
1221 ms |
120824 KB |
Output is correct |
30 |
Correct |
1850 ms |
223992 KB |
Output is correct |
31 |
Correct |
2369 ms |
323192 KB |
Output is correct |
32 |
Correct |
2963 ms |
422276 KB |
Output is correct |
33 |
Correct |
1173 ms |
120208 KB |
Output is correct |
34 |
Correct |
1747 ms |
225016 KB |
Output is correct |
35 |
Correct |
2321 ms |
325368 KB |
Output is correct |
36 |
Correct |
2878 ms |
424212 KB |
Output is correct |
37 |
Correct |
1911 ms |
123348 KB |
Output is correct |
38 |
Correct |
2506 ms |
227320 KB |
Output is correct |
39 |
Correct |
2989 ms |
330360 KB |
Output is correct |
40 |
Correct |
3298 ms |
429476 KB |
Output is correct |
41 |
Correct |
2020 ms |
121464 KB |
Output is correct |
42 |
Correct |
2592 ms |
224120 KB |
Output is correct |
43 |
Correct |
2906 ms |
323448 KB |
Output is correct |
44 |
Correct |
3280 ms |
421568 KB |
Output is correct |
45 |
Correct |
2226 ms |
120824 KB |
Output is correct |
46 |
Correct |
2794 ms |
224876 KB |
Output is correct |
47 |
Correct |
3059 ms |
324348 KB |
Output is correct |
48 |
Correct |
3326 ms |
424344 KB |
Output is correct |