#include <bits/stdc++.h>
#define fast cin.tie(0)->sync_with_stdio(0);
#define inf ((int)1e18)
#define N 200005
using namespace std;
int tin[N], tout[N], xr[N], edgeVal[N], eulerTime = 1;
vector <vector<int> > adj(N);
const int maxi = 30;
int nodeSize = 0;
vector <int> zero, one;
vector <set<int> > sets;
void newNode() {
zero.push_back(0);
one.push_back(0);
set <int> st;
sets.push_back(st);
nodeSize++;
}
void add(int value, int index) {
int node = 0;
for(int bit = (1ll << maxi); bit > 0; bit /= 2) {
// do recursive shit here
if(!zero[node]) {
zero[node] = nodeSize;
newNode();
one[node] = nodeSize;
newNode();
}
if(value & bit) {
node = one[node];
}
else {
node = zero[node];
}
sets[node].insert(index);
}
}
bool areThere(int node, int left, int right) {
auto it = sets[node].lower_bound(left);
return (it == sets[node].end()) ? (false) : (*it <= right);
}
int query(int want, int left, int right) {
int node = 0, ret = 0;
for(int bit = (1ll << maxi); bit > 0; bit /= 2) {
// do recursive shit here
if(!zero[node]) {
return ret;
}
if(want & bit) { // istediğim 1
if(areThere(one[node], left, right)) {
node = one[node];
ret += bit;
}
else {
node = zero[node];
}
}
else { // istediğim 0
if(areThere(zero[node], left, right)) {
node = zero[node];
}
else {
node = one[node];
ret += bit;
}
}
}
return ret;
}
void dfs(int node) {
tin[node] = eulerTime;
eulerTime++;
for(auto it:adj[node]) {
xr[it] = xr[node] ^ edgeVal[it];
dfs(it);
}
tout[node] = eulerTime - 1;
}
int nodeIndex = 2;
int32_t main(){
fast
int n;
cin>>n;
vector <vector<int> > queries;
{ // precalculation of euler index
for(int i = 0; i < n; i++) {
string type;
cin>>type;
if(type == "Add") {
int parentNode, value;
cin>>parentNode>>value;
adj[parentNode].push_back(nodeIndex);
edgeVal[nodeIndex] = value;
queries.push_back({1, nodeIndex});
nodeIndex++;
}
else {
int starting, subTree;
cin>>starting>>subTree;
queries.push_back({0, starting, subTree});
}
}
dfs(1);
nodeIndex--;
newNode();
}
// --------- o ----------
add(0, 1);
for(auto it:queries) {
if(it[0]) {
int nodeIndex = it[1];
add(xr[nodeIndex], tin[nodeIndex]);
}
else {
int starting = it[1], subTree = it[2];
int want = ~xr[starting], stbeg = tin[subTree], stend = tout[subTree];
cout<<(xr[starting] ^ query(want, stbeg, stend))<<"\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5464 KB |
Output is correct |
2 |
Correct |
2 ms |
5464 KB |
Output is correct |
3 |
Correct |
3 ms |
5720 KB |
Output is correct |
4 |
Correct |
2 ms |
5784 KB |
Output is correct |
5 |
Correct |
2 ms |
5208 KB |
Output is correct |
6 |
Correct |
2 ms |
5468 KB |
Output is correct |
7 |
Correct |
3 ms |
5720 KB |
Output is correct |
8 |
Correct |
2 ms |
5780 KB |
Output is correct |
9 |
Correct |
2 ms |
5464 KB |
Output is correct |
10 |
Correct |
2 ms |
5724 KB |
Output is correct |
11 |
Correct |
2 ms |
5720 KB |
Output is correct |
12 |
Correct |
2 ms |
5784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5464 KB |
Output is correct |
2 |
Correct |
2 ms |
5464 KB |
Output is correct |
3 |
Correct |
3 ms |
5720 KB |
Output is correct |
4 |
Correct |
2 ms |
5784 KB |
Output is correct |
5 |
Correct |
2 ms |
5208 KB |
Output is correct |
6 |
Correct |
2 ms |
5468 KB |
Output is correct |
7 |
Correct |
3 ms |
5720 KB |
Output is correct |
8 |
Correct |
2 ms |
5780 KB |
Output is correct |
9 |
Correct |
2 ms |
5464 KB |
Output is correct |
10 |
Correct |
2 ms |
5724 KB |
Output is correct |
11 |
Correct |
2 ms |
5720 KB |
Output is correct |
12 |
Correct |
2 ms |
5784 KB |
Output is correct |
13 |
Correct |
6 ms |
7696 KB |
Output is correct |
14 |
Correct |
7 ms |
10764 KB |
Output is correct |
15 |
Correct |
9 ms |
9996 KB |
Output is correct |
16 |
Correct |
10 ms |
11016 KB |
Output is correct |
17 |
Correct |
6 ms |
7696 KB |
Output is correct |
18 |
Correct |
8 ms |
11536 KB |
Output is correct |
19 |
Correct |
10 ms |
11276 KB |
Output is correct |
20 |
Correct |
10 ms |
11024 KB |
Output is correct |
21 |
Correct |
6 ms |
7700 KB |
Output is correct |
22 |
Correct |
9 ms |
11276 KB |
Output is correct |
23 |
Correct |
10 ms |
9996 KB |
Output is correct |
24 |
Correct |
10 ms |
11020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
618 ms |
188640 KB |
Output is correct |
2 |
Correct |
1197 ms |
369620 KB |
Output is correct |
3 |
Correct |
1658 ms |
380004 KB |
Output is correct |
4 |
Correct |
2255 ms |
494620 KB |
Output is correct |
5 |
Correct |
787 ms |
191276 KB |
Output is correct |
6 |
Correct |
1497 ms |
367076 KB |
Output is correct |
7 |
Correct |
2198 ms |
375900 KB |
Output is correct |
8 |
Correct |
2738 ms |
484244 KB |
Output is correct |
9 |
Correct |
689 ms |
190944 KB |
Output is correct |
10 |
Correct |
1291 ms |
370236 KB |
Output is correct |
11 |
Correct |
1821 ms |
380124 KB |
Output is correct |
12 |
Correct |
2395 ms |
487032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5464 KB |
Output is correct |
2 |
Correct |
2 ms |
5464 KB |
Output is correct |
3 |
Correct |
3 ms |
5720 KB |
Output is correct |
4 |
Correct |
2 ms |
5784 KB |
Output is correct |
5 |
Correct |
2 ms |
5208 KB |
Output is correct |
6 |
Correct |
2 ms |
5468 KB |
Output is correct |
7 |
Correct |
3 ms |
5720 KB |
Output is correct |
8 |
Correct |
2 ms |
5780 KB |
Output is correct |
9 |
Correct |
2 ms |
5464 KB |
Output is correct |
10 |
Correct |
2 ms |
5724 KB |
Output is correct |
11 |
Correct |
2 ms |
5720 KB |
Output is correct |
12 |
Correct |
2 ms |
5784 KB |
Output is correct |
13 |
Correct |
6 ms |
7696 KB |
Output is correct |
14 |
Correct |
7 ms |
10764 KB |
Output is correct |
15 |
Correct |
9 ms |
9996 KB |
Output is correct |
16 |
Correct |
10 ms |
11016 KB |
Output is correct |
17 |
Correct |
6 ms |
7696 KB |
Output is correct |
18 |
Correct |
8 ms |
11536 KB |
Output is correct |
19 |
Correct |
10 ms |
11276 KB |
Output is correct |
20 |
Correct |
10 ms |
11024 KB |
Output is correct |
21 |
Correct |
6 ms |
7700 KB |
Output is correct |
22 |
Correct |
9 ms |
11276 KB |
Output is correct |
23 |
Correct |
10 ms |
9996 KB |
Output is correct |
24 |
Correct |
10 ms |
11020 KB |
Output is correct |
25 |
Correct |
618 ms |
188640 KB |
Output is correct |
26 |
Correct |
1197 ms |
369620 KB |
Output is correct |
27 |
Correct |
1658 ms |
380004 KB |
Output is correct |
28 |
Correct |
2255 ms |
494620 KB |
Output is correct |
29 |
Correct |
787 ms |
191276 KB |
Output is correct |
30 |
Correct |
1497 ms |
367076 KB |
Output is correct |
31 |
Correct |
2198 ms |
375900 KB |
Output is correct |
32 |
Correct |
2738 ms |
484244 KB |
Output is correct |
33 |
Correct |
689 ms |
190944 KB |
Output is correct |
34 |
Correct |
1291 ms |
370236 KB |
Output is correct |
35 |
Correct |
1821 ms |
380124 KB |
Output is correct |
36 |
Correct |
2395 ms |
487032 KB |
Output is correct |
37 |
Correct |
1121 ms |
192996 KB |
Output is correct |
38 |
Correct |
1819 ms |
372408 KB |
Output is correct |
39 |
Correct |
2305 ms |
384508 KB |
Output is correct |
40 |
Correct |
2574 ms |
494276 KB |
Output is correct |
41 |
Correct |
1326 ms |
191852 KB |
Output is correct |
42 |
Correct |
1933 ms |
367508 KB |
Output is correct |
43 |
Correct |
2457 ms |
378052 KB |
Output is correct |
44 |
Correct |
2951 ms |
484120 KB |
Output is correct |
45 |
Correct |
1219 ms |
190680 KB |
Output is correct |
46 |
Correct |
1901 ms |
367836 KB |
Output is correct |
47 |
Correct |
2487 ms |
380144 KB |
Output is correct |
48 |
Correct |
2728 ms |
487872 KB |
Output is correct |