#include <bits/stdc++.h>
using std::vector;
using std::array;
using std::pair;
using std::tuple;
constexpr int LOG = 30;
struct Trie {
struct Node {
array<int, 2> child;
Node() { child.fill(-1); }
};
static inline vector<Node> node;
static int add_node() {
node.emplace_back();
return node.size() - 1;
}
int root;
Trie() : root(-1) {}
void insert(const int x) {
if (root == -1) {
root = add_node();
}
int u = root;
for (int d = LOG - 1; d >= 0; --d) {
const int k = x >> d & 1;
if (node[u].child[k] == -1) {
node[u].child[k] = add_node();
}
u = node[u].child[k];
}
}
int max(const int x) const {
if (root == -1) {
return 0;
}
int u = root, ret = 0;
for (int d = LOG - 1; d >= 0; --d) {
const int k = x >> d & 1;
if (node[u].child[k ^ 1] == -1) {
u = node[u].child[k];
} else {
u = node[u].child[k ^ 1];
ret |= 1 << d;
}
}
return ret;
}
};
struct SegTree {
int size;
vector<Trie> data;
SegTree(const int n) : size(n), data(2 * n, Trie()) {}
void add(int k, const int x) {
k += size;
while (k > 0) {
data[k].insert(x);
k >>= 1;
}
}
int max(int l, int r, const int x) const {
int ret = 0;
l += size, r += size;
while (l < r) {
if (l & 1) ret = std::max(ret, data[l++].max(x));
if (r & 1) ret = std::max(ret, data[--r].max(x));
l >>= 1, r >>= 1;
}
return ret;
}
};
template <class F> struct RecLambda : private F {
explicit RecLambda(F&& f) : F(std::forward<F>(f)) {}
template <class... Args> decltype(auto) operator()(Args&&... args) const {
return F::operator()(*this, std::forward<Args>(args)...);
}
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int Q;
std::cin >> Q;
int N = 1;
vector<int> value(N);
vector<vector<int>> graph(N);
vector<pair<int, int>> query(Q);
for (auto& [a, b] : query) {
std::string s;
std::cin >> s;
if (s == "Add") {
int x, y;
std::cin >> x >> y;
const int u = N++;
value.push_back(y);
graph.emplace_back();
graph[x - 1].push_back(u);
a = u, b = -1;
} else {
std::cin >> a >> b;
a -= 1, b -= 1;
}
}
vector<int> in(N), out(N);
int stamp = 0;
RecLambda([&](auto&& dfs, const int u, const int p) -> void {
in[u] = stamp++;
value[u] ^= value[p];
for (const int v : graph[u]) {
dfs(v, u);
}
out[u] = stamp;
})(0, 0);
SegTree seg(N);
seg.add(in[0], value[0]);
for (const auto& [a, b] : query) {
if (b == -1) {
seg.add(in[a], value[a]);
} else {
std::cout << seg.max(in[b], out[b], value[a]) << '\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
436 KB |
Output is correct |
3 |
Correct |
1 ms |
700 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
564 KB |
Output is correct |
7 |
Correct |
1 ms |
676 KB |
Output is correct |
8 |
Correct |
1 ms |
932 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
696 KB |
Output is correct |
11 |
Correct |
1 ms |
696 KB |
Output is correct |
12 |
Correct |
1 ms |
976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
436 KB |
Output is correct |
3 |
Correct |
1 ms |
700 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
564 KB |
Output is correct |
7 |
Correct |
1 ms |
676 KB |
Output is correct |
8 |
Correct |
1 ms |
932 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
696 KB |
Output is correct |
11 |
Correct |
1 ms |
696 KB |
Output is correct |
12 |
Correct |
1 ms |
976 KB |
Output is correct |
13 |
Correct |
4 ms |
1476 KB |
Output is correct |
14 |
Correct |
5 ms |
2608 KB |
Output is correct |
15 |
Correct |
7 ms |
4708 KB |
Output is correct |
16 |
Correct |
7 ms |
4740 KB |
Output is correct |
17 |
Correct |
3 ms |
1472 KB |
Output is correct |
18 |
Correct |
4 ms |
2624 KB |
Output is correct |
19 |
Correct |
7 ms |
4576 KB |
Output is correct |
20 |
Correct |
8 ms |
4680 KB |
Output is correct |
21 |
Correct |
4 ms |
1476 KB |
Output is correct |
22 |
Correct |
5 ms |
2624 KB |
Output is correct |
23 |
Correct |
7 ms |
4604 KB |
Output is correct |
24 |
Correct |
8 ms |
4680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
589 ms |
142492 KB |
Output is correct |
2 |
Correct |
835 ms |
279348 KB |
Output is correct |
3 |
Runtime error |
901 ms |
524292 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
436 KB |
Output is correct |
3 |
Correct |
1 ms |
700 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
564 KB |
Output is correct |
7 |
Correct |
1 ms |
676 KB |
Output is correct |
8 |
Correct |
1 ms |
932 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
696 KB |
Output is correct |
11 |
Correct |
1 ms |
696 KB |
Output is correct |
12 |
Correct |
1 ms |
976 KB |
Output is correct |
13 |
Correct |
4 ms |
1476 KB |
Output is correct |
14 |
Correct |
5 ms |
2608 KB |
Output is correct |
15 |
Correct |
7 ms |
4708 KB |
Output is correct |
16 |
Correct |
7 ms |
4740 KB |
Output is correct |
17 |
Correct |
3 ms |
1472 KB |
Output is correct |
18 |
Correct |
4 ms |
2624 KB |
Output is correct |
19 |
Correct |
7 ms |
4576 KB |
Output is correct |
20 |
Correct |
8 ms |
4680 KB |
Output is correct |
21 |
Correct |
4 ms |
1476 KB |
Output is correct |
22 |
Correct |
5 ms |
2624 KB |
Output is correct |
23 |
Correct |
7 ms |
4604 KB |
Output is correct |
24 |
Correct |
8 ms |
4680 KB |
Output is correct |
25 |
Correct |
589 ms |
142492 KB |
Output is correct |
26 |
Correct |
835 ms |
279348 KB |
Output is correct |
27 |
Runtime error |
901 ms |
524292 KB |
Execution killed with signal 9 |
28 |
Halted |
0 ms |
0 KB |
- |