#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<ll,ll>
#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)
{
in[u]=++tg;
for(auto vv:g[u])
{
val[vv.fi]=val[u]^vv.se;
dfs(vv.fi);
}
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
{
p=p->child[ut2];
}
}
else
{
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]});
}
}
val[1]=0;
dfs();
c=1;
add(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 |
11440 KB |
Output is correct |
2 |
Correct |
6 ms |
11476 KB |
Output is correct |
3 |
Correct |
8 ms |
11684 KB |
Output is correct |
4 |
Correct |
7 ms |
11732 KB |
Output is correct |
5 |
Correct |
7 ms |
11348 KB |
Output is correct |
6 |
Correct |
7 ms |
11544 KB |
Output is correct |
7 |
Correct |
6 ms |
11712 KB |
Output is correct |
8 |
Correct |
6 ms |
11860 KB |
Output is correct |
9 |
Correct |
6 ms |
11384 KB |
Output is correct |
10 |
Correct |
6 ms |
11556 KB |
Output is correct |
11 |
Correct |
6 ms |
11676 KB |
Output is correct |
12 |
Correct |
6 ms |
11732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11440 KB |
Output is correct |
2 |
Correct |
6 ms |
11476 KB |
Output is correct |
3 |
Correct |
8 ms |
11684 KB |
Output is correct |
4 |
Correct |
7 ms |
11732 KB |
Output is correct |
5 |
Correct |
7 ms |
11348 KB |
Output is correct |
6 |
Correct |
7 ms |
11544 KB |
Output is correct |
7 |
Correct |
6 ms |
11712 KB |
Output is correct |
8 |
Correct |
6 ms |
11860 KB |
Output is correct |
9 |
Correct |
6 ms |
11384 KB |
Output is correct |
10 |
Correct |
6 ms |
11556 KB |
Output is correct |
11 |
Correct |
6 ms |
11676 KB |
Output is correct |
12 |
Correct |
6 ms |
11732 KB |
Output is correct |
13 |
Correct |
9 ms |
12728 KB |
Output is correct |
14 |
Correct |
10 ms |
14000 KB |
Output is correct |
15 |
Correct |
12 ms |
15308 KB |
Output is correct |
16 |
Correct |
14 ms |
16468 KB |
Output is correct |
17 |
Correct |
9 ms |
12624 KB |
Output is correct |
18 |
Correct |
12 ms |
13908 KB |
Output is correct |
19 |
Correct |
12 ms |
15216 KB |
Output is correct |
20 |
Correct |
12 ms |
16300 KB |
Output is correct |
21 |
Correct |
9 ms |
12592 KB |
Output is correct |
22 |
Correct |
11 ms |
13868 KB |
Output is correct |
23 |
Correct |
12 ms |
15188 KB |
Output is correct |
24 |
Correct |
13 ms |
16212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
578 ms |
127404 KB |
Output is correct |
2 |
Correct |
1014 ms |
236364 KB |
Output is correct |
3 |
Correct |
1442 ms |
338024 KB |
Output is correct |
4 |
Correct |
1880 ms |
440236 KB |
Output is correct |
5 |
Correct |
631 ms |
128436 KB |
Output is correct |
6 |
Correct |
1057 ms |
232664 KB |
Output is correct |
7 |
Correct |
1545 ms |
332612 KB |
Output is correct |
8 |
Correct |
2011 ms |
432552 KB |
Output is correct |
9 |
Correct |
634 ms |
128240 KB |
Output is correct |
10 |
Correct |
997 ms |
233600 KB |
Output is correct |
11 |
Correct |
1508 ms |
334908 KB |
Output is correct |
12 |
Correct |
1852 ms |
434424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11440 KB |
Output is correct |
2 |
Correct |
6 ms |
11476 KB |
Output is correct |
3 |
Correct |
8 ms |
11684 KB |
Output is correct |
4 |
Correct |
7 ms |
11732 KB |
Output is correct |
5 |
Correct |
7 ms |
11348 KB |
Output is correct |
6 |
Correct |
7 ms |
11544 KB |
Output is correct |
7 |
Correct |
6 ms |
11712 KB |
Output is correct |
8 |
Correct |
6 ms |
11860 KB |
Output is correct |
9 |
Correct |
6 ms |
11384 KB |
Output is correct |
10 |
Correct |
6 ms |
11556 KB |
Output is correct |
11 |
Correct |
6 ms |
11676 KB |
Output is correct |
12 |
Correct |
6 ms |
11732 KB |
Output is correct |
13 |
Correct |
9 ms |
12728 KB |
Output is correct |
14 |
Correct |
10 ms |
14000 KB |
Output is correct |
15 |
Correct |
12 ms |
15308 KB |
Output is correct |
16 |
Correct |
14 ms |
16468 KB |
Output is correct |
17 |
Correct |
9 ms |
12624 KB |
Output is correct |
18 |
Correct |
12 ms |
13908 KB |
Output is correct |
19 |
Correct |
12 ms |
15216 KB |
Output is correct |
20 |
Correct |
12 ms |
16300 KB |
Output is correct |
21 |
Correct |
9 ms |
12592 KB |
Output is correct |
22 |
Correct |
11 ms |
13868 KB |
Output is correct |
23 |
Correct |
12 ms |
15188 KB |
Output is correct |
24 |
Correct |
13 ms |
16212 KB |
Output is correct |
25 |
Correct |
578 ms |
127404 KB |
Output is correct |
26 |
Correct |
1014 ms |
236364 KB |
Output is correct |
27 |
Correct |
1442 ms |
338024 KB |
Output is correct |
28 |
Correct |
1880 ms |
440236 KB |
Output is correct |
29 |
Correct |
631 ms |
128436 KB |
Output is correct |
30 |
Correct |
1057 ms |
232664 KB |
Output is correct |
31 |
Correct |
1545 ms |
332612 KB |
Output is correct |
32 |
Correct |
2011 ms |
432552 KB |
Output is correct |
33 |
Correct |
634 ms |
128240 KB |
Output is correct |
34 |
Correct |
997 ms |
233600 KB |
Output is correct |
35 |
Correct |
1508 ms |
334908 KB |
Output is correct |
36 |
Correct |
1852 ms |
434424 KB |
Output is correct |
37 |
Correct |
1107 ms |
131444 KB |
Output is correct |
38 |
Correct |
1595 ms |
236492 KB |
Output is correct |
39 |
Correct |
1837 ms |
340912 KB |
Output is correct |
40 |
Correct |
2121 ms |
440596 KB |
Output is correct |
41 |
Correct |
1150 ms |
128932 KB |
Output is correct |
42 |
Correct |
1609 ms |
232700 KB |
Output is correct |
43 |
Correct |
1990 ms |
332708 KB |
Output is correct |
44 |
Correct |
2340 ms |
431752 KB |
Output is correct |
45 |
Correct |
1251 ms |
128584 KB |
Output is correct |
46 |
Correct |
1722 ms |
233548 KB |
Output is correct |
47 |
Correct |
2001 ms |
333500 KB |
Output is correct |
48 |
Correct |
2136 ms |
434296 KB |
Output is correct |