#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=2e5+10;
const ll inf=1e18;
const ll mod=1e9+7;
vector<pli>g[maxN];
ll val[maxN],in[maxN],out[maxN],tg=0;
void dfs(ll u=1,ll p=0)
{
in[u]=++tg;
for(auto vv:g[u])
{
val[vv.fi]=val[u]^vv.se;
dfs(vv.fi,u);
}
out[u]=tg;
}
struct TrieNode
{
TrieNode *child[2];
set<int>s;
TrieNode()
{
child[0]=child[1]=nullptr;
}
};
TrieNode *r=new TrieNode();
void add(ll u)
{
auto p=r;
for(int i=30;i>=0;i--)
{
ll x=val[u]>>i&1;
if(p->child[x]==nullptr)
{
p->child[x]=new TrieNode();
}
p=p->child[x];
p->s.insert(in[u]);
}
}
set<int>::iterator it;
ll get(ll x,ll l,ll rr)
{
auto p=r;
ll ans=0;
for(int i=30;i>=0;i--)
{
ll ut1=1,ut2=0;
if(x>>i&1) swap(ut1,ut2);
if(p->child[ut1]!=nullptr)
{
it=p->child[ut1]->s.lower_bound(l);
if(it!=p->child[ut1]->s.end()&&*it<=rr)
{
p=p->child[ut1];
ans+=(1<<i);
}
else if(p->child[ut2]!=nullptr)
{
p=p->child[ut2];
}
}
else if(p->child[ut2]!=nullptr)
{
p=p->child[ut2];
}
}
return ans;
}
ll q,c,x[maxN],y[maxN];
string s[maxN];
void solve()
{
cin >> q;
c=1;
for(int i=1;i<=q;i++)
{
cin >> s[i];
cin >> x[i] >> y[i];
if(s[i]=="Add")
{
c++;
g[x[i]].pb({c,y[i]});
}
}
dfs();
c=1;
for(int i=1;i<=q;i++)
{
if(s[i]=="Add")
{
c++;
add(c);
}
else
{
cout << get(val[x[i]],in[y[i]],out[y[i]])<<'\n';
}
}
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11476 KB |
Output is correct |
2 |
Incorrect |
6 ms |
11476 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11476 KB |
Output is correct |
2 |
Incorrect |
6 ms |
11476 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
578 ms |
127400 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11476 KB |
Output is correct |
2 |
Incorrect |
6 ms |
11476 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |