#include<bits/stdc++.h>
using namespace std;
const int MAXN=200009,MAXL=29,MAXTRIE=6900000;
int father[MAXN],fatherW[MAXN],dp[MAXN],endPod[MAXN],startPod[MAXN],a[MAXN],b[MAXN];
string str[MAXN];
vector<int> edg[MAXN];
vector<int> eul;
set<int> s[MAXTRIE];
int trie[MAXTRIE][2];
int currNodes;
void dfs(int n)
{
startPod[n]=eul.size();
eul.push_back(n);
for(auto i:edg[n])
{
dfs(i);
}
endPod[n]=eul.size()-1;
}
int getAns(int indx,int l,int r)
{
if(s[indx].lower_bound(l)==s[indx].end())return 0;
return (*s[indx].lower_bound(l))<=r;
}
int ansQuery(int n,int l,int r)
{
int node=0,ans=0;
for(int i=MAXL; i>=0; i--)
{
bool bit=n&(1LL<<i);
if(getAns(trie[node][1-bit],l,r)==0)
{
node=trie[node][bit];
}
else
{
node=trie[node][1-bit];
ans+=(1LL<<i);
}
}
return ans;
}
void addNodeTrie(int n,int pos)
{
int node=0;
for(int i=MAXL; i>=0; i--)
{
bool bit=n&(1LL<<i);
if(trie[node][bit]==0)
{
trie[node][bit]=++currNodes;
}
node=trie[node][bit];
s[node].insert(pos);
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int Q,currTreeNode=1;
cin>>Q;
for(int i=0; i<Q; i++)
{
cin>>str[i]>>a[i]>>b[i];
if(str[i]=="Add")
{
++currTreeNode;
father[currTreeNode]=a[i];
fatherW[currTreeNode]=b[i];
edg[a[i]].push_back(currTreeNode);
a[i]=currTreeNode;
}
}
eul.push_back(-1);
dfs(1);
addNodeTrie(0,1);
for(int i=0; i<Q; i++)
{
if(str[i]=="Add")
{
addNodeTrie(dp[a[i]]=fatherW[a[i]]^dp[father[a[i]]],startPod[a[i]]);
}
else
{
cout<<ansQuery(dp[a[i]],startPod[b[i]],endPod[b[i]])<<'\n';
}
}
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |