#include <bits/stdc++.h>
using namespace std;
struct node{
int s,e;
set<int> v;
node *l,*r;
node(int _s, int _e){
s = _s;
e = _e;
if (s!=e){
l = new node(s,(s+e)/2);
r = new node((s+e)/2+1,e);
}
}
void addVal(int pos, int val){
v.insert(val);
if (s==e) return;
if (pos>(s+e)/2){
r->addVal(pos,val);
}
else{
l->addVal(pos,val);
}
}
int query(int a, int b, int val){
if (a<=s && e<=b){
if (v.empty()) return -1;
int lb = 0;
for (int x = 30; x>=0; x--){
int bitmask = ((1<<30)-1+(1<<30)-((1<<(x+1))-1));
int pref = (lb&bitmask);
if ((val&(1<<x))==0){
auto it = v.lower_bound(pref+(1<<x));
if (it==v.end()){
continue;
}
int res = *(it);
if ((res&(1<<x))!=0 && (pref==(res&bitmask))){
lb += (1<<x);
}
}
else{
auto it = v.lower_bound(pref);
if (it==v.end()){
return 1/0;
}
int res = *(it);
if ((res&(1<<x))!=0){
lb += (1<<x);
}
}
}
/*
printf("node %d to %d: ",s,e);
for (auto x : v){
printf("%d ",x);
}
printf("\n");
printf("chose %d for val %d\n",lb,val);
*/
return lb^val;
}
else if (b<=(s+e)/2){
return l->query(a,b,val);
}
else if (a>(s+e)/2){
return r->query(a,b,val);
}
else{
return max(l->query(a,b,val),r->query(a,b,val));
}
}
}*root;
int order[200005];
int subtree[200005];///subtree size
int dist[200005];///length of path to root
vector<pair<bool,pair<int,int> > > queries; ///offline lol
vector<pair<int,int> > adjl[200005];
int q;
int n = 1;
int cur = 1;
int dfs(int node){
order[node] = cur;
cur++;
subtree[node] = 1;
for (auto x : adjl[node]){
dist[x.first] = dist[node]^x.second;
subtree[node] += dfs(x.first);
}
return subtree[node];
}
int main(){
cin>>q;
for (int x = 0; x<q; x++){
string str;
cin>>str;
int a,b;
cin>>a;
cin>>b;
if (str=="Add"){
n++;
queries.push_back({true,{n,b}});
adjl[a].push_back({n,b});
}
else{
queries.push_back({false,{a,b}});
}
}
root = new node(1,n);
dfs(1);/*
for (int x = 1; x<=n; x++){
printf("%d ",dist[x]);
}
printf("\n");*/
root->addVal(1,0);
for (auto x : queries){
if (x.first){
//printf("adding %d %d\n",order[x.second.first],dist[x.second.first]);
root->addVal(order[x.second.first],dist[x.second.first]);
}
else{
int xr = dist[x.second.first];
printf("%d\n",root->query(order[x.second.second],order[x.second.second]+subtree[x.second.second]-1,xr));
}
}
}
/*
10
Add 1 4
Add 1 0
Add 3 1
Add 3 2
Add 3 3
Add 3 4
Query 2 3
Add 3 7
Add 3 9
Query 4 1
*/
Compilation message
klasika.cpp: In member function 'int node::query(int, int, int)':
klasika.cpp:46:29: warning: division by zero [-Wdiv-by-zero]
return 1/0;
~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
4992 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
5120 KB |
Output is correct |
5 |
Correct |
9 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
8 ms |
5120 KB |
Output is correct |
8 |
Correct |
8 ms |
5120 KB |
Output is correct |
9 |
Correct |
8 ms |
4992 KB |
Output is correct |
10 |
Correct |
8 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
9 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
4992 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
5120 KB |
Output is correct |
5 |
Correct |
9 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
8 ms |
5120 KB |
Output is correct |
8 |
Correct |
8 ms |
5120 KB |
Output is correct |
9 |
Correct |
8 ms |
4992 KB |
Output is correct |
10 |
Correct |
8 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
9 ms |
5120 KB |
Output is correct |
13 |
Correct |
17 ms |
5376 KB |
Output is correct |
14 |
Correct |
17 ms |
5632 KB |
Output is correct |
15 |
Correct |
22 ms |
6016 KB |
Output is correct |
16 |
Correct |
17 ms |
6400 KB |
Output is correct |
17 |
Correct |
12 ms |
5376 KB |
Output is correct |
18 |
Correct |
13 ms |
5632 KB |
Output is correct |
19 |
Correct |
14 ms |
5888 KB |
Output is correct |
20 |
Correct |
14 ms |
6272 KB |
Output is correct |
21 |
Correct |
14 ms |
5376 KB |
Output is correct |
22 |
Correct |
15 ms |
5632 KB |
Output is correct |
23 |
Correct |
15 ms |
5888 KB |
Output is correct |
24 |
Correct |
14 ms |
6272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
937 ms |
49044 KB |
Output is correct |
2 |
Correct |
1215 ms |
92440 KB |
Output is correct |
3 |
Correct |
1434 ms |
136460 KB |
Output is correct |
4 |
Correct |
1463 ms |
182560 KB |
Output is correct |
5 |
Correct |
1120 ms |
47364 KB |
Output is correct |
6 |
Correct |
1625 ms |
89508 KB |
Output is correct |
7 |
Correct |
2106 ms |
131540 KB |
Output is correct |
8 |
Correct |
2672 ms |
175756 KB |
Output is correct |
9 |
Correct |
1070 ms |
47236 KB |
Output is correct |
10 |
Correct |
1409 ms |
89828 KB |
Output is correct |
11 |
Correct |
1621 ms |
133004 KB |
Output is correct |
12 |
Correct |
1812 ms |
177180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
4992 KB |
Output is correct |
2 |
Correct |
8 ms |
5120 KB |
Output is correct |
3 |
Correct |
8 ms |
5120 KB |
Output is correct |
4 |
Correct |
8 ms |
5120 KB |
Output is correct |
5 |
Correct |
9 ms |
5120 KB |
Output is correct |
6 |
Correct |
8 ms |
5120 KB |
Output is correct |
7 |
Correct |
8 ms |
5120 KB |
Output is correct |
8 |
Correct |
8 ms |
5120 KB |
Output is correct |
9 |
Correct |
8 ms |
4992 KB |
Output is correct |
10 |
Correct |
8 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
9 ms |
5120 KB |
Output is correct |
13 |
Correct |
17 ms |
5376 KB |
Output is correct |
14 |
Correct |
17 ms |
5632 KB |
Output is correct |
15 |
Correct |
22 ms |
6016 KB |
Output is correct |
16 |
Correct |
17 ms |
6400 KB |
Output is correct |
17 |
Correct |
12 ms |
5376 KB |
Output is correct |
18 |
Correct |
13 ms |
5632 KB |
Output is correct |
19 |
Correct |
14 ms |
5888 KB |
Output is correct |
20 |
Correct |
14 ms |
6272 KB |
Output is correct |
21 |
Correct |
14 ms |
5376 KB |
Output is correct |
22 |
Correct |
15 ms |
5632 KB |
Output is correct |
23 |
Correct |
15 ms |
5888 KB |
Output is correct |
24 |
Correct |
14 ms |
6272 KB |
Output is correct |
25 |
Correct |
937 ms |
49044 KB |
Output is correct |
26 |
Correct |
1215 ms |
92440 KB |
Output is correct |
27 |
Correct |
1434 ms |
136460 KB |
Output is correct |
28 |
Correct |
1463 ms |
182560 KB |
Output is correct |
29 |
Correct |
1120 ms |
47364 KB |
Output is correct |
30 |
Correct |
1625 ms |
89508 KB |
Output is correct |
31 |
Correct |
2106 ms |
131540 KB |
Output is correct |
32 |
Correct |
2672 ms |
175756 KB |
Output is correct |
33 |
Correct |
1070 ms |
47236 KB |
Output is correct |
34 |
Correct |
1409 ms |
89828 KB |
Output is correct |
35 |
Correct |
1621 ms |
133004 KB |
Output is correct |
36 |
Correct |
1812 ms |
177180 KB |
Output is correct |
37 |
Correct |
3667 ms |
49484 KB |
Output is correct |
38 |
Correct |
3704 ms |
92620 KB |
Output is correct |
39 |
Correct |
3399 ms |
137476 KB |
Output is correct |
40 |
Correct |
2647 ms |
182688 KB |
Output is correct |
41 |
Correct |
932 ms |
47376 KB |
Output is correct |
42 |
Correct |
1409 ms |
89032 KB |
Output is correct |
43 |
Correct |
1910 ms |
131384 KB |
Output is correct |
44 |
Correct |
2523 ms |
175536 KB |
Output is correct |
45 |
Correct |
1781 ms |
47264 KB |
Output is correct |
46 |
Correct |
2192 ms |
89772 KB |
Output is correct |
47 |
Correct |
2235 ms |
133016 KB |
Output is correct |
48 |
Correct |
2053 ms |
178180 KB |
Output is correct |