#include <iostream>
#include <vector>
#include <map>
using namespace std;
int best=0;
vector<vector<int>> trie(1,vector<int>(2,-1));
void add(int u,int x){
for(int i=30;i>=0;i--){
if(x&(1<<i)){
if(trie[u][1]==-1){
vector<int> v(2,-1);
trie[u][1]=trie.size();
trie.push_back(v);
}
u=trie[u][1];
}
else {
if(trie[u][0]==-1){
vector<int> v(2,-1);
trie[u][0]=trie.size();
trie.push_back(v);
}
u=trie[u][0];
}
}
}
int maxi(int u,int x){
int ans=0;
for(int i=30;i>=0;i--){
int op=x&(1<<i);
if(op)op=1;
if(trie[u][1-op]!=-1){
u=trie[u][1-op];
ans+=(1LL*(1<<i));
}
else {
u=trie[u][op];
}
}
return ans;
}
int bin_jump_dist(int a,int dif,vector<vector<int>>& par,vector<vector<int>>& dist){
int ans=0;
for(int i=17;i>=0;i--){
if(dif&(1<<i)){
ans^=dist[a][i];
a=par[a][i];
}
}
return ans;
}
int bin_jump_node(int a,int dif,vector<vector<int>>& par,vector<vector<int>>& dist){
int ans=0;
for(int i=17;i>=0;i--){
if(dif&(1<<i)){
ans^=dist[a][i];
a=par[a][i];
}
}
return a;
}
int LCA(int x,int y,vector<vector<int>>& par,vector<vector<int>>& dist,vector<int>& depth){
if(depth[x]>depth[y])swap(x,y);
int ans=bin_jump_dist(y,depth[y]-depth[x],par,dist);
y=bin_jump_node(y,depth[y]-depth[x],par,dist);
//cout<<ans<<'\n';
if(x==y)return ans;
for(int i=17;i>=0;i--){
if(par[x][i]!=par[y][i]){
ans^=dist[x][i];
ans^=dist[y][i];
x=par[x][i];
y=par[y][i];
}
}
return ans^dist[x][0]^dist[y][0];
}
void dfs(int a,vector<vector<pair<int,int>>>& g,int cnt,int sol){
best=max(best,cnt^sol);
for(auto x:g[a]){
dfs(x.first,g,cnt^x.second,sol);
}
}
int main(){
int q,x,y,sz=1;
string s;
vector<vector<int>> par(1,vector<int>(18));
vector<vector<int>> w(1,vector<int>(18));
vector<int> depth(1),root1(1);
for(int i=0;i<18;i++)par[0][i]=0;
vector<vector<pair<int,int>>> g(3000,vector<pair<int,int>>());
add(0,0);
cin>>q;
for(int qu=0;qu<q;qu++){
cin>>s>>x>>y;
if(s=="Add"){
x--;
if(q<2001)g[x].push_back({par.size(),y});
depth.push_back(depth[x]+1);
root1.push_back(y^root1[x]);
add(0,root1.back());
vector<int> parx(18),wx(18);
wx[0]=y;
parx[0]=x;
for(int i=1;i<18;i++){
parx[i]=par[parx[i-1]][i-1];
wx[i]=w[parx[i-1]][i-1]^wx[i-1];
//cout<<wx[i]<<' ';
}
//cout<<wx[0]<<'\n';
par.push_back(parx);
w.push_back(wx);
}
else{
x--;y--;
int sol=LCA(x,y,par,w,depth);
best=sol;
//cout<<sol<<'\n';
if(q>2000)best=maxi(0,best);
else dfs(y,g,0,sol);
cout<<best<<'\n';
//cout<<trie.size()<<'\n';
}
}
}
Compilation message
klasika.cpp: In function 'int main()':
klasika.cpp:85:15: warning: unused variable 'sz' [-Wunused-variable]
85 | int q,x,y,sz=1;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
7 ms |
1108 KB |
Output is correct |
14 |
Correct |
8 ms |
1808 KB |
Output is correct |
15 |
Correct |
9 ms |
2060 KB |
Output is correct |
16 |
Correct |
9 ms |
3340 KB |
Output is correct |
17 |
Correct |
5 ms |
1108 KB |
Output is correct |
18 |
Correct |
9 ms |
1808 KB |
Output is correct |
19 |
Correct |
7 ms |
2060 KB |
Output is correct |
20 |
Correct |
6 ms |
2444 KB |
Output is correct |
21 |
Correct |
6 ms |
1072 KB |
Output is correct |
22 |
Correct |
6 ms |
1808 KB |
Output is correct |
23 |
Correct |
6 ms |
2060 KB |
Output is correct |
24 |
Correct |
7 ms |
2572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
690 ms |
49824 KB |
Output is correct |
2 |
Correct |
769 ms |
102412 KB |
Output is correct |
3 |
Correct |
807 ms |
123144 KB |
Output is correct |
4 |
Correct |
873 ms |
200580 KB |
Output is correct |
5 |
Correct |
612 ms |
52356 KB |
Output is correct |
6 |
Correct |
693 ms |
102404 KB |
Output is correct |
7 |
Correct |
739 ms |
123416 KB |
Output is correct |
8 |
Correct |
823 ms |
200408 KB |
Output is correct |
9 |
Correct |
705 ms |
52412 KB |
Output is correct |
10 |
Correct |
781 ms |
102440 KB |
Output is correct |
11 |
Correct |
750 ms |
124024 KB |
Output is correct |
12 |
Correct |
793 ms |
200604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
7 ms |
1108 KB |
Output is correct |
14 |
Correct |
8 ms |
1808 KB |
Output is correct |
15 |
Correct |
9 ms |
2060 KB |
Output is correct |
16 |
Correct |
9 ms |
3340 KB |
Output is correct |
17 |
Correct |
5 ms |
1108 KB |
Output is correct |
18 |
Correct |
9 ms |
1808 KB |
Output is correct |
19 |
Correct |
7 ms |
2060 KB |
Output is correct |
20 |
Correct |
6 ms |
2444 KB |
Output is correct |
21 |
Correct |
6 ms |
1072 KB |
Output is correct |
22 |
Correct |
6 ms |
1808 KB |
Output is correct |
23 |
Correct |
6 ms |
2060 KB |
Output is correct |
24 |
Correct |
7 ms |
2572 KB |
Output is correct |
25 |
Correct |
690 ms |
49824 KB |
Output is correct |
26 |
Correct |
769 ms |
102412 KB |
Output is correct |
27 |
Correct |
807 ms |
123144 KB |
Output is correct |
28 |
Correct |
873 ms |
200580 KB |
Output is correct |
29 |
Correct |
612 ms |
52356 KB |
Output is correct |
30 |
Correct |
693 ms |
102404 KB |
Output is correct |
31 |
Correct |
739 ms |
123416 KB |
Output is correct |
32 |
Correct |
823 ms |
200408 KB |
Output is correct |
33 |
Correct |
705 ms |
52412 KB |
Output is correct |
34 |
Correct |
781 ms |
102440 KB |
Output is correct |
35 |
Correct |
750 ms |
124024 KB |
Output is correct |
36 |
Correct |
793 ms |
200604 KB |
Output is correct |
37 |
Incorrect |
774 ms |
52752 KB |
Output isn't correct |
38 |
Halted |
0 ms |
0 KB |
- |