#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200000;
const int LG = __lg(MAXN);
struct Trie {
struct Node {
Node *ch[2];
set<int> id;
Node() {
ch[0] = ch[1] = nullptr;
}
bool check(int l,int r) {
auto it = id.lower_bound(l);
return it != id.end() && *it <= r;
}
};
Node *head = new Node();
void add(int x,int id) {
Node *cur = head;
for (int i=30;i>=0;i--) {
int c = (x >> i) & 1;
if (cur->ch[c] == nullptr) cur->ch[c] = new Node();
cur = cur->ch[c];
cur->id.insert(id);
}
}
int query(int l,int r,int x) {
return query_recur(l,r,x,head,30);
}
int query_recur(int l,int r,int x,Node *cur,int cur_b) {
if (cur_b < 0) return 0;
if ((x >> cur_b) & 1) {
if (cur->ch[0] == nullptr || !cur->ch[0]->check(l,r))
return query_recur(l,r,x,cur->ch[1],cur_b-1);
else return (1 << cur_b) | query_recur(l,r,x,cur->ch[0],cur_b-1);
}
else {
if (cur->ch[1] == nullptr || !cur->ch[1]->check(l,r))
return query_recur(l,r,x,cur->ch[0],cur_b-1);
else return (1 << cur_b) | query_recur(l,r,x,cur->ch[1],cur_b-1);
}
}
} trie;
struct Query {
int type;
int x,y;
Query(){};
} query[MAXN+1];
vector<int> adj[MAXN+1];
int xor_p[MAXN+1];
int tin[MAXN+1],tout[MAXN+1];
int timeDFS = 0;
void dfs(int u) {
tin[u] = ++timeDFS;
for (int i=0;i<adj[u].size();i++) {
int v = adj[u][i];
dfs(v);
}
tout[u] = timeDFS;
}
int main() {
cin.tie(0) -> sync_with_stdio(0);
int q; cin >> q;
int cnt = 1;
for (int i=1;i<=q;i++) {
string s; cin >> s >> query[i].x >> query[i].y;
if (s == "Add") {
cnt++;
adj[query[i].x].push_back(cnt);
xor_p[cnt] = xor_p[query[i].x] ^ query[i].y;
}
else {
query[i].type = 1;
}
}
dfs(1);
cnt = 1;
trie.add(0,1);
for (int i=1;i<=q;i++) {
if (query[i].type == 0) {
cnt++;
trie.add(xor_p[cnt],tin[cnt]);
}
else {
// cerr << query[i].y << ' ' << tin[query[i].y] << ' ' << tout[query[i].y] << '\n';
cout << trie.query(tin[query[i].y],tout[query[i].y],xor_p[query[i].x]) << '\n';
}
}
return 0;
}
Compilation message
klasika.cpp: In function 'void dfs(int)':
klasika.cpp:61:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int i=0;i<adj[u].size();i++) {
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6736 KB |
Output is correct |
2 |
Correct |
3 ms |
6904 KB |
Output is correct |
3 |
Correct |
3 ms |
6880 KB |
Output is correct |
4 |
Correct |
2 ms |
6996 KB |
Output is correct |
5 |
Correct |
2 ms |
6904 KB |
Output is correct |
6 |
Correct |
2 ms |
6740 KB |
Output is correct |
7 |
Correct |
3 ms |
6996 KB |
Output is correct |
8 |
Correct |
2 ms |
6996 KB |
Output is correct |
9 |
Correct |
2 ms |
6740 KB |
Output is correct |
10 |
Correct |
2 ms |
6740 KB |
Output is correct |
11 |
Correct |
3 ms |
7160 KB |
Output is correct |
12 |
Correct |
3 ms |
6996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6736 KB |
Output is correct |
2 |
Correct |
3 ms |
6904 KB |
Output is correct |
3 |
Correct |
3 ms |
6880 KB |
Output is correct |
4 |
Correct |
2 ms |
6996 KB |
Output is correct |
5 |
Correct |
2 ms |
6904 KB |
Output is correct |
6 |
Correct |
2 ms |
6740 KB |
Output is correct |
7 |
Correct |
3 ms |
6996 KB |
Output is correct |
8 |
Correct |
2 ms |
6996 KB |
Output is correct |
9 |
Correct |
2 ms |
6740 KB |
Output is correct |
10 |
Correct |
2 ms |
6740 KB |
Output is correct |
11 |
Correct |
3 ms |
7160 KB |
Output is correct |
12 |
Correct |
3 ms |
6996 KB |
Output is correct |
13 |
Correct |
5 ms |
8020 KB |
Output is correct |
14 |
Correct |
6 ms |
9300 KB |
Output is correct |
15 |
Correct |
8 ms |
10656 KB |
Output is correct |
16 |
Correct |
9 ms |
11604 KB |
Output is correct |
17 |
Correct |
5 ms |
7764 KB |
Output is correct |
18 |
Correct |
8 ms |
9044 KB |
Output is correct |
19 |
Correct |
7 ms |
10324 KB |
Output is correct |
20 |
Correct |
13 ms |
11604 KB |
Output is correct |
21 |
Correct |
5 ms |
7920 KB |
Output is correct |
22 |
Correct |
9 ms |
9044 KB |
Output is correct |
23 |
Correct |
8 ms |
10324 KB |
Output is correct |
24 |
Correct |
11 ms |
11348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
469 ms |
119968 KB |
Output is correct |
2 |
Correct |
886 ms |
228528 KB |
Output is correct |
3 |
Correct |
1220 ms |
329804 KB |
Output is correct |
4 |
Correct |
1704 ms |
431416 KB |
Output is correct |
5 |
Correct |
503 ms |
120856 KB |
Output is correct |
6 |
Correct |
960 ms |
223980 KB |
Output is correct |
7 |
Correct |
1334 ms |
323028 KB |
Output is correct |
8 |
Correct |
1821 ms |
422248 KB |
Output is correct |
9 |
Correct |
439 ms |
120396 KB |
Output is correct |
10 |
Correct |
781 ms |
225132 KB |
Output is correct |
11 |
Correct |
1178 ms |
325708 KB |
Output is correct |
12 |
Correct |
1623 ms |
424684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6736 KB |
Output is correct |
2 |
Correct |
3 ms |
6904 KB |
Output is correct |
3 |
Correct |
3 ms |
6880 KB |
Output is correct |
4 |
Correct |
2 ms |
6996 KB |
Output is correct |
5 |
Correct |
2 ms |
6904 KB |
Output is correct |
6 |
Correct |
2 ms |
6740 KB |
Output is correct |
7 |
Correct |
3 ms |
6996 KB |
Output is correct |
8 |
Correct |
2 ms |
6996 KB |
Output is correct |
9 |
Correct |
2 ms |
6740 KB |
Output is correct |
10 |
Correct |
2 ms |
6740 KB |
Output is correct |
11 |
Correct |
3 ms |
7160 KB |
Output is correct |
12 |
Correct |
3 ms |
6996 KB |
Output is correct |
13 |
Correct |
5 ms |
8020 KB |
Output is correct |
14 |
Correct |
6 ms |
9300 KB |
Output is correct |
15 |
Correct |
8 ms |
10656 KB |
Output is correct |
16 |
Correct |
9 ms |
11604 KB |
Output is correct |
17 |
Correct |
5 ms |
7764 KB |
Output is correct |
18 |
Correct |
8 ms |
9044 KB |
Output is correct |
19 |
Correct |
7 ms |
10324 KB |
Output is correct |
20 |
Correct |
13 ms |
11604 KB |
Output is correct |
21 |
Correct |
5 ms |
7920 KB |
Output is correct |
22 |
Correct |
9 ms |
9044 KB |
Output is correct |
23 |
Correct |
8 ms |
10324 KB |
Output is correct |
24 |
Correct |
11 ms |
11348 KB |
Output is correct |
25 |
Correct |
469 ms |
119968 KB |
Output is correct |
26 |
Correct |
886 ms |
228528 KB |
Output is correct |
27 |
Correct |
1220 ms |
329804 KB |
Output is correct |
28 |
Correct |
1704 ms |
431416 KB |
Output is correct |
29 |
Correct |
503 ms |
120856 KB |
Output is correct |
30 |
Correct |
960 ms |
223980 KB |
Output is correct |
31 |
Correct |
1334 ms |
323028 KB |
Output is correct |
32 |
Correct |
1821 ms |
422248 KB |
Output is correct |
33 |
Correct |
439 ms |
120396 KB |
Output is correct |
34 |
Correct |
781 ms |
225132 KB |
Output is correct |
35 |
Correct |
1178 ms |
325708 KB |
Output is correct |
36 |
Correct |
1623 ms |
424684 KB |
Output is correct |
37 |
Correct |
838 ms |
123908 KB |
Output is correct |
38 |
Correct |
1275 ms |
228432 KB |
Output is correct |
39 |
Correct |
1612 ms |
332364 KB |
Output is correct |
40 |
Correct |
1596 ms |
431864 KB |
Output is correct |
41 |
Correct |
922 ms |
121164 KB |
Output is correct |
42 |
Correct |
1408 ms |
224124 KB |
Output is correct |
43 |
Correct |
1805 ms |
323420 KB |
Output is correct |
44 |
Correct |
2175 ms |
421520 KB |
Output is correct |
45 |
Correct |
1076 ms |
120780 KB |
Output is correct |
46 |
Correct |
1513 ms |
225100 KB |
Output is correct |
47 |
Correct |
1612 ms |
324544 KB |
Output is correct |
48 |
Correct |
1966 ms |
424612 KB |
Output is correct |