結果
問題 | No.1641 Tree Xor Query |
ユーザー |
|
提出日時 | 2024-11-13 21:24:18 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 158 ms / 5,000 ms |
コード長 | 2,104 bytes |
コンパイル時間 | 3,350 ms |
コンパイル使用メモリ | 254,600 KB |
実行使用メモリ | 20,956 KB |
最終ジャッジ日時 | 2024-11-13 21:24:25 |
合計ジャッジ時間 | 5,682 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 18 |
ソースコード
#include <bits/stdc++.h>using namespace std;using ll = long long;#define rep(i, s, e) for (int i = (int)s; i < (int)e; ++i)#define all(a) (a).begin(),(a).end()template<typename T>struct BIT {vector<T> data;int siz;BIT(int n) {siz = n;data.assign(siz + 1, 0);}BIT(int n, const vector<T> &v) : BIT(n) {assert(n == (int)v.size());for (int i = 1; i <= siz; ++i) data[i] = v[i - 1];for (int i = 1; i <= siz; ++i) {int j = i + (i & -i);if (j <= siz) data[j] ^= data[i];}}void apply(int i, const T &x) {for (int k = i + 1; k <= siz; k += k & -k) data[k] ^= x;}T sum(int a) {T res = 0;for (int k = a + 1; k > 0; k -= k & -k) res ^= data[k];return res;}T sum(int a, int b) {return (sum(b) ^ sum(a));}};void dfs(int N, int v, vector<vector<int>> &G, vector<bool> &seen, vector<int> &tour) {seen[v] = true;tour.push_back(v);for (int next : G[v]) {if (seen[next]) continue;dfs(N, next, G, seen, tour);}tour.push_back(v + N);}int main() {cin.tie(nullptr);int N, Q;cin >> N >> Q;vector<int> C(N);rep(i, 0, N) cin >> C[i];vector<vector<int>> G(N, vector<int>());rep(i, 0, N - 1) {int a, b;cin >> a >> b;a--, b--;G[a].push_back(b);G[b].push_back(a);}vector<int> tour;vector<bool> seen(N, false);dfs(N, 0, G, seen, tour);vector<int> tmp, in(N), out(N);rep(i, 0, 2 * N) {if (tour[i] < N) {in[tour[i]] = tmp.size();tmp.push_back(C[tour[i]]);} else {out[tour[i] - N] = tmp.size() - 1;}}BIT<int> bit(N, tmp);rep(query, 0, Q) {int T, x, y;cin >> T >> x >> y;if (T == 1) {bit.apply(in[x - 1], y);} else {cout << bit.sum(in[x - 1] - 1, out[x - 1]) << '\n';}}}