結果

問題 No.1641 Tree Xor Query
ユーザー 小高悠太郎
提出日時 2025-09-05 12:56:22
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 361 ms / 5,000 ms
コード長 3,924 bytes
コンパイル時間 3,900 ms
コンパイル使用メモリ 299,560 KB
実行使用メモリ 26,568 KB
最終ジャッジ日時 2025-09-05 12:56:29
合計ジャッジ時間 5,976 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i< (n); ++i)
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define all(x) (x).begin(), (x).end()
#define fore(i, a) for(auto &i:a)
using ll = long long;
#include<atcoder/lazysegtree>
using namespace atcoder;

class HLdecomposition{
private:
    int V;
    vector<vector<int> > G;
    vector<int> stsize, parent, pathtop, in, out;
    int root;
    void BuildStsize(int u, int p){
        stsize[u] = 1, parent[u] = p;
        for(int& v : G[u]){
            if(v == p){
                if(v == G[u].back()) break;
                else swap(v, G[u].back());
            }
            BuildStsize(v, u);
            stsize[u] += stsize[v];
            if(stsize[v] > stsize[G[u][0]]){
                swap(v, G[u][0]);
            }
        }
    }
    void BuildPath(int u, int p, int& tm){
        in[u] = tm++;
        for(int v : G[u]){
            if(v == p) continue;
            pathtop[v] = (v == G[u][0] ? pathtop[u] : v);
            BuildPath(v, u, tm);
        }
        out[u] = tm;
    }
 
public:
		int getter(int u){
			return in[u];
		}
    void add_edge(int u, int v){
        G[u].push_back(v), G[v].push_back(u);
    }
    void build(int _root=0){
        root = _root;
        int tm = 0;
        BuildStsize(root, -1);
        pathtop[root] = root;
        BuildPath(root, -1, tm);
    }
    //元の頂点のインデックスの配列上でのidを返す
    inline int get(int a){
        return in[a];
    }
    int lca(int a, int b){
        int pa = pathtop[a], pb = pathtop[b];
        while(pathtop[a] != pathtop[b]){
            if(in[pa] > in[pb]){
                a = parent[pa], pa = pathtop[a];
            }else{
                b = parent[pb], pb = pathtop[b];
            }
        }
        if(in[a] > in[b]) swap(a, b);
        return a;
    }
    void subtree_query(int a, const function< void(int, int) > &func){
        func(in[a], out[a]);
    }
    // 例: hl.path_query(q, r, [&](int l, int r){ seg.range(l, r, s); })
    // 例: hl.query(q, r, [&](int l, int r){ ans += seg.query(l, r); })
    void path_query(int a, int b, const function< void(int, int) > &func){
        int pa = pathtop[a], pb = pathtop[b];
        while(pathtop[a] != pathtop[b]){
            if(in[pa] > in[pb]){
                func(in[pa], in[a] + 1);
                a = parent[pa], pa = pathtop[a];
            }else{
                func(in[pb], in[b] + 1);
                b = parent[pb], pb = pathtop[b];
            }
        }
        if(in[a] > in[b]) swap(a, b);
        func(in[a], in[b] + 1);
    }
    HLdecomposition(int node_size) : V(node_size), G(V), stsize(V, 0), parent(V, -1),
        pathtop(V, -1), in(V, -1), out(V, -1){}
};
struct S{
	ll value;
};
S op(S a, S b){
	return S{a.value^b.value};
}
S e(){
	return S{0};
}
struct F{
	ll value;
};
S mapping(F f, S x){
	return S{x.value^f.value};
}
F composition(F f, F g){
	return F{f.value^g.value};
}
F id(){
	return F{0};
}
int main() {
	ll n, q;
	cin >> n >> q;
	vector<ll> c(n);
	rep(i, n){
		cin >> c[i];
	}
	HLdecomposition HLD(n);
	rep(i, n-1){
		ll a, b;
		cin >> a >> b;
		a--;b--;
		HLD.add_edge(a, b);
	}
  HLD.build();
	vector<S> vec(2*n, S{0});
	rep(i, n){
		vec[HLD.get(i)] = S{c[i]};
	}
	lazy_segtree<S, op, e, F, mapping, composition, id> seg(vec);
	vector<tuple<ll,ll,ll>> qs(q);
	rep(i, q){
		ll t, x, y;
		cin >> t >> x >> y;
		x--;
		qs[i] = {t, x, y};
	}
	rep(i, q){
		auto[t, x, y] = qs[i];
		if(t == 1){
			auto ff = [&](int l, int r){
				seg.apply(l, r, F{y});
			};
			HLD.path_query(x, x, ff);
		}
		else{
			ll ans = 0;
			auto ff = [&](int l, int r){
        ans = seg.prod(l, r).value;
			};
			HLD.subtree_query(x, ff);
			cout << ans << endl;
		}
	}
	

	
}
0