#include <bits/stdc++.h>
using namespace std;
#define nl "\n"
#define nf endl
#define ll long long
#define pb push_back
#define _ << ' ' <<
#define INF (ll)1e18
#define mod 998244353
#define maxn 200010
struct trie {
vector<array<ll, 2>> ta;
vector<array<ll, 3>> tv;
ll tcr, td;
trie(): ta({}), tv({{0, 0, 0}}), tcr(1), td(0) {}
void ins(ll p, ll x) {
ll i, k, s = 0;
ta.pb({p, x});
for (i = 30; i >= -1; i--) {
tv[s][2] = min(tv[s][2], p);
if (i == -1) continue;
k = ((x >> i) & 1);
if (tv[s][k] == 0) {
tv[s][k] = tcr; tcr++; tv.pb({0, 0, INF});
}
s = tv[s][k];
}
}
ll find_max(ll x, ll m) {
// cout << "find_max " << x _ m << nl;
ll i, k, s = 0, r = 0;
for (i = 30; i >= 0; i--) {
k = ((x >> i) & 1);
if (tv[s][k ^ 1] != 0 && tv[tv[s][k ^ 1]][2] <= m) {
s = tv[s][k ^ 1]; r += (1 << i);
} else {
s = tv[s][k];
}
}
return r;
}
};
ll i, i1, j, k, k1, t, n, m, res, flag[10], a, b;
ll cr, pr[maxn], dp[maxn], q;
bool vis[maxn];
string s;
vector<array<ll, 2>> rs;
vector<array<ll, 2>> adj[maxn];
vector<array<ll, 3>> qdj[maxn];
trie tr[maxn];
void onion(ll a, ll b, ll d) { // (a, b) = (high, low)
if (tr[a].ta.size() < tr[b].ta.size()) {
swap(tr[a], tr[b]); tr[a].td ^= d; tr[b].td ^= d;
}
for (auto [p, x] : tr[b].ta) tr[a].ins(p, x ^ d ^ tr[a].td ^ tr[b].td);
tr[b] = trie();
}
void dfs(ll s) {
for (auto [u, w] : adj[s]) {
dp[u] = dp[s] ^ w;
dfs(u);
}
}
void sml(ll s) {
for (auto [u, w] : adj[s]) {
sml(u);
onion(s, u, w);
}
for (auto [u, m, t] : qdj[s]) {
rs.pb({t, tr[s].find_max(dp[s] ^ dp[u] ^ tr[s].td, m)});
}
/* cout << "dbg trie " << s << nl;
for (auto u : tr[s].ta) cout << u[0] _ u[1] << nl;
for (auto u : tr[s].tv) cout << u[0] _ u[1] _ u[2] << nl;
cout << "tcr = " << tr[s].tcr << nl;
cout << "td = " << tr[s].td << nl;
cout << nl; */
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
#if !ONLINE_JUDGE && !EVAL
ifstream cin("input.txt");
ofstream cout("output.txt");
#endif
cin >> q; cr = 2;
for (i = 1; i <= q; i++) {
cin >> s >> a >> b;
if (s[0] == 'A') {
adj[a].pb({cr, b}); cr++; // directed?
} else {
qdj[b].pb({a, cr - 1, i});
}
}
n = cr - 1;
for (i = 1; i <= n; i++) tr[i].ins(i, 0);
dfs(1); sml(1);
sort(rs.begin(), rs.end());
for (auto [cr, u] : rs) cout << u << nl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
28620 KB |
Output is correct |
2 |
Correct |
24 ms |
28572 KB |
Output is correct |
3 |
Correct |
24 ms |
28764 KB |
Output is correct |
4 |
Correct |
24 ms |
28704 KB |
Output is correct |
5 |
Correct |
31 ms |
28576 KB |
Output is correct |
6 |
Correct |
25 ms |
28620 KB |
Output is correct |
7 |
Correct |
25 ms |
28752 KB |
Output is correct |
8 |
Correct |
25 ms |
28848 KB |
Output is correct |
9 |
Correct |
24 ms |
28592 KB |
Output is correct |
10 |
Correct |
24 ms |
28768 KB |
Output is correct |
11 |
Correct |
24 ms |
28792 KB |
Output is correct |
12 |
Correct |
25 ms |
28756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
28620 KB |
Output is correct |
2 |
Correct |
24 ms |
28572 KB |
Output is correct |
3 |
Correct |
24 ms |
28764 KB |
Output is correct |
4 |
Correct |
24 ms |
28704 KB |
Output is correct |
5 |
Correct |
31 ms |
28576 KB |
Output is correct |
6 |
Correct |
25 ms |
28620 KB |
Output is correct |
7 |
Correct |
25 ms |
28752 KB |
Output is correct |
8 |
Correct |
25 ms |
28848 KB |
Output is correct |
9 |
Correct |
24 ms |
28592 KB |
Output is correct |
10 |
Correct |
24 ms |
28768 KB |
Output is correct |
11 |
Correct |
24 ms |
28792 KB |
Output is correct |
12 |
Correct |
25 ms |
28756 KB |
Output is correct |
13 |
Correct |
27 ms |
29488 KB |
Output is correct |
14 |
Correct |
27 ms |
30144 KB |
Output is correct |
15 |
Correct |
28 ms |
30552 KB |
Output is correct |
16 |
Correct |
28 ms |
31688 KB |
Output is correct |
17 |
Correct |
27 ms |
29528 KB |
Output is correct |
18 |
Correct |
30 ms |
30188 KB |
Output is correct |
19 |
Correct |
28 ms |
30716 KB |
Output is correct |
20 |
Correct |
31 ms |
31060 KB |
Output is correct |
21 |
Correct |
27 ms |
29372 KB |
Output is correct |
22 |
Correct |
28 ms |
30196 KB |
Output is correct |
23 |
Correct |
25 ms |
30768 KB |
Output is correct |
24 |
Correct |
28 ms |
31116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
267 ms |
93688 KB |
Output is correct |
2 |
Correct |
366 ms |
157204 KB |
Output is correct |
3 |
Correct |
438 ms |
192492 KB |
Output is correct |
4 |
Correct |
500 ms |
278052 KB |
Output is correct |
5 |
Correct |
376 ms |
93096 KB |
Output is correct |
6 |
Correct |
615 ms |
175868 KB |
Output is correct |
7 |
Correct |
776 ms |
198092 KB |
Output is correct |
8 |
Correct |
990 ms |
283912 KB |
Output is correct |
9 |
Correct |
330 ms |
100296 KB |
Output is correct |
10 |
Correct |
427 ms |
168616 KB |
Output is correct |
11 |
Correct |
552 ms |
212076 KB |
Output is correct |
12 |
Correct |
650 ms |
303504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
28620 KB |
Output is correct |
2 |
Correct |
24 ms |
28572 KB |
Output is correct |
3 |
Correct |
24 ms |
28764 KB |
Output is correct |
4 |
Correct |
24 ms |
28704 KB |
Output is correct |
5 |
Correct |
31 ms |
28576 KB |
Output is correct |
6 |
Correct |
25 ms |
28620 KB |
Output is correct |
7 |
Correct |
25 ms |
28752 KB |
Output is correct |
8 |
Correct |
25 ms |
28848 KB |
Output is correct |
9 |
Correct |
24 ms |
28592 KB |
Output is correct |
10 |
Correct |
24 ms |
28768 KB |
Output is correct |
11 |
Correct |
24 ms |
28792 KB |
Output is correct |
12 |
Correct |
25 ms |
28756 KB |
Output is correct |
13 |
Correct |
27 ms |
29488 KB |
Output is correct |
14 |
Correct |
27 ms |
30144 KB |
Output is correct |
15 |
Correct |
28 ms |
30552 KB |
Output is correct |
16 |
Correct |
28 ms |
31688 KB |
Output is correct |
17 |
Correct |
27 ms |
29528 KB |
Output is correct |
18 |
Correct |
30 ms |
30188 KB |
Output is correct |
19 |
Correct |
28 ms |
30716 KB |
Output is correct |
20 |
Correct |
31 ms |
31060 KB |
Output is correct |
21 |
Correct |
27 ms |
29372 KB |
Output is correct |
22 |
Correct |
28 ms |
30196 KB |
Output is correct |
23 |
Correct |
25 ms |
30768 KB |
Output is correct |
24 |
Correct |
28 ms |
31116 KB |
Output is correct |
25 |
Correct |
267 ms |
93688 KB |
Output is correct |
26 |
Correct |
366 ms |
157204 KB |
Output is correct |
27 |
Correct |
438 ms |
192492 KB |
Output is correct |
28 |
Correct |
500 ms |
278052 KB |
Output is correct |
29 |
Correct |
376 ms |
93096 KB |
Output is correct |
30 |
Correct |
615 ms |
175868 KB |
Output is correct |
31 |
Correct |
776 ms |
198092 KB |
Output is correct |
32 |
Correct |
990 ms |
283912 KB |
Output is correct |
33 |
Correct |
330 ms |
100296 KB |
Output is correct |
34 |
Correct |
427 ms |
168616 KB |
Output is correct |
35 |
Correct |
552 ms |
212076 KB |
Output is correct |
36 |
Correct |
650 ms |
303504 KB |
Output is correct |
37 |
Correct |
319 ms |
98508 KB |
Output is correct |
38 |
Correct |
394 ms |
158536 KB |
Output is correct |
39 |
Correct |
469 ms |
194400 KB |
Output is correct |
40 |
Correct |
500 ms |
278632 KB |
Output is correct |
41 |
Correct |
393 ms |
112012 KB |
Output is correct |
42 |
Correct |
610 ms |
176284 KB |
Output is correct |
43 |
Correct |
756 ms |
201800 KB |
Output is correct |
44 |
Correct |
993 ms |
291668 KB |
Output is correct |
45 |
Correct |
339 ms |
105952 KB |
Output is correct |
46 |
Correct |
453 ms |
171420 KB |
Output is correct |
47 |
Correct |
560 ms |
213656 KB |
Output is correct |
48 |
Correct |
634 ms |
304524 KB |
Output is correct |