#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "C:\GCC\debug.h"
#else
#define debug(...) void(42)
#endif
const int lg = 31;
struct Trie {
public:
vector<vector<int>> trie;
Trie() {
trie.push_back(vector<int> (2, -1));
};
void add(int x) {
int t = 0;
for (int i = lg - 1; i >= 0; i--) {
int bit = (x >> i) & 1;
if (trie[t][bit] == -1) {
trie[t][bit] = (int) trie.size();
trie.emplace_back(2, -1);
}
t = trie[t][bit];
}
}
int get(int x) {
int t = 0;
int ret = 0;
for (int i = lg - 1; i >= 0; i--) {
int bit = (x >> i) & 1;
bit ^= 1;
if (trie[t][bit] == -1) {
bit ^= 1;
} else {
ret |= (1 << i);
}
if (trie[t][bit] == -1) {
return 0;
}
t = trie[t][bit];
}
return ret;
};
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int q;
cin >> q;
vector<vector<int>> adj(q + 1);
vector<int> pref(q + 1);
Trie t;
t.add(0);
auto Add = [&](int x, int y, int w) {
adj[x].push_back(y);
pref[y] = pref[x] ^ w;
t.add(pref[y]);
};
int cnt = 1;
for (int i = 0; i < q; i++) {
string type;
cin >> type;
if (type == "Add") {
int x, w;
cin >> x >> w;
--x;
Add(x, cnt, w);
++cnt;
} else {
int a, b;
cin >> a >> b;
--a, --b;
if (b == 0) {
int res = t.get(pref[a]);
res ^= pref[a];
cout << res << '\n';
} else {
int res = 0;
function<void(int, bool)> Dfs = [&](int u, bool insideSubtree) {
bool isOk = insideSubtree;
if (u == b) {
isOk = true;
}
if (isOk) {
res = max(res, pref[a] ^ pref[u]);
}
for (auto v : adj[u]) {
Dfs(v, isOk);
}
};
Dfs(0, false);
cout << res << '\n';
}
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
194 ms |
49948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |