#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
typedef pair<int,pii> piii;
const int maxn = 2*1e5 + 1;
int tin[maxn], tout[maxn], trie[31*maxn][2];
int dis[maxn],Trietimer = 0, DFStimer = 0;
vector<set<int> > StoreTin;
vector<pii> adj[maxn];
vector<piii> query;
void DFS(int u) {
tin[u] = ++DFStimer;
for (pii i: adj[u]) {
int v = i.first, w = i.second;
dis[v] = dis[u] ^ w;
DFS(v);
}
tout[u] = DFStimer;
}
void addNum(int u) {
int cur = 0;
set<int> st;
for (int i = 30; i >= 0; i--) {
int val = (dis[u] & (1 << i)) > 0;
if (!trie[cur][val]) {
trie[cur][val] = ++Trietimer;
StoreTin.push_back(st);
}
StoreTin[trie[cur][val]].insert(tin[u]);
cur = trie[cur][val];
}
}
int findNum(int u, int toXor) {
int cur = 0;
for (int i = 30; i >= 0; i--) {
int val = (toXor & (1 << i)) > 0;
bool check;
if (!trie[cur][val^1]) check = 0;
else {
auto l = StoreTin[trie[cur][val ^ 1]].lower_bound(tin[u]);
if (l != StoreTin[trie[cur][val ^ 1]].end() && *l <= tout[u]) check = 1;
else check = 0;
}
if (check) {
toXor |= (1 << i);
cur = trie[cur][val ^ 1];
} else {
if (val) toXor ^= (1 << i);
cur = trie[cur][val];
}
}
return toXor;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int Q,x,y;
cin >> Q;
string type;
int cnt = 1;
for (int i = 1; i <= Q; i++) {
cin >> type >> x >> y;
if (type[0] == 'A') {
adj[x].emplace_back(pii(++cnt,y));
query.emplace_back(piii(0,pii(cnt,y)));
} else query.emplace_back(piii(1,pii(x,y)));
}
set<int> st;
StoreTin.push_back(st);
DFS(1);
addNum(1);
for (int i = 0; i < query.size(); i++) {
if (query[i].first == 0) {
addNum(query[i].second.first);
} else {
int res = findNum(query[i].second.second,dis[query[i].second.first]);
cout << res << '\n';
}
}
return 0;
}
Compilation message
klasika.cpp: In function 'int main()':
klasika.cpp:78:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for (int i = 0; i < query.size(); i++) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
5196 KB |
Output is correct |
10 |
Correct |
3 ms |
5296 KB |
Output is correct |
11 |
Correct |
3 ms |
5324 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 |
5196 KB |
Output is correct |
10 |
Correct |
3 ms |
5296 KB |
Output is correct |
11 |
Correct |
3 ms |
5324 KB |
Output is correct |
12 |
Correct |
3 ms |
5452 KB |
Output is correct |
13 |
Correct |
5 ms |
6408 KB |
Output is correct |
14 |
Correct |
7 ms |
7812 KB |
Output is correct |
15 |
Correct |
8 ms |
8320 KB |
Output is correct |
16 |
Correct |
11 ms |
10880 KB |
Output is correct |
17 |
Correct |
5 ms |
6408 KB |
Output is correct |
18 |
Correct |
8 ms |
7812 KB |
Output is correct |
19 |
Correct |
9 ms |
8192 KB |
Output is correct |
20 |
Correct |
10 ms |
9216 KB |
Output is correct |
21 |
Correct |
6 ms |
6408 KB |
Output is correct |
22 |
Correct |
8 ms |
7812 KB |
Output is correct |
23 |
Correct |
8 ms |
8192 KB |
Output is correct |
24 |
Correct |
11 ms |
9216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
681 ms |
113028 KB |
Output is correct |
2 |
Correct |
1200 ms |
224184 KB |
Output is correct |
3 |
Correct |
1663 ms |
286192 KB |
Output is correct |
4 |
Correct |
2250 ms |
458740 KB |
Output is correct |
5 |
Correct |
705 ms |
113644 KB |
Output is correct |
6 |
Correct |
1297 ms |
222540 KB |
Output is correct |
7 |
Correct |
1806 ms |
283180 KB |
Output is correct |
8 |
Correct |
2504 ms |
449176 KB |
Output is correct |
9 |
Correct |
666 ms |
113900 KB |
Output is correct |
10 |
Correct |
1203 ms |
223256 KB |
Output is correct |
11 |
Correct |
1706 ms |
285748 KB |
Output is correct |
12 |
Correct |
2239 ms |
450932 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 |
5196 KB |
Output is correct |
10 |
Correct |
3 ms |
5296 KB |
Output is correct |
11 |
Correct |
3 ms |
5324 KB |
Output is correct |
12 |
Correct |
3 ms |
5452 KB |
Output is correct |
13 |
Correct |
5 ms |
6408 KB |
Output is correct |
14 |
Correct |
7 ms |
7812 KB |
Output is correct |
15 |
Correct |
8 ms |
8320 KB |
Output is correct |
16 |
Correct |
11 ms |
10880 KB |
Output is correct |
17 |
Correct |
5 ms |
6408 KB |
Output is correct |
18 |
Correct |
8 ms |
7812 KB |
Output is correct |
19 |
Correct |
9 ms |
8192 KB |
Output is correct |
20 |
Correct |
10 ms |
9216 KB |
Output is correct |
21 |
Correct |
6 ms |
6408 KB |
Output is correct |
22 |
Correct |
8 ms |
7812 KB |
Output is correct |
23 |
Correct |
8 ms |
8192 KB |
Output is correct |
24 |
Correct |
11 ms |
9216 KB |
Output is correct |
25 |
Correct |
681 ms |
113028 KB |
Output is correct |
26 |
Correct |
1200 ms |
224184 KB |
Output is correct |
27 |
Correct |
1663 ms |
286192 KB |
Output is correct |
28 |
Correct |
2250 ms |
458740 KB |
Output is correct |
29 |
Correct |
705 ms |
113644 KB |
Output is correct |
30 |
Correct |
1297 ms |
222540 KB |
Output is correct |
31 |
Correct |
1806 ms |
283180 KB |
Output is correct |
32 |
Correct |
2504 ms |
449176 KB |
Output is correct |
33 |
Correct |
666 ms |
113900 KB |
Output is correct |
34 |
Correct |
1203 ms |
223256 KB |
Output is correct |
35 |
Correct |
1706 ms |
285748 KB |
Output is correct |
36 |
Correct |
2239 ms |
450932 KB |
Output is correct |
37 |
Correct |
1154 ms |
116596 KB |
Output is correct |
38 |
Correct |
1714 ms |
227636 KB |
Output is correct |
39 |
Correct |
2098 ms |
292156 KB |
Output is correct |
40 |
Correct |
2447 ms |
459044 KB |
Output is correct |
41 |
Correct |
1279 ms |
114004 KB |
Output is correct |
42 |
Correct |
1821 ms |
222724 KB |
Output is correct |
43 |
Correct |
2247 ms |
283424 KB |
Output is correct |
44 |
Correct |
2645 ms |
449512 KB |
Output is correct |
45 |
Correct |
1307 ms |
114372 KB |
Output is correct |
46 |
Correct |
1881 ms |
223592 KB |
Output is correct |
47 |
Correct |
2306 ms |
284420 KB |
Output is correct |
48 |
Correct |
2597 ms |
451128 KB |
Output is correct |