結果
問題 | No.1641 Tree Xor Query |
ユーザー | siro53 |
提出日時 | 2021-08-06 22:32:26 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 234 ms / 5,000 ms |
コード長 | 4,666 bytes |
コンパイル時間 | 2,546 ms |
コンパイル使用メモリ | 207,572 KB |
最終ジャッジ日時 | 2025-01-23 15:53:31 |
ジャッジサーバーID (参考情報) |
judge2 / judge6 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 18 |
ソースコード
#pragma region Macros #include <bits/stdc++.h> using namespace std; template <class T> inline bool chmax(T &a, T b) { if(a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if(a > b) { a = b; return 1; } return 0; } #ifdef DEBUG template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << '(' << p.first << ',' << p.second << ')'; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << '{'; for(int i = 0; i < (int)v.size(); i++) { if(i) { os << ','; } os << v[i]; } os << '}'; return os; } void debugg() { cerr << endl; } template <class T, class... Args> void debugg(const T &x, const Args &... args) { cerr << " " << x; debugg(args...); } #define debug(...) \ cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__) #define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl #else #define debug(...) (void(0)) #define dump(x) (void(0)) #endif struct Setup { Setup() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } __Setup; using ll = long long; #define ALL(v) (v).begin(), (v).end() #define RALL(v) (v).rbegin(), (v).rend() #define FOR(i, a, b) for(int i = (a); i < int(b); i++) #define REP(i, n) FOR(i, 0, n) const int INF = 1 << 30; const ll LLINF = 1LL << 60; constexpr int MOD = 1000000007; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; void Case(int i) { cout << "Case #" << i << ": "; } int popcount(int x) { return __builtin_popcount(x); } ll popcount(ll x) { return __builtin_popcountll(x); } #pragma endregion Macros class heavy_light_decomposition { const int n; vector<vector<int>> g; vector<int> in, out, size, head, par; int it; void erase_par(int v, int prev) { par[v] = prev; for (auto& u : g[v]) { if (u == g[v].back()) break; if (u == prev) swap(u, g[v].back()); erase_par(u, v); } g[v].pop_back(); } void dfs1(int v) { for (auto& u : g[v]) { dfs1(u); size[v] += size[u]; if (size[u] > size[g[v][0]]) swap(u, g[v][0]); } } void dfs2(int v) { in[v] = it++; for (auto u : g[v]) { head[u] = (u == g[v][0] ? head[v] : u); dfs2(u); } out[v] = it; } public: heavy_light_decomposition(int n_) : n(n_), g(n), in(n, -1), out(n, -1), size(n, 1), head(n), par(n, -1), it(0) {} heavy_light_decomposition(const vector<vector<int>>& G) : n(G.size()), g(G), in(n, -1), out(n, -1), size(n, 1), head(n), par(n, -1), it(0) {} void add_edge(int u, int v) { g[u].push_back(v); g[v].push_back(u); } void build(int rt = 0) { for (auto v : g[rt]) erase_par(v, rt); dfs1(rt); head[rt] = rt; dfs2(rt); } int get_id(int v) { return in[v]; } int get_head(int v) { return head[v]; } int get_lca(int u, int v) { while (true) { if (in[u] > in[v]) swap(u, v); if (head[u] == head[v]) return u; v = par[head[v]]; } } void path_query(int u, int v, function<void(int, int)> f) { while (true) { if (in[u] > in[v]) swap(u, v); f(max(in[head[v]], in[u]), in[v] + 1); if (head[u] == head[v]) return; v = par[head[v]]; } } void subtree_query(int v, function<void(int, int)> f) { f(in[v], out[v]); } }; #include <atcoder/segtree> int e() { return 0; } int op(int a, int b) { return a^b; } using segtree = atcoder::segtree<int, op, e>; int main() { int N, Q; cin >> N >> Q; vector<int> c(N); REP(i, N) cin >> c[i]; heavy_light_decomposition hld(N); REP(i, N-1) { int a, b; cin >> a >> b; a--; b--; hld.add_edge(a, b); } hld.build(); segtree seg(N); REP(i, N) seg.set(hld.get_id(i), c[i]); while(Q--) { int t, x, y; cin >> t >> x >> y; x--; if(t == 1) { int id = hld.get_id(x); int tmp = seg.get(id); seg.set(id, tmp ^ y); } else { int res = 0; auto f = [&](int l, int r) -> void { res ^= seg.prod(l, r); }; hld.subtree_query(x, f); cout << res << endl; } } }