結果
問題 | No.1641 Tree Xor Query |
ユーザー | magsta |
提出日時 | 2021-07-09 08:59:12 |
言語 | C++14 (gcc 13.2.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 326 ms / 5,000 ms |
コード長 | 2,428 bytes |
コンパイル時間 | 1,795 ms |
コンパイル使用メモリ | 178,008 KB |
実行使用メモリ | 23,588 KB |
最終ジャッジ日時 | 2023-10-17 02:25:38 |
合計ジャッジ時間 | 3,868 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge14 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
4,348 KB |
testcase_01 | AC | 2 ms
4,348 KB |
testcase_02 | AC | 1 ms
4,348 KB |
testcase_03 | AC | 2 ms
4,348 KB |
testcase_04 | AC | 2 ms
4,348 KB |
testcase_05 | AC | 2 ms
4,348 KB |
testcase_06 | AC | 2 ms
4,348 KB |
testcase_07 | AC | 2 ms
4,348 KB |
testcase_08 | AC | 2 ms
4,348 KB |
testcase_09 | AC | 2 ms
4,348 KB |
testcase_10 | AC | 2 ms
4,348 KB |
testcase_11 | AC | 1 ms
4,348 KB |
testcase_12 | AC | 2 ms
4,348 KB |
testcase_13 | AC | 326 ms
23,588 KB |
testcase_14 | AC | 323 ms
23,588 KB |
testcase_15 | AC | 6 ms
4,348 KB |
testcase_16 | AC | 18 ms
4,384 KB |
testcase_17 | AC | 13 ms
4,348 KB |
testcase_18 | AC | 10 ms
4,384 KB |
testcase_19 | AC | 10 ms
4,348 KB |
testcase_20 | AC | 216 ms
15,484 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; struct Edge { int u; int v; int cost; Edge(int a, int b, int c) : u(a), v(b), cost(c) { } }; using Graph = vector<vector<Edge>>; vector<int> depth; vector<int> subtree_size; vector<int> num, num2; int cou = 0; void dfs(const Graph& G, int v, int p, int d) { depth[v] = d; num[v] = cou; cou++; for (auto nv : G[v]) { if (nv.v == p) continue; dfs(G, nv.v, v, d + 1); } num2[v] = cou; subtree_size[v] = 1; for (auto c : G[v]) { if (c.v == p) continue; subtree_size[v] += subtree_size[c.v]; } } template <typename T> struct SegTree { const T INF = numeric_limits<T>::max(); int n; vector<T> dat; SegTree(int n_) : dat(n_ * 4, INF) { int x = 1; while (n_ > x) { x *= 2; } n = x; } void init(T x) { for (int i = 0; i < 2 * n - 1; i++) dat[i] = x; } void update(int i, T x) { i += n - 1; dat[i] = x ^ dat[i]; while (i > 0) { i = (i - 1) / 2; dat[i] = dat[i * 2 + 1] ^ dat[i * 2 + 2]; } } T query(int a, int b) { return query_sub(a, b, 0, 0, n); } T query_sub(int a, int b, int k, int l, int r) { if (r <= a || b <= l) { return 0; } else if (a <= l && r <= b) { return dat[k]; } else { T vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2); T vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r); return vl ^ vr; } } }; int main() { long long N, Q; cin >> N >> Q; vector<long long> C(N); for (int i = 0; i < N; i++) cin >> C[i]; Graph G(N); for (int i = 0; i < N - 1; i++) { int a, b; cin >> a >> b; G[a - 1].emplace_back(a - 1, b - 1, 1); G[b - 1].emplace_back(b - 1, a - 1, 1); } depth.assign(N, 0); subtree_size.assign(N, 0); num.assign(N, 0); num2.assign(N, 0); dfs(G, 0, -1, 0); SegTree<long long> seg(N); seg.init(0); for (int i = 0; i < N; i++) { seg.update(num[i], C[i]); } for (int i = 0; i < Q; i++) { int T, x, y; cin >> T >> x >> y; if (T == 1) { seg.update(num[x - 1], y); } else { cout << seg.query(num[x - 1], num2[x - 1]) << endl; } } }