#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()
using namespace std;
const int mxN = 5e5 + 227;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
struct Node{
Node* child[2];
set<int> s;
Node(){
child[0] = child[1] = nullptr;
}
};
int q;
int sta[mxN];
int fin[mxN];
int dist[mxN];
vector<pair<int, int>> adj[mxN];
pair<int, int> qr[mxN];
string cmd[mxN];
int timer = 0;
Node *root = new Node();
void add_trie(int x, int idx)
{
Node* p = root;
for(int i = 29; i >= 0; i--) {
int nxt = BIT(x, i);
if(p -> child[nxt] == nullptr) p -> child[nxt] = new Node();
p = p -> child[nxt];
(p -> s).insert(idx);
}
}
void dfs_pre(int u, int p)
{
sta[u] = ++timer;
for(auto it : adj[u]) {
int v = it.fi;
int c = it.se;
if(v == p) continue;
dist[v] = (dist[u] ^ c);
dfs_pre(v, u);
}
fin[u] = timer;
}
int findmaxxor(int x, int l, int r)
{
Node* p = root;
int res = 0;
for(int i = 29; i >= 0; i--) {
int nxt = !BIT(x, i);
if(p -> child[nxt] == nullptr) {
p = p -> child[!nxt];
}
else {
auto it = (p -> child[nxt] -> s).lower_bound(l);
if(it != (p -> child[nxt] -> s).end() && *it <= r) {
res |= (1 << i);
p = p -> child[nxt];
}
else {
p = p -> child[!nxt];
}
}
}
return res;
}
void solve()
{
cin >> q;
int node = 1;
for(int i = 1; i <= q; i++) {
cin >> cmd[i] >> qr[i].fi >> qr[i].se;
if(cmd[i] == "Add") {
++node;
adj[node].pb({qr[i].fi, qr[i].se});
adj[qr[i].fi].pb({node, qr[i].se});
}
}
dfs_pre(1, -1);
node = 1;
add_trie(dist[node], sta[node]);
for(int i = 1; i <= q; i++) {
if(cmd[i] == "Add") {
++node;
add_trie(dist[node], sta[node]);
}
else {
cout << findmaxxor(dist[qr[i].fi], sta[qr[i].se], fin[qr[i].se]) << endl;
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// freopen(TASK".inp" , "r" , stdin);
// freopen(TASK".out" , "w" , stdout);
int tc = 1;
//cin >> tc;
while(tc--) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
27860 KB |
Output is correct |
2 |
Correct |
13 ms |
27988 KB |
Output is correct |
3 |
Correct |
16 ms |
28104 KB |
Output is correct |
4 |
Correct |
14 ms |
28244 KB |
Output is correct |
5 |
Correct |
13 ms |
27800 KB |
Output is correct |
6 |
Correct |
13 ms |
28008 KB |
Output is correct |
7 |
Correct |
13 ms |
28116 KB |
Output is correct |
8 |
Correct |
15 ms |
28220 KB |
Output is correct |
9 |
Correct |
15 ms |
27876 KB |
Output is correct |
10 |
Correct |
13 ms |
27988 KB |
Output is correct |
11 |
Correct |
14 ms |
28116 KB |
Output is correct |
12 |
Correct |
16 ms |
28172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
27860 KB |
Output is correct |
2 |
Correct |
13 ms |
27988 KB |
Output is correct |
3 |
Correct |
16 ms |
28104 KB |
Output is correct |
4 |
Correct |
14 ms |
28244 KB |
Output is correct |
5 |
Correct |
13 ms |
27800 KB |
Output is correct |
6 |
Correct |
13 ms |
28008 KB |
Output is correct |
7 |
Correct |
13 ms |
28116 KB |
Output is correct |
8 |
Correct |
15 ms |
28220 KB |
Output is correct |
9 |
Correct |
15 ms |
27876 KB |
Output is correct |
10 |
Correct |
13 ms |
27988 KB |
Output is correct |
11 |
Correct |
14 ms |
28116 KB |
Output is correct |
12 |
Correct |
16 ms |
28172 KB |
Output is correct |
13 |
Correct |
17 ms |
29100 KB |
Output is correct |
14 |
Correct |
19 ms |
30452 KB |
Output is correct |
15 |
Correct |
18 ms |
31632 KB |
Output is correct |
16 |
Correct |
19 ms |
32724 KB |
Output is correct |
17 |
Correct |
17 ms |
29012 KB |
Output is correct |
18 |
Correct |
16 ms |
30264 KB |
Output is correct |
19 |
Correct |
18 ms |
31468 KB |
Output is correct |
20 |
Correct |
18 ms |
32624 KB |
Output is correct |
21 |
Correct |
18 ms |
29132 KB |
Output is correct |
22 |
Correct |
19 ms |
30336 KB |
Output is correct |
23 |
Correct |
18 ms |
31504 KB |
Output is correct |
24 |
Correct |
21 ms |
32620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
516 ms |
143580 KB |
Output is correct |
2 |
Correct |
907 ms |
247752 KB |
Output is correct |
3 |
Correct |
1350 ms |
347660 KB |
Output is correct |
4 |
Correct |
1733 ms |
448212 KB |
Output is correct |
5 |
Correct |
575 ms |
141588 KB |
Output is correct |
6 |
Correct |
972 ms |
243788 KB |
Output is correct |
7 |
Correct |
1435 ms |
341940 KB |
Output is correct |
8 |
Correct |
1821 ms |
439892 KB |
Output is correct |
9 |
Correct |
565 ms |
141068 KB |
Output is correct |
10 |
Correct |
957 ms |
244744 KB |
Output is correct |
11 |
Correct |
1332 ms |
344108 KB |
Output is correct |
12 |
Correct |
1734 ms |
442056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
27860 KB |
Output is correct |
2 |
Correct |
13 ms |
27988 KB |
Output is correct |
3 |
Correct |
16 ms |
28104 KB |
Output is correct |
4 |
Correct |
14 ms |
28244 KB |
Output is correct |
5 |
Correct |
13 ms |
27800 KB |
Output is correct |
6 |
Correct |
13 ms |
28008 KB |
Output is correct |
7 |
Correct |
13 ms |
28116 KB |
Output is correct |
8 |
Correct |
15 ms |
28220 KB |
Output is correct |
9 |
Correct |
15 ms |
27876 KB |
Output is correct |
10 |
Correct |
13 ms |
27988 KB |
Output is correct |
11 |
Correct |
14 ms |
28116 KB |
Output is correct |
12 |
Correct |
16 ms |
28172 KB |
Output is correct |
13 |
Correct |
17 ms |
29100 KB |
Output is correct |
14 |
Correct |
19 ms |
30452 KB |
Output is correct |
15 |
Correct |
18 ms |
31632 KB |
Output is correct |
16 |
Correct |
19 ms |
32724 KB |
Output is correct |
17 |
Correct |
17 ms |
29012 KB |
Output is correct |
18 |
Correct |
16 ms |
30264 KB |
Output is correct |
19 |
Correct |
18 ms |
31468 KB |
Output is correct |
20 |
Correct |
18 ms |
32624 KB |
Output is correct |
21 |
Correct |
18 ms |
29132 KB |
Output is correct |
22 |
Correct |
19 ms |
30336 KB |
Output is correct |
23 |
Correct |
18 ms |
31504 KB |
Output is correct |
24 |
Correct |
21 ms |
32620 KB |
Output is correct |
25 |
Correct |
516 ms |
143580 KB |
Output is correct |
26 |
Correct |
907 ms |
247752 KB |
Output is correct |
27 |
Correct |
1350 ms |
347660 KB |
Output is correct |
28 |
Correct |
1733 ms |
448212 KB |
Output is correct |
29 |
Correct |
575 ms |
141588 KB |
Output is correct |
30 |
Correct |
972 ms |
243788 KB |
Output is correct |
31 |
Correct |
1435 ms |
341940 KB |
Output is correct |
32 |
Correct |
1821 ms |
439892 KB |
Output is correct |
33 |
Correct |
565 ms |
141068 KB |
Output is correct |
34 |
Correct |
957 ms |
244744 KB |
Output is correct |
35 |
Correct |
1332 ms |
344108 KB |
Output is correct |
36 |
Correct |
1734 ms |
442056 KB |
Output is correct |
37 |
Correct |
1029 ms |
144500 KB |
Output is correct |
38 |
Correct |
1448 ms |
247716 KB |
Output is correct |
39 |
Correct |
1730 ms |
350432 KB |
Output is correct |
40 |
Correct |
1963 ms |
448892 KB |
Output is correct |
41 |
Correct |
1107 ms |
142140 KB |
Output is correct |
42 |
Correct |
1507 ms |
243732 KB |
Output is correct |
43 |
Correct |
1838 ms |
342032 KB |
Output is correct |
44 |
Correct |
2228 ms |
439120 KB |
Output is correct |
45 |
Correct |
1149 ms |
141484 KB |
Output is correct |
46 |
Correct |
1587 ms |
244716 KB |
Output is correct |
47 |
Correct |
1858 ms |
342948 KB |
Output is correct |
48 |
Correct |
1988 ms |
441588 KB |
Output is correct |