#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define pb push_back
#define db(val) "[" #val " = " << (val) << "] "
const ll mod = 1e9 + 7;
const int maxn = 2e5 + 4;
const int INF = 1e9;
const int LOG = 30;
struct Query {
int t, u, v, idx;
};
struct Node {
int nxt[2];
set<int> mySet;
Node() {
nxt[0] = nxt[1] = 0;
}
};
int n, q, tin[maxn], tout[maxn], dis[maxn];
vector<pair<int,int>> adj[maxn];
vector<Query> queries;
vector<Node> t(1);
void add(int u) {
int v = 0;
for (int i = LOG; i >= 0; i--) {
t[v].mySet.insert(tin[u]);
int c = (dis[u] >> i & 1);
if (!t[v].nxt[c]) {
t[v].nxt[c] = t.size();
t.emplace_back();
}
v = t[v].nxt[c];
}
t[v].mySet.insert(tin[u]);
}
bool ok(int u, int v) {
auto lo = t[v].mySet.lower_bound(tin[u]);
if (lo == t[v].mySet.end() || *lo > tout[u]) return 0;
return 1;
}
int get(int x, int u) {
int v = 0, res = 0;
for (int i = LOG; i >= 0; i--) {
int c = (x >> i & 1) ^ 1;
if (t[v].nxt[c] && ok(u, t[v].nxt[c])) {
v = t[v].nxt[c];
res += (1 << i);
}
else if (t[v].nxt[c ^ 1] && ok(u, t[v].nxt[c ^ 1]))
v = t[v].nxt[c ^ 1];
else break;
}
return res;
}
void dfs(int u) {
tin[u] = ++tin[0];
for (auto it : adj[u]) {
int v, w; tie(v, w) = it;
dis[v] = dis[u] ^ w;
dfs(v);
}
tout[u] = tin[0];
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen(".INP", "r", stdin);
//freopen(".OUT", "w", stdout);
cin >> q;
n = 1;
for (int i = 1; i <= q; i++) {
string type; int u, v;
cin >> type >> u >> v;
if (type == "Add")
adj[u].pb({++n, v});
if (type == "Add")
queries.pb({0, u, n, i});
else queries.pb({1, u, v, i});
}
dfs(1);
add(1);
for (auto it : queries) {
int type = it.t, u = it.u, v = it.v;
if (type == 0) add(v);
else cout << get(dis[u], v) << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5212 KB |
Output is correct |
2 |
Correct |
5 ms |
5204 KB |
Output is correct |
3 |
Correct |
4 ms |
5460 KB |
Output is correct |
4 |
Correct |
5 ms |
5468 KB |
Output is correct |
5 |
Correct |
4 ms |
5084 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
5 ms |
5332 KB |
Output is correct |
8 |
Correct |
5 ms |
5460 KB |
Output is correct |
9 |
Correct |
4 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5332 KB |
Output is correct |
11 |
Correct |
3 ms |
5460 KB |
Output is correct |
12 |
Correct |
5 ms |
5408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5212 KB |
Output is correct |
2 |
Correct |
5 ms |
5204 KB |
Output is correct |
3 |
Correct |
4 ms |
5460 KB |
Output is correct |
4 |
Correct |
5 ms |
5468 KB |
Output is correct |
5 |
Correct |
4 ms |
5084 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
5 ms |
5332 KB |
Output is correct |
8 |
Correct |
5 ms |
5460 KB |
Output is correct |
9 |
Correct |
4 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5332 KB |
Output is correct |
11 |
Correct |
3 ms |
5460 KB |
Output is correct |
12 |
Correct |
5 ms |
5408 KB |
Output is correct |
13 |
Correct |
7 ms |
6512 KB |
Output is correct |
14 |
Correct |
8 ms |
8108 KB |
Output is correct |
15 |
Correct |
14 ms |
8360 KB |
Output is correct |
16 |
Correct |
11 ms |
11204 KB |
Output is correct |
17 |
Correct |
7 ms |
6512 KB |
Output is correct |
18 |
Correct |
8 ms |
7964 KB |
Output is correct |
19 |
Correct |
10 ms |
8232 KB |
Output is correct |
20 |
Correct |
14 ms |
9244 KB |
Output is correct |
21 |
Correct |
9 ms |
6512 KB |
Output is correct |
22 |
Correct |
9 ms |
7980 KB |
Output is correct |
23 |
Correct |
11 ms |
8256 KB |
Output is correct |
24 |
Correct |
14 ms |
9304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
778 ms |
119552 KB |
Output is correct |
2 |
Correct |
1534 ms |
236252 KB |
Output is correct |
3 |
Correct |
1885 ms |
292592 KB |
Output is correct |
4 |
Correct |
2765 ms |
483056 KB |
Output is correct |
5 |
Correct |
1028 ms |
119904 KB |
Output is correct |
6 |
Correct |
1596 ms |
234632 KB |
Output is correct |
7 |
Correct |
2218 ms |
289592 KB |
Output is correct |
8 |
Correct |
3146 ms |
473360 KB |
Output is correct |
9 |
Correct |
1012 ms |
120320 KB |
Output is correct |
10 |
Correct |
1514 ms |
235624 KB |
Output is correct |
11 |
Correct |
2106 ms |
291956 KB |
Output is correct |
12 |
Correct |
2953 ms |
475156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5212 KB |
Output is correct |
2 |
Correct |
5 ms |
5204 KB |
Output is correct |
3 |
Correct |
4 ms |
5460 KB |
Output is correct |
4 |
Correct |
5 ms |
5468 KB |
Output is correct |
5 |
Correct |
4 ms |
5084 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
5 ms |
5332 KB |
Output is correct |
8 |
Correct |
5 ms |
5460 KB |
Output is correct |
9 |
Correct |
4 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5332 KB |
Output is correct |
11 |
Correct |
3 ms |
5460 KB |
Output is correct |
12 |
Correct |
5 ms |
5408 KB |
Output is correct |
13 |
Correct |
7 ms |
6512 KB |
Output is correct |
14 |
Correct |
8 ms |
8108 KB |
Output is correct |
15 |
Correct |
14 ms |
8360 KB |
Output is correct |
16 |
Correct |
11 ms |
11204 KB |
Output is correct |
17 |
Correct |
7 ms |
6512 KB |
Output is correct |
18 |
Correct |
8 ms |
7964 KB |
Output is correct |
19 |
Correct |
10 ms |
8232 KB |
Output is correct |
20 |
Correct |
14 ms |
9244 KB |
Output is correct |
21 |
Correct |
9 ms |
6512 KB |
Output is correct |
22 |
Correct |
9 ms |
7980 KB |
Output is correct |
23 |
Correct |
11 ms |
8256 KB |
Output is correct |
24 |
Correct |
14 ms |
9304 KB |
Output is correct |
25 |
Correct |
778 ms |
119552 KB |
Output is correct |
26 |
Correct |
1534 ms |
236252 KB |
Output is correct |
27 |
Correct |
1885 ms |
292592 KB |
Output is correct |
28 |
Correct |
2765 ms |
483056 KB |
Output is correct |
29 |
Correct |
1028 ms |
119904 KB |
Output is correct |
30 |
Correct |
1596 ms |
234632 KB |
Output is correct |
31 |
Correct |
2218 ms |
289592 KB |
Output is correct |
32 |
Correct |
3146 ms |
473360 KB |
Output is correct |
33 |
Correct |
1012 ms |
120320 KB |
Output is correct |
34 |
Correct |
1514 ms |
235624 KB |
Output is correct |
35 |
Correct |
2106 ms |
291956 KB |
Output is correct |
36 |
Correct |
2953 ms |
475156 KB |
Output is correct |
37 |
Correct |
1486 ms |
122888 KB |
Output is correct |
38 |
Correct |
2130 ms |
239884 KB |
Output is correct |
39 |
Correct |
3650 ms |
298456 KB |
Output is correct |
40 |
Correct |
3558 ms |
483312 KB |
Output is correct |
41 |
Correct |
1742 ms |
120408 KB |
Output is correct |
42 |
Correct |
2441 ms |
235000 KB |
Output is correct |
43 |
Correct |
2856 ms |
289744 KB |
Output is correct |
44 |
Correct |
3201 ms |
473680 KB |
Output is correct |
45 |
Correct |
1624 ms |
120924 KB |
Output is correct |
46 |
Correct |
2285 ms |
236056 KB |
Output is correct |
47 |
Correct |
2612 ms |
290840 KB |
Output is correct |
48 |
Correct |
3003 ms |
475312 KB |
Output is correct |