// iee
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#define rep(i, a, b) for (auto i = (a); i <= (b); ++i)
#define per(i, a, b) for (auto i = (a); i >= (b); --i)
#define fi first
#define se second
using ll = long long;
using ull = unsigned long long;
using namespace std;
void work(int);
template <class T> void read(T &x) {
x = 0; int f = 1, ch = getchar();
while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); }
while (isdigit(ch)) x = x * 10 + (ch - '0'), ch = getchar();
x *= f;
}
int main() {
int TT = 1; // cin >> TT;
rep(CAS, 1, TT)
work(CAS);
return 0;
}
const int N = 3e5 + 5, M = 4e6 + 5;
int n, dfn_l[N], dfn_r[N], trie[M][3], tot = 1, idx, val[N];
int q;
int qry[N][3];
set<int> s[M];
vector<pair<int, int>> e[N];
void dfs(int u, int pr) {
dfn_l[u] = ++idx;
for (auto ver: e[u]) {
int v, w;
tie(v, w) = ver;
if (v == pr) continue;
val[v] = (val[u] ^ w);
dfs(v, u);
}
dfn_r[u] = idx;
}
void insert(int x, int dfn) {
int cur = 1;
s[cur].emplace(dfn);
per(i, 30, 0) {
int &to = trie[cur][x >> i & 1];
if (!to) to = ++tot;
cur = to;
s[cur].emplace(dfn);
}
}
int query(int x, int l, int r) {
int cur = 1, ans = 0;
per(i, 30, 0) {
int to = trie[cur][x >> i & 1 ^ 1];
bool ok = !!to;
auto it = s[to].lower_bound(l);
ok &= it != s[to].end() && (*it) <= r;
if (ok) cur = to, ans += (1 << i);
else cur = trie[cur][x >> i & 1];
}
return ans;
}
void work(int CASE) {
scanf("%d", &q);
++n;
rep(i, 1, q) {
char op[10];
scanf("%s%d%d", op, &qry[i][1], &qry[i][2]);
if (op[0] == 'A') {
qry[i][0] = 1;
++n;
e[qry[i][1]].emplace_back(n, qry[i][2]);
e[n].emplace_back(qry[i][1], qry[i][2]);
qry[i][1] = n;
} else {
qry[i][0] = 0;
}
}
dfs(1, 0);
insert(0, 1);
rep(i, 1, q) {
if (qry[i][0]) {
insert(val[qry[i][1]], dfn_l[qry[i][1]]);
} else {
printf("%d\n", query(val[qry[i][1]], dfn_l[qry[i][2]], dfn_r[qry[i][2]]));
}
}
}
Compilation message
klasika.cpp: In function 'int query(int, int, int)':
klasika.cpp:62:31: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
62 | int to = trie[cur][x >> i & 1 ^ 1];
| ~~~~~~~^~~
klasika.cpp: In function 'void work(int)':
klasika.cpp:72:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
klasika.cpp:76:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | scanf("%s%d%d", op, &qry[i][1], &qry[i][2]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
195308 KB |
Output is correct |
2 |
Correct |
90 ms |
195436 KB |
Output is correct |
3 |
Correct |
85 ms |
195460 KB |
Output is correct |
4 |
Correct |
85 ms |
195460 KB |
Output is correct |
5 |
Correct |
90 ms |
195276 KB |
Output is correct |
6 |
Correct |
86 ms |
195368 KB |
Output is correct |
7 |
Correct |
87 ms |
195372 KB |
Output is correct |
8 |
Correct |
103 ms |
195428 KB |
Output is correct |
9 |
Correct |
98 ms |
195260 KB |
Output is correct |
10 |
Correct |
87 ms |
195404 KB |
Output is correct |
11 |
Correct |
87 ms |
195352 KB |
Output is correct |
12 |
Correct |
88 ms |
195600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
195308 KB |
Output is correct |
2 |
Correct |
90 ms |
195436 KB |
Output is correct |
3 |
Correct |
85 ms |
195460 KB |
Output is correct |
4 |
Correct |
85 ms |
195460 KB |
Output is correct |
5 |
Correct |
90 ms |
195276 KB |
Output is correct |
6 |
Correct |
86 ms |
195368 KB |
Output is correct |
7 |
Correct |
87 ms |
195372 KB |
Output is correct |
8 |
Correct |
103 ms |
195428 KB |
Output is correct |
9 |
Correct |
98 ms |
195260 KB |
Output is correct |
10 |
Correct |
87 ms |
195404 KB |
Output is correct |
11 |
Correct |
87 ms |
195352 KB |
Output is correct |
12 |
Correct |
88 ms |
195600 KB |
Output is correct |
13 |
Correct |
91 ms |
195980 KB |
Output is correct |
14 |
Correct |
103 ms |
196768 KB |
Output is correct |
15 |
Correct |
95 ms |
197592 KB |
Output is correct |
16 |
Correct |
93 ms |
198172 KB |
Output is correct |
17 |
Correct |
91 ms |
195996 KB |
Output is correct |
18 |
Correct |
90 ms |
196644 KB |
Output is correct |
19 |
Correct |
96 ms |
197452 KB |
Output is correct |
20 |
Correct |
105 ms |
198152 KB |
Output is correct |
21 |
Correct |
89 ms |
195976 KB |
Output is correct |
22 |
Correct |
93 ms |
196668 KB |
Output is correct |
23 |
Correct |
93 ms |
197404 KB |
Output is correct |
24 |
Correct |
105 ms |
198156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
720 ms |
271168 KB |
Output is correct |
2 |
Correct |
1232 ms |
342112 KB |
Output is correct |
3 |
Correct |
1718 ms |
411132 KB |
Output is correct |
4 |
Correct |
2391 ms |
481064 KB |
Output is correct |
5 |
Correct |
762 ms |
268864 KB |
Output is correct |
6 |
Correct |
1345 ms |
337536 KB |
Output is correct |
7 |
Correct |
1959 ms |
404704 KB |
Output is correct |
8 |
Correct |
2533 ms |
472720 KB |
Output is correct |
9 |
Correct |
748 ms |
268660 KB |
Output is correct |
10 |
Correct |
1251 ms |
338336 KB |
Output is correct |
11 |
Correct |
1720 ms |
406752 KB |
Output is correct |
12 |
Correct |
2331 ms |
474164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
195308 KB |
Output is correct |
2 |
Correct |
90 ms |
195436 KB |
Output is correct |
3 |
Correct |
85 ms |
195460 KB |
Output is correct |
4 |
Correct |
85 ms |
195460 KB |
Output is correct |
5 |
Correct |
90 ms |
195276 KB |
Output is correct |
6 |
Correct |
86 ms |
195368 KB |
Output is correct |
7 |
Correct |
87 ms |
195372 KB |
Output is correct |
8 |
Correct |
103 ms |
195428 KB |
Output is correct |
9 |
Correct |
98 ms |
195260 KB |
Output is correct |
10 |
Correct |
87 ms |
195404 KB |
Output is correct |
11 |
Correct |
87 ms |
195352 KB |
Output is correct |
12 |
Correct |
88 ms |
195600 KB |
Output is correct |
13 |
Correct |
91 ms |
195980 KB |
Output is correct |
14 |
Correct |
103 ms |
196768 KB |
Output is correct |
15 |
Correct |
95 ms |
197592 KB |
Output is correct |
16 |
Correct |
93 ms |
198172 KB |
Output is correct |
17 |
Correct |
91 ms |
195996 KB |
Output is correct |
18 |
Correct |
90 ms |
196644 KB |
Output is correct |
19 |
Correct |
96 ms |
197452 KB |
Output is correct |
20 |
Correct |
105 ms |
198152 KB |
Output is correct |
21 |
Correct |
89 ms |
195976 KB |
Output is correct |
22 |
Correct |
93 ms |
196668 KB |
Output is correct |
23 |
Correct |
93 ms |
197404 KB |
Output is correct |
24 |
Correct |
105 ms |
198156 KB |
Output is correct |
25 |
Correct |
720 ms |
271168 KB |
Output is correct |
26 |
Correct |
1232 ms |
342112 KB |
Output is correct |
27 |
Correct |
1718 ms |
411132 KB |
Output is correct |
28 |
Correct |
2391 ms |
481064 KB |
Output is correct |
29 |
Correct |
762 ms |
268864 KB |
Output is correct |
30 |
Correct |
1345 ms |
337536 KB |
Output is correct |
31 |
Correct |
1959 ms |
404704 KB |
Output is correct |
32 |
Correct |
2533 ms |
472720 KB |
Output is correct |
33 |
Correct |
748 ms |
268660 KB |
Output is correct |
34 |
Correct |
1251 ms |
338336 KB |
Output is correct |
35 |
Correct |
1720 ms |
406752 KB |
Output is correct |
36 |
Correct |
2331 ms |
474164 KB |
Output is correct |
37 |
Correct |
1173 ms |
271312 KB |
Output is correct |
38 |
Correct |
1779 ms |
341596 KB |
Output is correct |
39 |
Correct |
2193 ms |
412504 KB |
Output is correct |
40 |
Correct |
2538 ms |
481396 KB |
Output is correct |
41 |
Correct |
1338 ms |
268864 KB |
Output is correct |
42 |
Correct |
1891 ms |
337416 KB |
Output is correct |
43 |
Correct |
2293 ms |
404380 KB |
Output is correct |
44 |
Correct |
2695 ms |
471808 KB |
Output is correct |
45 |
Correct |
1351 ms |
268300 KB |
Output is correct |
46 |
Correct |
1935 ms |
337824 KB |
Output is correct |
47 |
Correct |
2255 ms |
405260 KB |
Output is correct |
48 |
Correct |
2574 ms |
473888 KB |
Output is correct |