#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 998244353;
const ll LOGN = 20;
const ll MAXN = 2e5 + 5;
vector<pair<int,pair<int,int>>> queries;
vector<vector<int>> graph;
vector<int> val;
int tin[MAXN], tout[MAXN];
struct Node {
Node *to[2];
set<int> st;
Node () {
st = set<int>();
}
};
int T = 0;
void dfs(int node, int parent) {
tin[node] = ++T;
for (auto u : graph[node])
dfs(u, node);
tout[node] = T;
}
bool check(Node *node, int subtree) {
int st = tin[subtree];
int en = tout[subtree];
auto u = node->st.lower_bound(st);
if (u == node->st.end() || *u > en)
return false;
return true;
}
int solve(Node *node, int subtree, int val) {
int ans = 0;
for (int i = 30; i >= 0; i--) {
bool bit = ((1<<i) & val) != 0;
if (node->to[!bit] != NULL && check(node->to[!bit], subtree)) {
ans += (1<<i);
node = node->to[!bit];
} else if (node->to[bit] != NULL && check(node->to[bit], subtree))
node = node->to[bit];
else
return 0;
}
return ans;
}
int main() {
fast
int Q;
cin >> Q;
queries = vector<pair<int,pair<int,int>>>(Q);
graph = vector<vector<int>>(Q+5, vector<int>());
val = vector<int>(Q+5, 0);
int nodes = 1, a, b;
string type;
for (int i = 0; i < Q; i++) {
cin >> type >> a >> b;
if (type == "Add") {
nodes++;
val[nodes] = (val[a] ^ b);
queries[i] = make_pair(1, make_pair(nodes, val[nodes]));
graph[a].push_back(nodes);
} else
queries[i] = make_pair(0, make_pair(a, b));
}
dfs(1, 1);
Node *root = new Node();
Node *now2 = root;
now2->st.insert(1);
for (int i = 30; i >= 0; i--) {
bool bit = 0;
if (now2->to[bit] == NULL) {
now2->to[bit] = new Node();
}
now2 = now2->to[bit];
now2->st.insert(1);
}
for (auto u : queries) {
if (u.f) {
Node *now = root;
now->st.insert(tin[u.s.f]);
int x = u.s.s;
for (int i = 30; i >= 0; i--) {
bool bit = ((1<<i) & x) != 0;
if (now->to[bit] == NULL) {
now->to[bit] = new Node();
}
now = now->to[bit];
now->st.insert(tin[u.s.f]);
}
} else {
int node = u.s.f;
int subtree = u.s.s;
Node *now = root;
cout << solve(now, subtree, val[node]) << "\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
856 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
856 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
856 KB |
Output is correct |
12 |
Correct |
1 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
856 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
856 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
856 KB |
Output is correct |
12 |
Correct |
1 ms |
856 KB |
Output is correct |
13 |
Correct |
3 ms |
1880 KB |
Output is correct |
14 |
Correct |
4 ms |
3164 KB |
Output is correct |
15 |
Correct |
5 ms |
4444 KB |
Output is correct |
16 |
Correct |
6 ms |
5468 KB |
Output is correct |
17 |
Correct |
3 ms |
1624 KB |
Output is correct |
18 |
Correct |
4 ms |
3148 KB |
Output is correct |
19 |
Correct |
5 ms |
4188 KB |
Output is correct |
20 |
Correct |
6 ms |
5464 KB |
Output is correct |
21 |
Correct |
3 ms |
1880 KB |
Output is correct |
22 |
Correct |
4 ms |
3160 KB |
Output is correct |
23 |
Correct |
5 ms |
4188 KB |
Output is correct |
24 |
Correct |
6 ms |
5468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
455 ms |
122408 KB |
Output is correct |
2 |
Correct |
752 ms |
229460 KB |
Output is correct |
3 |
Correct |
1116 ms |
332020 KB |
Output is correct |
4 |
Correct |
1442 ms |
435212 KB |
Output is correct |
5 |
Correct |
468 ms |
120400 KB |
Output is correct |
6 |
Correct |
888 ms |
225348 KB |
Output is correct |
7 |
Correct |
1321 ms |
325620 KB |
Output is correct |
8 |
Correct |
1790 ms |
426164 KB |
Output is correct |
9 |
Correct |
453 ms |
120044 KB |
Output is correct |
10 |
Correct |
799 ms |
226276 KB |
Output is correct |
11 |
Correct |
1124 ms |
328184 KB |
Output is correct |
12 |
Correct |
1498 ms |
428332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
856 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
856 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
856 KB |
Output is correct |
12 |
Correct |
1 ms |
856 KB |
Output is correct |
13 |
Correct |
3 ms |
1880 KB |
Output is correct |
14 |
Correct |
4 ms |
3164 KB |
Output is correct |
15 |
Correct |
5 ms |
4444 KB |
Output is correct |
16 |
Correct |
6 ms |
5468 KB |
Output is correct |
17 |
Correct |
3 ms |
1624 KB |
Output is correct |
18 |
Correct |
4 ms |
3148 KB |
Output is correct |
19 |
Correct |
5 ms |
4188 KB |
Output is correct |
20 |
Correct |
6 ms |
5464 KB |
Output is correct |
21 |
Correct |
3 ms |
1880 KB |
Output is correct |
22 |
Correct |
4 ms |
3160 KB |
Output is correct |
23 |
Correct |
5 ms |
4188 KB |
Output is correct |
24 |
Correct |
6 ms |
5468 KB |
Output is correct |
25 |
Correct |
455 ms |
122408 KB |
Output is correct |
26 |
Correct |
752 ms |
229460 KB |
Output is correct |
27 |
Correct |
1116 ms |
332020 KB |
Output is correct |
28 |
Correct |
1442 ms |
435212 KB |
Output is correct |
29 |
Correct |
468 ms |
120400 KB |
Output is correct |
30 |
Correct |
888 ms |
225348 KB |
Output is correct |
31 |
Correct |
1321 ms |
325620 KB |
Output is correct |
32 |
Correct |
1790 ms |
426164 KB |
Output is correct |
33 |
Correct |
453 ms |
120044 KB |
Output is correct |
34 |
Correct |
799 ms |
226276 KB |
Output is correct |
35 |
Correct |
1124 ms |
328184 KB |
Output is correct |
36 |
Correct |
1498 ms |
428332 KB |
Output is correct |
37 |
Correct |
860 ms |
122948 KB |
Output is correct |
38 |
Correct |
1295 ms |
229276 KB |
Output is correct |
39 |
Correct |
1525 ms |
334464 KB |
Output is correct |
40 |
Correct |
1707 ms |
435540 KB |
Output is correct |
41 |
Correct |
970 ms |
120400 KB |
Output is correct |
42 |
Correct |
1484 ms |
224516 KB |
Output is correct |
43 |
Correct |
1807 ms |
325376 KB |
Output is correct |
44 |
Correct |
2012 ms |
425304 KB |
Output is correct |
45 |
Correct |
1034 ms |
119900 KB |
Output is correct |
46 |
Correct |
1528 ms |
225832 KB |
Output is correct |
47 |
Correct |
1869 ms |
326648 KB |
Output is correct |
48 |
Correct |
1946 ms |
428280 KB |
Output is correct |