#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) cerr << #x << " " << x << endl
#define FOR(i, a, b) for (int i = (a); i < int(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define _ << " " <<
typedef long long llint;
const int MAXN = 2e5 + 10;
struct Query {
bool print;
int x, y;
Query() {}
Query(bool print, int x, int y) : print(print), x(x), y(y) {}
};
struct Node {
set<int> ids;
Node *zero, *one;
Node () {
zero = one = NULL;
}
} root;
int q, n, t;
int l[MAXN], r[MAXN], rxor[MAXN];
vector<pair<int, int>> v[MAXN];
vector<Query> Q;
void dfs(int node, int dad, int xval) {
l[node] = ++t;
rxor[node] = xval;
for (const auto &p : v[node]) {
int nxt = p.first;
int w = p.second;
if (nxt == dad) continue;
dfs(nxt, node, xval ^ w);
}
r[node] = t;
}
void trie_add(Node *node, int bit, int val, int id) {
node->ids.insert(id);
if (bit < 0) return;
if (val & (1 << bit)) {
if (node->one == NULL) node->one = new Node();
trie_add(node->one, bit - 1, val, id);
} else {
if (node->zero == NULL) node->zero = new Node();
trie_add(node->zero, bit - 1, val, id);
}
}
int trie_get(Node *node, int bit, int val, int from, int to) {
if (bit < 0) return 0;
if ((val & (1 << bit)) == 0) { // want 1
if (node->one == NULL ||
node->one->ids.lower_bound(from) == node->one->ids.upper_bound(to))
return trie_get(node->zero, bit - 1, val, from, to);
else
return (1 << bit) + trie_get(node->one, bit - 1, val, from, to);
} else { // want 0
if (node->zero == NULL ||
node->zero->ids.lower_bound(from) == node->zero->ids.upper_bound(to))
return trie_get(node->one, bit - 1, val, from, to);
else
return (1 << bit) + trie_get(node->zero, bit - 1, val, from, to);
}
}
int main(void) {
int n = 1;
scanf("%d", &q);
for (int i = 0; i < q; ++i) {
char s[10];
scanf("%s", s);
if (s[0] == 'A') {
int x, y;
scanf("%d%d", &x, &y); --x;
Q.emplace_back(false, n, y);
v[x].emplace_back(n, y);
v[n].emplace_back(x, y);
++n;
} else {
int a, b;
scanf("%d%d", &a, &b); --a; --b;
Q.emplace_back(true, a, b);
}
}
dfs(0, -1, 0);
trie_add(&root, 30, 0, l[0]);
int i = 1;
for (const auto &qq : Q) {
if (!qq.print)
trie_add(&root, 30, rxor[qq.x], l[qq.x]);
else
printf("%d\n", trie_get(&root, 30, rxor[qq.x], l[qq.y], r[qq.y]));
++i;
}
return 0;
}
Compilation message
klasika.cpp: In function 'int main()':
klasika.cpp:78:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
klasika.cpp:81:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | scanf("%s", s);
| ~~~~~^~~~~~~~~
klasika.cpp:84:12: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | scanf("%d%d", &x, &y); --x;
| ~~~~~^~~~~~~~~~~~~~~~
klasika.cpp:91:12: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | scanf("%d%d", &a, &b); --a; --b;
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5196 KB |
Output is correct |
2 |
Correct |
3 ms |
5196 KB |
Output is correct |
3 |
Correct |
3 ms |
5324 KB |
Output is correct |
4 |
Correct |
3 ms |
5452 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
3 ms |
5196 KB |
Output is correct |
7 |
Correct |
3 ms |
5324 KB |
Output is correct |
8 |
Correct |
3 ms |
5452 KB |
Output is correct |
9 |
Correct |
3 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
5272 KB |
Output is correct |
11 |
Correct |
3 ms |
5452 KB |
Output is correct |
12 |
Correct |
3 ms |
5452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5196 KB |
Output is correct |
2 |
Correct |
3 ms |
5196 KB |
Output is correct |
3 |
Correct |
3 ms |
5324 KB |
Output is correct |
4 |
Correct |
3 ms |
5452 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
3 ms |
5196 KB |
Output is correct |
7 |
Correct |
3 ms |
5324 KB |
Output is correct |
8 |
Correct |
3 ms |
5452 KB |
Output is correct |
9 |
Correct |
3 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
5272 KB |
Output is correct |
11 |
Correct |
3 ms |
5452 KB |
Output is correct |
12 |
Correct |
3 ms |
5452 KB |
Output is correct |
13 |
Correct |
5 ms |
6348 KB |
Output is correct |
14 |
Correct |
7 ms |
7628 KB |
Output is correct |
15 |
Correct |
8 ms |
9036 KB |
Output is correct |
16 |
Correct |
10 ms |
10188 KB |
Output is correct |
17 |
Correct |
6 ms |
6348 KB |
Output is correct |
18 |
Correct |
7 ms |
7592 KB |
Output is correct |
19 |
Correct |
11 ms |
8908 KB |
Output is correct |
20 |
Correct |
10 ms |
10060 KB |
Output is correct |
21 |
Correct |
5 ms |
6348 KB |
Output is correct |
22 |
Correct |
7 ms |
7628 KB |
Output is correct |
23 |
Correct |
8 ms |
8904 KB |
Output is correct |
24 |
Correct |
10 ms |
10028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
805 ms |
122456 KB |
Output is correct |
2 |
Correct |
1284 ms |
230240 KB |
Output is correct |
3 |
Correct |
1664 ms |
333792 KB |
Output is correct |
4 |
Correct |
2125 ms |
437600 KB |
Output is correct |
5 |
Correct |
751 ms |
120424 KB |
Output is correct |
6 |
Correct |
1239 ms |
226232 KB |
Output is correct |
7 |
Correct |
1735 ms |
327712 KB |
Output is correct |
8 |
Correct |
2269 ms |
429320 KB |
Output is correct |
9 |
Correct |
730 ms |
119964 KB |
Output is correct |
10 |
Correct |
1189 ms |
226956 KB |
Output is correct |
11 |
Correct |
1642 ms |
330064 KB |
Output is correct |
12 |
Correct |
2541 ms |
431060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5196 KB |
Output is correct |
2 |
Correct |
3 ms |
5196 KB |
Output is correct |
3 |
Correct |
3 ms |
5324 KB |
Output is correct |
4 |
Correct |
3 ms |
5452 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
3 ms |
5196 KB |
Output is correct |
7 |
Correct |
3 ms |
5324 KB |
Output is correct |
8 |
Correct |
3 ms |
5452 KB |
Output is correct |
9 |
Correct |
3 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
5272 KB |
Output is correct |
11 |
Correct |
3 ms |
5452 KB |
Output is correct |
12 |
Correct |
3 ms |
5452 KB |
Output is correct |
13 |
Correct |
5 ms |
6348 KB |
Output is correct |
14 |
Correct |
7 ms |
7628 KB |
Output is correct |
15 |
Correct |
8 ms |
9036 KB |
Output is correct |
16 |
Correct |
10 ms |
10188 KB |
Output is correct |
17 |
Correct |
6 ms |
6348 KB |
Output is correct |
18 |
Correct |
7 ms |
7592 KB |
Output is correct |
19 |
Correct |
11 ms |
8908 KB |
Output is correct |
20 |
Correct |
10 ms |
10060 KB |
Output is correct |
21 |
Correct |
5 ms |
6348 KB |
Output is correct |
22 |
Correct |
7 ms |
7628 KB |
Output is correct |
23 |
Correct |
8 ms |
8904 KB |
Output is correct |
24 |
Correct |
10 ms |
10028 KB |
Output is correct |
25 |
Correct |
805 ms |
122456 KB |
Output is correct |
26 |
Correct |
1284 ms |
230240 KB |
Output is correct |
27 |
Correct |
1664 ms |
333792 KB |
Output is correct |
28 |
Correct |
2125 ms |
437600 KB |
Output is correct |
29 |
Correct |
751 ms |
120424 KB |
Output is correct |
30 |
Correct |
1239 ms |
226232 KB |
Output is correct |
31 |
Correct |
1735 ms |
327712 KB |
Output is correct |
32 |
Correct |
2269 ms |
429320 KB |
Output is correct |
33 |
Correct |
730 ms |
119964 KB |
Output is correct |
34 |
Correct |
1189 ms |
226956 KB |
Output is correct |
35 |
Correct |
1642 ms |
330064 KB |
Output is correct |
36 |
Correct |
2541 ms |
431060 KB |
Output is correct |
37 |
Correct |
1463 ms |
122868 KB |
Output is correct |
38 |
Correct |
1826 ms |
229892 KB |
Output is correct |
39 |
Correct |
2179 ms |
335964 KB |
Output is correct |
40 |
Correct |
2346 ms |
437844 KB |
Output is correct |
41 |
Correct |
1461 ms |
120372 KB |
Output is correct |
42 |
Correct |
2210 ms |
225776 KB |
Output is correct |
43 |
Correct |
2432 ms |
327660 KB |
Output is correct |
44 |
Correct |
2533 ms |
428340 KB |
Output is correct |
45 |
Correct |
1488 ms |
119960 KB |
Output is correct |
46 |
Correct |
2012 ms |
227012 KB |
Output is correct |
47 |
Correct |
2319 ms |
328312 KB |
Output is correct |
48 |
Correct |
2401 ms |
431220 KB |
Output is correct |