#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 15;
const int LOG = 31;
int n = 1, q, x[MAX];
int timer, tin[MAX], tout[MAX];
vector<int> adj[MAX];
struct Trie
{
int numNodes;
vector<array<int, 2>> trie;
vector<set<int>> myTins;
int create()
{
trie.push_back({ 0, 0 });
set<int> aux; myTins.push_back(aux);
return numNodes++;
}
Trie(void){ numNodes = 0; create(); }
bool check(int node, int curTin, int curTout)
{
auto it = myTins[node].lower_bound(curTin);
if(it == myTins[node].end()) return false;
return (bool)(*it <= curTout);
}
void add(int x, int curTin)
{
int cur = 0;
for(int i = LOG - 1; i >= 0; i--)
{
bool id = (x & (1 << i));
if(trie[cur][id] == 0)
{
int newNode = create();
trie[cur][id] = newNode;
}
cur = trie[cur][id];
myTins[cur].insert(curTin);
}
}
int query(int x, int curTin, int curTout)
{
int cur = 0, resp = 0;
for(int i = LOG - 1; i >= 0; i--)
{
bool id = (x & (1 << i));
if(trie[cur][!id] != 0 && check(trie[cur][!id], curTin, curTout)){
resp ^= (1 << i);
cur = trie[cur][!id];
} else if(trie[cur][id] != 0 && check(trie[cur][id], curTin, curTout)){
cur = trie[cur][id];
} else return 0;
}
return resp;
}
};
void dfs(int u, int p)
{
tin[u] = ++timer;
for(auto v : adj[u])
{
if(v == p) continue;
dfs(v, u);
}
tout[u] = timer;
}
int32_t main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> q;
vector<tuple<int, int, int>> events;
events.emplace_back(1, 1, x[1]);
while(q--)
{
string op; cin >> op;
if(op == "Add")
{
int p, w; cin >> p >> w;
int newNode = ++n;
x[newNode] = x[p] ^ w;
adj[p].push_back(newNode);
adj[newNode].push_back(p);
events.emplace_back(1, newNode, x[newNode]);
}else{
int a, b; cin >> a >> b;
events.emplace_back(2, x[a], b);
}
}
dfs(1, 1);
Trie T = Trie();
for(auto [type, x, y] : events)
{
if(type == 1) T.add(y, tin[x]);
else cout << T.query(x, tin[y], tout[y]) << '\n';
}
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
3 ms |
5212 KB |
Output is correct |
3 |
Correct |
3 ms |
5468 KB |
Output is correct |
4 |
Correct |
3 ms |
5468 KB |
Output is correct |
5 |
Correct |
2 ms |
5160 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
2 ms |
5468 KB |
Output is correct |
8 |
Correct |
3 ms |
5468 KB |
Output is correct |
9 |
Correct |
2 ms |
5364 KB |
Output is correct |
10 |
Correct |
2 ms |
5468 KB |
Output is correct |
11 |
Correct |
3 ms |
5468 KB |
Output is correct |
12 |
Correct |
3 ms |
5468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
3 ms |
5212 KB |
Output is correct |
3 |
Correct |
3 ms |
5468 KB |
Output is correct |
4 |
Correct |
3 ms |
5468 KB |
Output is correct |
5 |
Correct |
2 ms |
5160 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
2 ms |
5468 KB |
Output is correct |
8 |
Correct |
3 ms |
5468 KB |
Output is correct |
9 |
Correct |
2 ms |
5364 KB |
Output is correct |
10 |
Correct |
2 ms |
5468 KB |
Output is correct |
11 |
Correct |
3 ms |
5468 KB |
Output is correct |
12 |
Correct |
3 ms |
5468 KB |
Output is correct |
13 |
Correct |
6 ms |
6516 KB |
Output is correct |
14 |
Correct |
7 ms |
8212 KB |
Output is correct |
15 |
Correct |
8 ms |
8484 KB |
Output is correct |
16 |
Correct |
10 ms |
11280 KB |
Output is correct |
17 |
Correct |
5 ms |
6512 KB |
Output is correct |
18 |
Correct |
7 ms |
8212 KB |
Output is correct |
19 |
Correct |
8 ms |
8464 KB |
Output is correct |
20 |
Correct |
10 ms |
9232 KB |
Output is correct |
21 |
Correct |
5 ms |
6552 KB |
Output is correct |
22 |
Correct |
7 ms |
8048 KB |
Output is correct |
23 |
Correct |
8 ms |
8464 KB |
Output is correct |
24 |
Correct |
10 ms |
9184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
543 ms |
119996 KB |
Output is correct |
2 |
Correct |
940 ms |
235416 KB |
Output is correct |
3 |
Correct |
1332 ms |
289528 KB |
Output is correct |
4 |
Correct |
1688 ms |
475188 KB |
Output is correct |
5 |
Correct |
582 ms |
118136 KB |
Output is correct |
6 |
Correct |
1123 ms |
231728 KB |
Output is correct |
7 |
Correct |
1657 ms |
284872 KB |
Output is correct |
8 |
Correct |
1908 ms |
467844 KB |
Output is correct |
9 |
Correct |
516 ms |
118468 KB |
Output is correct |
10 |
Correct |
962 ms |
232480 KB |
Output is correct |
11 |
Correct |
1568 ms |
286816 KB |
Output is correct |
12 |
Correct |
1868 ms |
469044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
3 ms |
5212 KB |
Output is correct |
3 |
Correct |
3 ms |
5468 KB |
Output is correct |
4 |
Correct |
3 ms |
5468 KB |
Output is correct |
5 |
Correct |
2 ms |
5160 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
2 ms |
5468 KB |
Output is correct |
8 |
Correct |
3 ms |
5468 KB |
Output is correct |
9 |
Correct |
2 ms |
5364 KB |
Output is correct |
10 |
Correct |
2 ms |
5468 KB |
Output is correct |
11 |
Correct |
3 ms |
5468 KB |
Output is correct |
12 |
Correct |
3 ms |
5468 KB |
Output is correct |
13 |
Correct |
6 ms |
6516 KB |
Output is correct |
14 |
Correct |
7 ms |
8212 KB |
Output is correct |
15 |
Correct |
8 ms |
8484 KB |
Output is correct |
16 |
Correct |
10 ms |
11280 KB |
Output is correct |
17 |
Correct |
5 ms |
6512 KB |
Output is correct |
18 |
Correct |
7 ms |
8212 KB |
Output is correct |
19 |
Correct |
8 ms |
8464 KB |
Output is correct |
20 |
Correct |
10 ms |
9232 KB |
Output is correct |
21 |
Correct |
5 ms |
6552 KB |
Output is correct |
22 |
Correct |
7 ms |
8048 KB |
Output is correct |
23 |
Correct |
8 ms |
8464 KB |
Output is correct |
24 |
Correct |
10 ms |
9184 KB |
Output is correct |
25 |
Correct |
543 ms |
119996 KB |
Output is correct |
26 |
Correct |
940 ms |
235416 KB |
Output is correct |
27 |
Correct |
1332 ms |
289528 KB |
Output is correct |
28 |
Correct |
1688 ms |
475188 KB |
Output is correct |
29 |
Correct |
582 ms |
118136 KB |
Output is correct |
30 |
Correct |
1123 ms |
231728 KB |
Output is correct |
31 |
Correct |
1657 ms |
284872 KB |
Output is correct |
32 |
Correct |
1908 ms |
467844 KB |
Output is correct |
33 |
Correct |
516 ms |
118468 KB |
Output is correct |
34 |
Correct |
962 ms |
232480 KB |
Output is correct |
35 |
Correct |
1568 ms |
286816 KB |
Output is correct |
36 |
Correct |
1868 ms |
469044 KB |
Output is correct |
37 |
Correct |
881 ms |
120700 KB |
Output is correct |
38 |
Correct |
1413 ms |
235932 KB |
Output is correct |
39 |
Correct |
1705 ms |
292148 KB |
Output is correct |
40 |
Correct |
1891 ms |
475440 KB |
Output is correct |
41 |
Correct |
1063 ms |
118724 KB |
Output is correct |
42 |
Correct |
1615 ms |
232084 KB |
Output is correct |
43 |
Correct |
1843 ms |
284972 KB |
Output is correct |
44 |
Correct |
2137 ms |
468060 KB |
Output is correct |
45 |
Correct |
1092 ms |
118980 KB |
Output is correct |
46 |
Correct |
1732 ms |
232596 KB |
Output is correct |
47 |
Correct |
1910 ms |
285748 KB |
Output is correct |
48 |
Correct |
1986 ms |
469044 KB |
Output is correct |