#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 1, MOD = 998244353,b = 30;
typedef long long ll;
vector<pair<int,int>> ans;
struct node{
node *next[2];
int col = 0;
node(){
col = 0;
for(int i = 0;i < 2;i++){
next[i] = nullptr;
}
}
};
using pnode = node *;
void add(pnode &x,int val,int delta){
if(!x) x = new node();
pnode f = x;
for(int i = b;i >= 0;i--){
int nv = ((val >> i) & 1);
// cout << nv << "x\n";
if(!x->next[nv]){
x->next[nv] = new node();
}
x = x->next[nv];
x->col+=delta;
}
x = f;
}
int get(int val,pnode x){
int res = 0;
if(!x) return 0;
for(int i = b;i >= 0;i--){
int nv = ((val >> i) & 1);
nv = (!nv);
// cout << (x->next[nv] ? 1 : 0) << ' ' << (x->next[!nv] ? 1 : 0) << '\n';
if(x->next[nv] && x->next[nv]->col){
res += (1ll << i);
x = x->next[nv];
}else if(x->next[(!nv)] && x->next[!nv]->col){
x = x->next[(!nv)];
}
}
return res;
}
pnode T[N * 4];
int n,q,tadd[N],d[N],s[N];
vector<pair<int,int>> g[N],qr[N];
void dfs(int v){
s[v] = 1;
for(auto [to,w]:g[v]){
d[to] = (d[v] ^ w);
dfs(to);
s[v] += s[to];
}
}
void upd(int pos,int val,int d,int v = 1,int tl = 1,int tr = N){
add(T[v],val,d);
// if(tl == tr && tl == 1){
// cout << val << "x\n";
// }
// cout << v << " " << tl << ' ' << tr << "f\n";
if(tl == tr) return;
int tm = (tl + tr) >> 1;
if(pos <= tm) upd(pos,val,d,v+v,tl,tm);
else upd(pos,val,d,v+v+1,tm+1,tr);
}
void add_sub(int v,int dd){
upd(tadd[v],d[v],dd);
// cout << tadd[v] << ' ' << d[v] << ' ' << dd << "q\n";
for(auto [to,w]:g[v]){
add_sub(to,dd);
}
}
int get(int l,int r,int x,int v = 1,int tl = 1,int tr = N){
if(l > r || tl > r || l > tr) return 0;
if(tl >= l && tr <= r){
// cout << v << " " << tl << ' ' << tr << '\n';
return get(x,T[v]);
}else{
int tm = (tl + tr) >> 1;
return max(get(l,r,x,v+v,tl,tm),get(l,r,x,v+v+1,tm+1,tr));
}
}
void _dfs(int v,bool keep = true){
int b = -1;
for(auto [to,w]:g[v]){
if(b == -1 || s[to] > s[b]){
b = to;
}
}
for(auto [to,w]:g[v]){
if(to != b){
_dfs(to,false);
}
}
if(b != -1){
_dfs(b,1);
}
for(auto [to,w]:g[v]){
if(to == b) continue;
add_sub(to,1);
}
upd(tadd[v],d[v],1);
// cout << tadd[v] << ' ' << d[v] << ' ' << 1 << "q\n";
// cout << b << '\n';
for(auto [x,y]:qr[v]){
// cout << 1 << ' ' << y - 1 << ' ' << x << ' ' << d[x] << " " << get(1,y - 1,d[x]) << '\n';
ans.push_back({y,get(1,y - 1,d[x])});
// exit(0);
}
if(!keep) add_sub(v,-1);
}
void test(){
cin >> q;
n = 1;
tadd[1] = 1;
for(int i = 1;i <= q;i++){
string tp;
cin >> tp;
if(tp[0] == 'A'){
n++;
int x,y;
cin >> x >> y;
g[x].push_back({n,y});
tadd[n] = i + 1;
}else{
int a,b;
cin >> a >> b;
qr[b].push_back({a,i + 1});
}
}
dfs(1);
_dfs(1);
sort(ans.begin(),ans.end());
for(auto [x,y]:ans){
cout << y << '\n';
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int T = 1; // cin >> T;
while (T--)
test();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
19804 KB |
Output is correct |
2 |
Correct |
5 ms |
20316 KB |
Output is correct |
3 |
Correct |
5 ms |
20824 KB |
Output is correct |
4 |
Correct |
6 ms |
21340 KB |
Output is correct |
5 |
Correct |
4 ms |
19544 KB |
Output is correct |
6 |
Correct |
6 ms |
20316 KB |
Output is correct |
7 |
Correct |
6 ms |
20828 KB |
Output is correct |
8 |
Correct |
7 ms |
21432 KB |
Output is correct |
9 |
Correct |
5 ms |
19804 KB |
Output is correct |
10 |
Correct |
5 ms |
20572 KB |
Output is correct |
11 |
Correct |
6 ms |
21080 KB |
Output is correct |
12 |
Correct |
7 ms |
21576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
19804 KB |
Output is correct |
2 |
Correct |
5 ms |
20316 KB |
Output is correct |
3 |
Correct |
5 ms |
20824 KB |
Output is correct |
4 |
Correct |
6 ms |
21340 KB |
Output is correct |
5 |
Correct |
4 ms |
19544 KB |
Output is correct |
6 |
Correct |
6 ms |
20316 KB |
Output is correct |
7 |
Correct |
6 ms |
20828 KB |
Output is correct |
8 |
Correct |
7 ms |
21432 KB |
Output is correct |
9 |
Correct |
5 ms |
19804 KB |
Output is correct |
10 |
Correct |
5 ms |
20572 KB |
Output is correct |
11 |
Correct |
6 ms |
21080 KB |
Output is correct |
12 |
Correct |
7 ms |
21576 KB |
Output is correct |
13 |
Correct |
12 ms |
25692 KB |
Output is correct |
14 |
Correct |
18 ms |
31492 KB |
Output is correct |
15 |
Correct |
24 ms |
37200 KB |
Output is correct |
16 |
Correct |
28 ms |
42580 KB |
Output is correct |
17 |
Correct |
13 ms |
25432 KB |
Output is correct |
18 |
Correct |
22 ms |
31064 KB |
Output is correct |
19 |
Correct |
31 ms |
36948 KB |
Output is correct |
20 |
Correct |
40 ms |
42068 KB |
Output is correct |
21 |
Correct |
13 ms |
25436 KB |
Output is correct |
22 |
Correct |
19 ms |
31348 KB |
Output is correct |
23 |
Correct |
33 ms |
37084 KB |
Output is correct |
24 |
Correct |
32 ms |
42104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
531 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
19804 KB |
Output is correct |
2 |
Correct |
5 ms |
20316 KB |
Output is correct |
3 |
Correct |
5 ms |
20824 KB |
Output is correct |
4 |
Correct |
6 ms |
21340 KB |
Output is correct |
5 |
Correct |
4 ms |
19544 KB |
Output is correct |
6 |
Correct |
6 ms |
20316 KB |
Output is correct |
7 |
Correct |
6 ms |
20828 KB |
Output is correct |
8 |
Correct |
7 ms |
21432 KB |
Output is correct |
9 |
Correct |
5 ms |
19804 KB |
Output is correct |
10 |
Correct |
5 ms |
20572 KB |
Output is correct |
11 |
Correct |
6 ms |
21080 KB |
Output is correct |
12 |
Correct |
7 ms |
21576 KB |
Output is correct |
13 |
Correct |
12 ms |
25692 KB |
Output is correct |
14 |
Correct |
18 ms |
31492 KB |
Output is correct |
15 |
Correct |
24 ms |
37200 KB |
Output is correct |
16 |
Correct |
28 ms |
42580 KB |
Output is correct |
17 |
Correct |
13 ms |
25432 KB |
Output is correct |
18 |
Correct |
22 ms |
31064 KB |
Output is correct |
19 |
Correct |
31 ms |
36948 KB |
Output is correct |
20 |
Correct |
40 ms |
42068 KB |
Output is correct |
21 |
Correct |
13 ms |
25436 KB |
Output is correct |
22 |
Correct |
19 ms |
31348 KB |
Output is correct |
23 |
Correct |
33 ms |
37084 KB |
Output is correct |
24 |
Correct |
32 ms |
42104 KB |
Output is correct |
25 |
Runtime error |
531 ms |
524288 KB |
Execution killed with signal 9 |
26 |
Halted |
0 ms |
0 KB |
- |