結果

問題 No.1641 Tree Xor Query
ユーザー ゆきのんゆきのん
提出日時 2021-08-06 23:10:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 279 ms / 5,000 ms
コード長 2,936 bytes
コンパイル時間 2,363 ms
コンパイル使用メモリ 211,584 KB
実行使用メモリ 25,664 KB
最終ジャッジ日時 2023-10-17 04:41:55
合計ジャッジ時間 3,618 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,304 KB
testcase_01 AC 3 ms
7,304 KB
testcase_02 AC 3 ms
7,304 KB
testcase_03 AC 3 ms
7,304 KB
testcase_04 AC 3 ms
7,304 KB
testcase_05 AC 3 ms
7,304 KB
testcase_06 AC 4 ms
7,304 KB
testcase_07 AC 4 ms
7,304 KB
testcase_08 AC 3 ms
7,304 KB
testcase_09 AC 4 ms
7,304 KB
testcase_10 AC 4 ms
7,304 KB
testcase_11 AC 4 ms
7,304 KB
testcase_12 AC 4 ms
7,304 KB
testcase_13 AC 279 ms
25,664 KB
testcase_14 AC 279 ms
25,664 KB
testcase_15 AC 8 ms
7,836 KB
testcase_16 AC 17 ms
8,632 KB
testcase_17 AC 13 ms
8,072 KB
testcase_18 AC 11 ms
8,376 KB
testcase_19 AC 10 ms
7,664 KB
testcase_20 AC 190 ms
18,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
//#include<atcoder/all>
//#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
//using namespace atcoder;
//using namespace boost::multiprecision;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<ll,ll> P;
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; }
template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const ll mod=1e9 + 7;
const ll LINF=1ll<<60;
const int INF=1<<30;
//int dx[]={0,1,0,-1,0,1,-1,1,-1};
//int dy[]={0,0,1,0,-1,1,-1,-1,1};
int dx[]={0,0,1,-1};
int dy[]={-1,1,0,0};

vector<vector<int>> g(1<<17); //グラフの隣接リスト
vector<int> v; //オイラーツアーの頂点配列
vector<int> ind(1<<17), out(1<<17); //各頂点がオイラーツアー配列の何番目に最初に訪れるか

void dfs(int now, int par){//今の頂点、親の頂点
    ind[now] = v.size();
    v.push_back(now);
    for(int i = 0; i < g[now].size(); i++){
        if(g[now][i] != par){
            dfs(g[now][i],now);
            v.push_back(now);
        }
    }
    out[now] = v.size();
}

template <typename T>
struct SegmentTree{
    using F = function<T(T,T)>;
    int n;
    F f;
    T ti;
    vector<T> dat;
    SegmentTree(){};
    SegmentTree(F f,T ti):f(f),ti(ti){}
    void init(int n_){    
        n=1;
        while(n<n_) n<<=1;
        dat.assign(n<<1,ti);
    }
    void build(const vector<T> &v){
        int n_=v.size();
        init(n_);
        for(int i=0;i<n_;i++) dat[n+i]=v[i];
        for(int i=n-1;i;i--)
            dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
    }
    void set_val(int k,T x){
        dat[k+=n]=x;
        while(k>>=1)
            dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);    
    }
    T query(int a,int b){
        T vl=ti,vr=ti;
        for(int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {
            if(l&1) vl=f(vl,dat[l++]);
            if(r&1) vr=f(dat[--r],vr);
        }
        return f(vl,vr);
    }
};



int main(){
    int n,q;cin >> n >> q;
    vector<ll> c(n);
    for (int i = 0; i < n; i++) {
        cin >> c[i];
    }
    for (int i = 0; i < n-1; i++) {
        int x,y;cin >> x >> y;
        x--,y--;
        g[x].pb(y);
        g[y].pb(x);
    }
    dfs(0, 0);
    SegmentTree<ll> seg([](ll a, ll b){
        return a ^ b;
    }, 0ll);
    seg.build(vector<ll> (2 * n, 0));
    for (int i = 0; i < n; i++) {
        seg.set_val(ind[i], c[i]);
    }
    
    while(q--){
        int t,x,y;cin >> t >> x >> y;
        x--;
        if(t == 1){
            ll t = seg.query(ind[x], ind[x] + 1);
            seg.set_val(ind[x], t ^ y);
        }
        else{
            cout << seg.query(ind[x], out[x] + 1) << endl;
        }
    }
    return 0;
}
0