#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) {
Node *cur = head;
int ans = 0;
// cerr << l << ' ' << r << ' ' << x << '\n';
for (int i=30;i>=0;i--) {
int c = 1 - ((x >> i) & 1);
if (cur->ch[c] == nullptr) c = 1 - c;
if (cur->ch[c] != nullptr && !cur->ch[c]->check(l,r)) c = 1 - c;
if (cur->ch[c] == nullptr) return ans ^ x;
ans |= c << i;
cur = cur->ch[c];
}
return ans ^ x;
}
} 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);
trie.add(0,1);
cnt = 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:59:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for (int i=0;i<adj[u].size();i++) {
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6736 KB |
Output is correct |
2 |
Correct |
2 ms |
6736 KB |
Output is correct |
3 |
Correct |
2 ms |
6992 KB |
Output is correct |
4 |
Correct |
2 ms |
6992 KB |
Output is correct |
5 |
Correct |
2 ms |
6736 KB |
Output is correct |
6 |
Correct |
2 ms |
6736 KB |
Output is correct |
7 |
Correct |
2 ms |
6992 KB |
Output is correct |
8 |
Correct |
2 ms |
6992 KB |
Output is correct |
9 |
Correct |
2 ms |
6736 KB |
Output is correct |
10 |
Correct |
2 ms |
6736 KB |
Output is correct |
11 |
Correct |
3 ms |
6992 KB |
Output is correct |
12 |
Correct |
2 ms |
6992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6736 KB |
Output is correct |
2 |
Correct |
2 ms |
6736 KB |
Output is correct |
3 |
Correct |
2 ms |
6992 KB |
Output is correct |
4 |
Correct |
2 ms |
6992 KB |
Output is correct |
5 |
Correct |
2 ms |
6736 KB |
Output is correct |
6 |
Correct |
2 ms |
6736 KB |
Output is correct |
7 |
Correct |
2 ms |
6992 KB |
Output is correct |
8 |
Correct |
2 ms |
6992 KB |
Output is correct |
9 |
Correct |
2 ms |
6736 KB |
Output is correct |
10 |
Correct |
2 ms |
6736 KB |
Output is correct |
11 |
Correct |
3 ms |
6992 KB |
Output is correct |
12 |
Correct |
2 ms |
6992 KB |
Output is correct |
13 |
Correct |
5 ms |
8016 KB |
Output is correct |
14 |
Correct |
6 ms |
9040 KB |
Output is correct |
15 |
Correct |
7 ms |
10320 KB |
Output is correct |
16 |
Correct |
9 ms |
11600 KB |
Output is correct |
17 |
Correct |
5 ms |
7928 KB |
Output is correct |
18 |
Correct |
6 ms |
9040 KB |
Output is correct |
19 |
Correct |
7 ms |
10320 KB |
Output is correct |
20 |
Correct |
9 ms |
11512 KB |
Output is correct |
21 |
Correct |
5 ms |
7760 KB |
Output is correct |
22 |
Correct |
6 ms |
9200 KB |
Output is correct |
23 |
Correct |
8 ms |
10320 KB |
Output is correct |
24 |
Correct |
9 ms |
11344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
494 ms |
120204 KB |
Output is correct |
2 |
Correct |
880 ms |
227112 KB |
Output is correct |
3 |
Correct |
1155 ms |
326260 KB |
Output is correct |
4 |
Correct |
1608 ms |
427744 KB |
Output is correct |
5 |
Correct |
476 ms |
119728 KB |
Output is correct |
6 |
Correct |
955 ms |
222792 KB |
Output is correct |
7 |
Correct |
1173 ms |
321816 KB |
Output is correct |
8 |
Correct |
1594 ms |
420628 KB |
Output is correct |
9 |
Correct |
444 ms |
117596 KB |
Output is correct |
10 |
Correct |
890 ms |
222024 KB |
Output is correct |
11 |
Correct |
1188 ms |
322280 KB |
Output is correct |
12 |
Correct |
1596 ms |
420756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6736 KB |
Output is correct |
2 |
Correct |
2 ms |
6736 KB |
Output is correct |
3 |
Correct |
2 ms |
6992 KB |
Output is correct |
4 |
Correct |
2 ms |
6992 KB |
Output is correct |
5 |
Correct |
2 ms |
6736 KB |
Output is correct |
6 |
Correct |
2 ms |
6736 KB |
Output is correct |
7 |
Correct |
2 ms |
6992 KB |
Output is correct |
8 |
Correct |
2 ms |
6992 KB |
Output is correct |
9 |
Correct |
2 ms |
6736 KB |
Output is correct |
10 |
Correct |
2 ms |
6736 KB |
Output is correct |
11 |
Correct |
3 ms |
6992 KB |
Output is correct |
12 |
Correct |
2 ms |
6992 KB |
Output is correct |
13 |
Correct |
5 ms |
8016 KB |
Output is correct |
14 |
Correct |
6 ms |
9040 KB |
Output is correct |
15 |
Correct |
7 ms |
10320 KB |
Output is correct |
16 |
Correct |
9 ms |
11600 KB |
Output is correct |
17 |
Correct |
5 ms |
7928 KB |
Output is correct |
18 |
Correct |
6 ms |
9040 KB |
Output is correct |
19 |
Correct |
7 ms |
10320 KB |
Output is correct |
20 |
Correct |
9 ms |
11512 KB |
Output is correct |
21 |
Correct |
5 ms |
7760 KB |
Output is correct |
22 |
Correct |
6 ms |
9200 KB |
Output is correct |
23 |
Correct |
8 ms |
10320 KB |
Output is correct |
24 |
Correct |
9 ms |
11344 KB |
Output is correct |
25 |
Correct |
494 ms |
120204 KB |
Output is correct |
26 |
Correct |
880 ms |
227112 KB |
Output is correct |
27 |
Correct |
1155 ms |
326260 KB |
Output is correct |
28 |
Correct |
1608 ms |
427744 KB |
Output is correct |
29 |
Correct |
476 ms |
119728 KB |
Output is correct |
30 |
Correct |
955 ms |
222792 KB |
Output is correct |
31 |
Correct |
1173 ms |
321816 KB |
Output is correct |
32 |
Correct |
1594 ms |
420628 KB |
Output is correct |
33 |
Correct |
444 ms |
117596 KB |
Output is correct |
34 |
Correct |
890 ms |
222024 KB |
Output is correct |
35 |
Correct |
1188 ms |
322280 KB |
Output is correct |
36 |
Correct |
1596 ms |
420756 KB |
Output is correct |
37 |
Correct |
907 ms |
120700 KB |
Output is correct |
38 |
Correct |
1301 ms |
224840 KB |
Output is correct |
39 |
Correct |
1579 ms |
328520 KB |
Output is correct |
40 |
Correct |
1590 ms |
428104 KB |
Output is correct |
41 |
Correct |
890 ms |
117756 KB |
Output is correct |
42 |
Correct |
1520 ms |
220408 KB |
Output is correct |
43 |
Correct |
1807 ms |
319548 KB |
Output is correct |
44 |
Correct |
2104 ms |
417824 KB |
Output is correct |
45 |
Correct |
997 ms |
117580 KB |
Output is correct |
46 |
Correct |
1444 ms |
223716 KB |
Output is correct |
47 |
Correct |
1738 ms |
320724 KB |
Output is correct |
48 |
Correct |
1891 ms |
420776 KB |
Output is correct |