// ॐ
#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265358979323846
#define ll long long int
const int N=2e5+5;
vector<pair<int,int>> adj[N];
int tin[N],tout[N],val[N];
int curr=0;
static const int K = 2; // check
static const int bits = 30; // check
struct node{
array<int,K> next;
int cnt;
set<pair<int,int>> s;
};
struct TRIE{
vector<node> trie;
int root,sz;
inline node create(){
node ret;
ret.cnt=0;
ret.s.clear();
for(int i=0;i<K;i++){
ret.next[i]=-1;
}
return ret;
}
inline void init(){
trie.clear();
trie.push_back(create());
sz=1;
root=0;
}
void insert(int val,int id){
int v=root;
++trie[v].cnt;
trie[v].s.insert({tin[id],tout[id]});
for(int i=bits;i>=0;i--){
int temp=val&(1<<i) ? 1 : 0;
if(trie[v].next[temp]==-1){
trie.push_back(create());
trie[v].next[temp]=(sz++);
}
v=trie[v].next[temp];
trie[v].s.insert({tin[id],tout[id]});
++trie[v].cnt;
}
}
void erase(int val,int id){
int v=root;
--trie[v].cnt;
trie[v].s.erase({tin[id],tout[id]});
for(int i=bits;i>=0;i--){
int temp=val&(1<<i) ? 1 : 0;
v=trie[v].next[temp];
trie[v].s.erase({tin[id],tout[id]});
--trie[v].cnt;
}
}
int max_query(int val,int y){
int ret=0,v=root;
for(int i=bits;i>=0;i--){
int temp=val&(1<<i) ? 0 : 1;
bool poss=0;
if(trie[v].next[temp]!=-1){
auto it=trie[trie[v].next[temp]].s.lower_bound({tin[y],tout[y]});
if(it!=trie[trie[v].next[temp]].s.end()){
int end=(*it).second;
if(end<=tout[y]){
poss=1;
}
}
}
if(!poss){
temp^=1;
}
else{
ret+=(1<<i);
}
v=trie[v].next[temp];
}
return ret;
}
};
void dfs(int v,int p,int xr){
tin[v]=(++curr);
val[v]=xr;
for(auto [u,w] : adj[v]){
if(u==p)
continue;
dfs(u,v,(xr^w));
}
tout[v]=(++curr);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int test = 1;
// cin>>test;
while(test--){
int q;
cin>>q;
vector<array<int,3>> v;
int tot=1;
for(int i=0;i<q;i++){
string s;
int a,b;
cin>>s>>a>>b;
if(s[0]=='A'){
++tot;
v.push_back({1,tot,b});
adj[a].push_back({tot,b});
adj[tot].push_back({a,b});
}
else{
v.push_back({2,a,b});
}
}
vector<int> ans;
TRIE tr;
tr.init();
reverse(v.begin(),v.end());
dfs(1,1,0);
for(int i=1;i<=tot;i++){
tr.insert(val[i],i);
}
for(auto [type,a,b] : v){
if(type==1){
// cout<<a<<' '<<val[a]<<'\n';
tr.erase(val[a],a);
}
else{
ans.push_back(tr.max_query(val[a],b));
}
}
reverse(ans.begin(),ans.end());
for(auto u : ans){
cout<<u<<'\n';
}
// cout<<'\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5408 KB |
Output is correct |
4 |
Correct |
4 ms |
5532 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5452 KB |
Output is correct |
8 |
Correct |
4 ms |
5532 KB |
Output is correct |
9 |
Correct |
4 ms |
5152 KB |
Output is correct |
10 |
Correct |
4 ms |
5332 KB |
Output is correct |
11 |
Correct |
3 ms |
5456 KB |
Output is correct |
12 |
Correct |
3 ms |
5536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5408 KB |
Output is correct |
4 |
Correct |
4 ms |
5532 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5452 KB |
Output is correct |
8 |
Correct |
4 ms |
5532 KB |
Output is correct |
9 |
Correct |
4 ms |
5152 KB |
Output is correct |
10 |
Correct |
4 ms |
5332 KB |
Output is correct |
11 |
Correct |
3 ms |
5456 KB |
Output is correct |
12 |
Correct |
3 ms |
5536 KB |
Output is correct |
13 |
Correct |
7 ms |
6696 KB |
Output is correct |
14 |
Correct |
12 ms |
8392 KB |
Output is correct |
15 |
Correct |
11 ms |
8644 KB |
Output is correct |
16 |
Correct |
13 ms |
11800 KB |
Output is correct |
17 |
Correct |
8 ms |
6688 KB |
Output is correct |
18 |
Correct |
9 ms |
8264 KB |
Output is correct |
19 |
Correct |
11 ms |
8600 KB |
Output is correct |
20 |
Correct |
13 ms |
9588 KB |
Output is correct |
21 |
Correct |
6 ms |
6688 KB |
Output is correct |
22 |
Correct |
9 ms |
8276 KB |
Output is correct |
23 |
Correct |
12 ms |
8564 KB |
Output is correct |
24 |
Correct |
13 ms |
9540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
808 ms |
128916 KB |
Output is correct |
2 |
Correct |
1542 ms |
255156 KB |
Output is correct |
3 |
Correct |
2243 ms |
310536 KB |
Output is correct |
4 |
Correct |
3015 ms |
517224 KB |
Output is correct |
5 |
Correct |
983 ms |
126580 KB |
Output is correct |
6 |
Correct |
1870 ms |
250824 KB |
Output is correct |
7 |
Correct |
2772 ms |
304436 KB |
Output is correct |
8 |
Correct |
3811 ms |
508128 KB |
Output is correct |
9 |
Correct |
805 ms |
127020 KB |
Output is correct |
10 |
Correct |
1564 ms |
251476 KB |
Output is correct |
11 |
Correct |
2348 ms |
306700 KB |
Output is correct |
12 |
Correct |
3123 ms |
509612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
3 ms |
5204 KB |
Output is correct |
3 |
Correct |
3 ms |
5408 KB |
Output is correct |
4 |
Correct |
4 ms |
5532 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5452 KB |
Output is correct |
8 |
Correct |
4 ms |
5532 KB |
Output is correct |
9 |
Correct |
4 ms |
5152 KB |
Output is correct |
10 |
Correct |
4 ms |
5332 KB |
Output is correct |
11 |
Correct |
3 ms |
5456 KB |
Output is correct |
12 |
Correct |
3 ms |
5536 KB |
Output is correct |
13 |
Correct |
7 ms |
6696 KB |
Output is correct |
14 |
Correct |
12 ms |
8392 KB |
Output is correct |
15 |
Correct |
11 ms |
8644 KB |
Output is correct |
16 |
Correct |
13 ms |
11800 KB |
Output is correct |
17 |
Correct |
8 ms |
6688 KB |
Output is correct |
18 |
Correct |
9 ms |
8264 KB |
Output is correct |
19 |
Correct |
11 ms |
8600 KB |
Output is correct |
20 |
Correct |
13 ms |
9588 KB |
Output is correct |
21 |
Correct |
6 ms |
6688 KB |
Output is correct |
22 |
Correct |
9 ms |
8276 KB |
Output is correct |
23 |
Correct |
12 ms |
8564 KB |
Output is correct |
24 |
Correct |
13 ms |
9540 KB |
Output is correct |
25 |
Correct |
808 ms |
128916 KB |
Output is correct |
26 |
Correct |
1542 ms |
255156 KB |
Output is correct |
27 |
Correct |
2243 ms |
310536 KB |
Output is correct |
28 |
Correct |
3015 ms |
517224 KB |
Output is correct |
29 |
Correct |
983 ms |
126580 KB |
Output is correct |
30 |
Correct |
1870 ms |
250824 KB |
Output is correct |
31 |
Correct |
2772 ms |
304436 KB |
Output is correct |
32 |
Correct |
3811 ms |
508128 KB |
Output is correct |
33 |
Correct |
805 ms |
127020 KB |
Output is correct |
34 |
Correct |
1564 ms |
251476 KB |
Output is correct |
35 |
Correct |
2348 ms |
306700 KB |
Output is correct |
36 |
Correct |
3123 ms |
509612 KB |
Output is correct |
37 |
Correct |
1315 ms |
129488 KB |
Output is correct |
38 |
Correct |
2001 ms |
255728 KB |
Output is correct |
39 |
Correct |
2607 ms |
312848 KB |
Output is correct |
40 |
Correct |
3334 ms |
517320 KB |
Output is correct |
41 |
Correct |
1463 ms |
127204 KB |
Output is correct |
42 |
Correct |
2354 ms |
251096 KB |
Output is correct |
43 |
Correct |
3241 ms |
304612 KB |
Output is correct |
44 |
Correct |
4123 ms |
508384 KB |
Output is correct |
45 |
Correct |
1437 ms |
127620 KB |
Output is correct |
46 |
Correct |
2224 ms |
251924 KB |
Output is correct |
47 |
Correct |
2831 ms |
305440 KB |
Output is correct |
48 |
Correct |
3620 ms |
509832 KB |
Output is correct |