#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;
unordered_map<int,int> fenTrie[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 lp2(int a)
{
return a&(-a);
}
void change(int indx,int t,int v)
{
while(t<MAXN)
{
fenTrie[indx][t]+=v;
t+=lp2(t);
}
}
int query(int indx,int t)
{
int ans=0;
while(t>0)
{
ans+=fenTrie[indx][t];
t-=lp2(t);
}
return ans;
}
int getAns(int indx,int l,int r)
{
return query(indx,r)-query(indx,l-1);
}
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];
change(node,pos,1);
}
}
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=1;i<=currTreeNode;i++)cout<<startPod[i]<<" "<<endPod[i]<<endl;
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... |