/**
* author: longvu
* created: 11/22/22 20:04:54
**/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
const int INF = numeric_limits<int>::max();
const int nax = (int)(200901);
const int mod = 1e9 + 7;
template<class X, class Y>
bool maximize(X& x, const Y y) {
if (y > x) {x = y; return true;}
return false;
}
template<class X, class Y>
bool minimize(X& x, const Y y) {
if (y < x) {x = y; return true;}
return false;
}
struct Node {
Node *node_child[2] = {nullptr};
set<int> memo_idx;
};
#define get_bit(mask, x) (((mask) >> (x)) & 1)
void insert(Node *root, int u, int c) {
for (int i = 30; i >= 0; --i) {
if (root -> node_child[get_bit(c, i)] == nullptr) {
root -> node_child[get_bit(c, i)] = new Node;
}
root -> node_child[get_bit(c, i)] -> memo_idx.insert(u);
root = root -> node_child[get_bit(c, i)];
}
}
int get(Node *root, int L, int R, int c) {
int res = 0;
for (int i = 30; i >= 0; --i) {
if (root -> node_child[!get_bit(c, i)] != nullptr) {
set<int>::iterator l = root -> node_child[!get_bit(c, i)] -> memo_idx.lower_bound(L);
if (l != root -> node_child[!get_bit(c, i)] -> memo_idx.end() && *l >= L && *l <= R) {
res |= (1 << i);
root = root -> node_child[!get_bit(c, i)];
continue;
}
}
if (root -> node_child[get_bit(c, i)] != nullptr) {
set<int>::iterator l = root -> node_child[get_bit(c, i)] -> memo_idx.lower_bound(L);
if (l != root -> node_child[get_bit(c, i)] -> memo_idx.end() && *l >= L && *l <= R) {
root = root -> node_child[get_bit(c, i)];
continue;
}
}
break;
}
return res;
}
#define Fi first
#define Se second
string TYPE[nax];
int U[nax], V[nax], C[nax], euler[nax], m = 0, fir[nax], subsz[nax], cost[nax];
vector<pair<int, int>> adj[nax];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int q;
cin >> q;
int n = 1;
for (int i = 1; i <= q; ++i) {
cin >> TYPE[i] >> U[i] >> C[i];
if (TYPE[i] == "Add") {
n++;
V[i] = n;
adj[U[i]].push_back({V[i], C[i]});
adj[V[i]].push_back({U[i], C[i]});
}
}
function<void(int, int)> dfs = [&](int u, int pa) {
euler[++m] = u;
fir[u] = m;
subsz[u] = 1;
for (auto v : adj[u]) {
if (v.Fi == pa) {
continue;
}
cost[v.Fi] = v.Se ^ cost[u];
dfs(v.Fi, u);
subsz[u] += subsz[v.Fi];
}
};
dfs(1, -1);
// for (int i = 1; i <= n; ++i) {
// cout << euler[i] << " ";
// }
// cout << '\n';
Node *root = new Node();
insert(root, fir[1], cost[1]);
for (int i = 1; i <= q; ++i) {
if (TYPE[i] == "Add") {
insert(root, fir[V[i]], cost[V[i]]);
} else {
cout << get(root, fir[C[i]], fir[C[i]] + subsz[C[i]] - 1, cost[U[i]]) << '\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
11476 KB |
Output is correct |
2 |
Correct |
7 ms |
11604 KB |
Output is correct |
3 |
Correct |
7 ms |
11732 KB |
Output is correct |
4 |
Correct |
6 ms |
11912 KB |
Output is correct |
5 |
Correct |
6 ms |
11476 KB |
Output is correct |
6 |
Correct |
6 ms |
11600 KB |
Output is correct |
7 |
Correct |
6 ms |
11732 KB |
Output is correct |
8 |
Correct |
7 ms |
11860 KB |
Output is correct |
9 |
Correct |
6 ms |
11464 KB |
Output is correct |
10 |
Correct |
6 ms |
11604 KB |
Output is correct |
11 |
Correct |
6 ms |
11732 KB |
Output is correct |
12 |
Correct |
6 ms |
11860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
11476 KB |
Output is correct |
2 |
Correct |
7 ms |
11604 KB |
Output is correct |
3 |
Correct |
7 ms |
11732 KB |
Output is correct |
4 |
Correct |
6 ms |
11912 KB |
Output is correct |
5 |
Correct |
6 ms |
11476 KB |
Output is correct |
6 |
Correct |
6 ms |
11600 KB |
Output is correct |
7 |
Correct |
6 ms |
11732 KB |
Output is correct |
8 |
Correct |
7 ms |
11860 KB |
Output is correct |
9 |
Correct |
6 ms |
11464 KB |
Output is correct |
10 |
Correct |
6 ms |
11604 KB |
Output is correct |
11 |
Correct |
6 ms |
11732 KB |
Output is correct |
12 |
Correct |
6 ms |
11860 KB |
Output is correct |
13 |
Correct |
8 ms |
12884 KB |
Output is correct |
14 |
Correct |
9 ms |
14168 KB |
Output is correct |
15 |
Correct |
12 ms |
15400 KB |
Output is correct |
16 |
Correct |
13 ms |
16596 KB |
Output is correct |
17 |
Correct |
10 ms |
12672 KB |
Output is correct |
18 |
Correct |
12 ms |
14040 KB |
Output is correct |
19 |
Correct |
12 ms |
15320 KB |
Output is correct |
20 |
Correct |
12 ms |
16472 KB |
Output is correct |
21 |
Correct |
9 ms |
12756 KB |
Output is correct |
22 |
Correct |
10 ms |
14036 KB |
Output is correct |
23 |
Correct |
13 ms |
15316 KB |
Output is correct |
24 |
Correct |
13 ms |
16424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
621 ms |
132404 KB |
Output is correct |
2 |
Correct |
980 ms |
242028 KB |
Output is correct |
3 |
Correct |
1403 ms |
346648 KB |
Output is correct |
4 |
Correct |
1920 ms |
452368 KB |
Output is correct |
5 |
Correct |
628 ms |
129268 KB |
Output is correct |
6 |
Correct |
1078 ms |
234592 KB |
Output is correct |
7 |
Correct |
1532 ms |
335508 KB |
Output is correct |
8 |
Correct |
2058 ms |
436480 KB |
Output is correct |
9 |
Correct |
593 ms |
129100 KB |
Output is correct |
10 |
Correct |
1022 ms |
236040 KB |
Output is correct |
11 |
Correct |
1409 ms |
338776 KB |
Output is correct |
12 |
Correct |
1848 ms |
439736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
11476 KB |
Output is correct |
2 |
Correct |
7 ms |
11604 KB |
Output is correct |
3 |
Correct |
7 ms |
11732 KB |
Output is correct |
4 |
Correct |
6 ms |
11912 KB |
Output is correct |
5 |
Correct |
6 ms |
11476 KB |
Output is correct |
6 |
Correct |
6 ms |
11600 KB |
Output is correct |
7 |
Correct |
6 ms |
11732 KB |
Output is correct |
8 |
Correct |
7 ms |
11860 KB |
Output is correct |
9 |
Correct |
6 ms |
11464 KB |
Output is correct |
10 |
Correct |
6 ms |
11604 KB |
Output is correct |
11 |
Correct |
6 ms |
11732 KB |
Output is correct |
12 |
Correct |
6 ms |
11860 KB |
Output is correct |
13 |
Correct |
8 ms |
12884 KB |
Output is correct |
14 |
Correct |
9 ms |
14168 KB |
Output is correct |
15 |
Correct |
12 ms |
15400 KB |
Output is correct |
16 |
Correct |
13 ms |
16596 KB |
Output is correct |
17 |
Correct |
10 ms |
12672 KB |
Output is correct |
18 |
Correct |
12 ms |
14040 KB |
Output is correct |
19 |
Correct |
12 ms |
15320 KB |
Output is correct |
20 |
Correct |
12 ms |
16472 KB |
Output is correct |
21 |
Correct |
9 ms |
12756 KB |
Output is correct |
22 |
Correct |
10 ms |
14036 KB |
Output is correct |
23 |
Correct |
13 ms |
15316 KB |
Output is correct |
24 |
Correct |
13 ms |
16424 KB |
Output is correct |
25 |
Correct |
621 ms |
132404 KB |
Output is correct |
26 |
Correct |
980 ms |
242028 KB |
Output is correct |
27 |
Correct |
1403 ms |
346648 KB |
Output is correct |
28 |
Correct |
1920 ms |
452368 KB |
Output is correct |
29 |
Correct |
628 ms |
129268 KB |
Output is correct |
30 |
Correct |
1078 ms |
234592 KB |
Output is correct |
31 |
Correct |
1532 ms |
335508 KB |
Output is correct |
32 |
Correct |
2058 ms |
436480 KB |
Output is correct |
33 |
Correct |
593 ms |
129100 KB |
Output is correct |
34 |
Correct |
1022 ms |
236040 KB |
Output is correct |
35 |
Correct |
1409 ms |
338776 KB |
Output is correct |
36 |
Correct |
1848 ms |
439736 KB |
Output is correct |
37 |
Correct |
1033 ms |
136392 KB |
Output is correct |
38 |
Correct |
1460 ms |
244920 KB |
Output is correct |
39 |
Correct |
1803 ms |
352872 KB |
Output is correct |
40 |
Correct |
2019 ms |
456124 KB |
Output is correct |
41 |
Correct |
1277 ms |
131844 KB |
Output is correct |
42 |
Correct |
1755 ms |
237004 KB |
Output is correct |
43 |
Correct |
2032 ms |
338384 KB |
Output is correct |
44 |
Correct |
2285 ms |
438876 KB |
Output is correct |
45 |
Correct |
1381 ms |
131808 KB |
Output is correct |
46 |
Correct |
1847 ms |
238488 KB |
Output is correct |
47 |
Correct |
2077 ms |
340460 KB |
Output is correct |
48 |
Correct |
2357 ms |
442632 KB |
Output is correct |