#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 9;
const int Log = 30;
struct Node
{
Node *c[2];
int mn;
Node()
{
c[0] = c[1] = NULL;
mn = N;
}
};
int n, q, Timer;
vector<pair<int, int>> adj[N];
int num[N], tail[N], pos[N], d[N];
vector<pair<pair<int, int>, int>> vt;
vector<pair<int, int>> vec[N*4];
Node *Trie[N*4];
void Add(int id, int x, int y)
{
Node *p = Trie[id];
for(int i = Log; i >= 0;i --)
{
bool w = (1<<i)&x;
if(p->c[w] == NULL) p->c[w] = new Node;
p = p->c[w];
p->mn = min(p->mn, y);
}
}
int Get(int id, int x, int y)
{
Node *p = Trie[id];
for(int i = Log; i >= 0;i --)
{
bool w = (1<<i)&x;
if(p->c[w^1] != NULL and p->c[w^1]->mn <= y)
{
x ^= ((w^1)<<i);
p = p->c[w^1];
}
else if(p->c[w] != NULL and p->c[w]->mn <= y)
{
x ^= (w<<i);
p = p->c[w];
}
else
{
return 0;
}
}
return x;
}
void Dfs(int u = 1, int du = 0)
{
num[u] = ++ Timer;
d[u] = du;
pos[Timer] = u;
for(auto [v, w] : adj[u])
{
Dfs(v, du^w);
}
tail[u] = Timer;
}
void Build(int id = 1, int l = 1, int r = n)
{
if(l==r)
{
Trie[id] = new Node();
Add(id, d[pos[l]], pos[l]);
vec[id].push_back({d[pos[l]], pos[l]});
return ;
}
int mid = l+r>>1;
Build(id<<1, l, mid);
Build(id<<1|1, mid+1, r);
vec[id] = vec[id<<1];
for(auto x : vec[id<<1|1]) vec[id].push_back(x);
Trie[id] = new Node();
int lst = -1;
for(auto [x, y] : vec[id])
{
if(x==lst) continue;
Add(id, x, y);
lst = x;
}
}
int Get(int L, int R, int x, int y, int id = 1, int l = 1, int r = n)
{
if(l>r or l>R or L>r) return 0;
if(L<=l and r<=R) return Get(id, x, y);
int mid = l+r>>1;
return max(Get(L, R, x, y, id<<1, l, mid), Get(L, R, x, y, id<<1|1, mid+1, r));
}
void Solve()
{
cin>>q;
++n;
while(q--)
{
string Type; cin>>Type;
int x, y; cin >> x >> y;
if(Type == "Add")
{
adj[x].push_back({++n, y});
}
else
{
vt.push_back({{x, y}, n});
}
}
Timer = 0;
Dfs();
Build();
for(auto [uv, w] : vt)
{
auto [u, v] = uv;
cout << Get(num[v], tail[v], d[u], w) << '\n';
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int TC = 1;
// cin>>TC;
while(TC--)
{
Solve();
}
}
Compilation message
klasika.cpp: In function 'void Build(int, int, int)':
klasika.cpp:86:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
86 | int mid = l+r>>1;
| ~^~
klasika.cpp: In function 'int Get(int, int, int, int, int, int, int)':
klasika.cpp:106:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
106 | int mid = l+r>>1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
29276 KB |
Output is correct |
2 |
Correct |
7 ms |
29572 KB |
Output is correct |
3 |
Correct |
7 ms |
29788 KB |
Output is correct |
4 |
Correct |
9 ms |
30044 KB |
Output is correct |
5 |
Correct |
6 ms |
29272 KB |
Output is correct |
6 |
Correct |
6 ms |
29500 KB |
Output is correct |
7 |
Correct |
7 ms |
29784 KB |
Output is correct |
8 |
Correct |
7 ms |
30040 KB |
Output is correct |
9 |
Correct |
7 ms |
29276 KB |
Output is correct |
10 |
Correct |
7 ms |
29532 KB |
Output is correct |
11 |
Correct |
7 ms |
29788 KB |
Output is correct |
12 |
Correct |
7 ms |
30044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
29276 KB |
Output is correct |
2 |
Correct |
7 ms |
29572 KB |
Output is correct |
3 |
Correct |
7 ms |
29788 KB |
Output is correct |
4 |
Correct |
9 ms |
30044 KB |
Output is correct |
5 |
Correct |
6 ms |
29272 KB |
Output is correct |
6 |
Correct |
6 ms |
29500 KB |
Output is correct |
7 |
Correct |
7 ms |
29784 KB |
Output is correct |
8 |
Correct |
7 ms |
30040 KB |
Output is correct |
9 |
Correct |
7 ms |
29276 KB |
Output is correct |
10 |
Correct |
7 ms |
29532 KB |
Output is correct |
11 |
Correct |
7 ms |
29788 KB |
Output is correct |
12 |
Correct |
7 ms |
30044 KB |
Output is correct |
13 |
Correct |
11 ms |
32440 KB |
Output is correct |
14 |
Correct |
15 ms |
36596 KB |
Output is correct |
15 |
Correct |
18 ms |
40800 KB |
Output is correct |
16 |
Correct |
21 ms |
44636 KB |
Output is correct |
17 |
Correct |
11 ms |
32348 KB |
Output is correct |
18 |
Correct |
15 ms |
36596 KB |
Output is correct |
19 |
Correct |
17 ms |
40540 KB |
Output is correct |
20 |
Correct |
20 ms |
44368 KB |
Output is correct |
21 |
Correct |
11 ms |
32344 KB |
Output is correct |
22 |
Correct |
14 ms |
36444 KB |
Output is correct |
23 |
Correct |
18 ms |
40540 KB |
Output is correct |
24 |
Correct |
21 ms |
44380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
537 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
29276 KB |
Output is correct |
2 |
Correct |
7 ms |
29572 KB |
Output is correct |
3 |
Correct |
7 ms |
29788 KB |
Output is correct |
4 |
Correct |
9 ms |
30044 KB |
Output is correct |
5 |
Correct |
6 ms |
29272 KB |
Output is correct |
6 |
Correct |
6 ms |
29500 KB |
Output is correct |
7 |
Correct |
7 ms |
29784 KB |
Output is correct |
8 |
Correct |
7 ms |
30040 KB |
Output is correct |
9 |
Correct |
7 ms |
29276 KB |
Output is correct |
10 |
Correct |
7 ms |
29532 KB |
Output is correct |
11 |
Correct |
7 ms |
29788 KB |
Output is correct |
12 |
Correct |
7 ms |
30044 KB |
Output is correct |
13 |
Correct |
11 ms |
32440 KB |
Output is correct |
14 |
Correct |
15 ms |
36596 KB |
Output is correct |
15 |
Correct |
18 ms |
40800 KB |
Output is correct |
16 |
Correct |
21 ms |
44636 KB |
Output is correct |
17 |
Correct |
11 ms |
32348 KB |
Output is correct |
18 |
Correct |
15 ms |
36596 KB |
Output is correct |
19 |
Correct |
17 ms |
40540 KB |
Output is correct |
20 |
Correct |
20 ms |
44368 KB |
Output is correct |
21 |
Correct |
11 ms |
32344 KB |
Output is correct |
22 |
Correct |
14 ms |
36444 KB |
Output is correct |
23 |
Correct |
18 ms |
40540 KB |
Output is correct |
24 |
Correct |
21 ms |
44380 KB |
Output is correct |
25 |
Runtime error |
537 ms |
524288 KB |
Execution killed with signal 9 |
26 |
Halted |
0 ms |
0 KB |
- |