結果

問題 No.1641 Tree Xor Query
ユーザー yakkiyakki
提出日時 2021-08-06 22:41:11
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 308 ms / 5,000 ms
コード長 2,730 bytes
コンパイル時間 1,362 ms
コンパイル使用メモリ 128,892 KB
実行使用メモリ 15,688 KB
最終ジャッジ日時 2023-10-17 04:08:41
合計ジャッジ時間 3,070 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 303 ms
15,688 KB
testcase_14 AC 308 ms
15,688 KB
testcase_15 AC 6 ms
4,348 KB
testcase_16 AC 17 ms
4,348 KB
testcase_17 AC 12 ms
4,348 KB
testcase_18 AC 9 ms
4,348 KB
testcase_19 AC 9 ms
4,348 KB
testcase_20 AC 209 ms
11,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<bitset>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<functional>
#include<cstdio>
#include<cstdlib>
#include<numeric>
#include<ctime>
//#include<atcoder/all>
using namespace std;
//using namespace atcoder;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
//#define MOD 1000000007
#define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592
#define PI acos(-1.0)

const double EPS = 1e-10;

using ll = long long;
using Pi = pair<int,int>;
using Pl = pair<ll,ll>;
//using mint = modint1000000007;

int dh[] = {-1,0,1,0};
int dw[] = {0,1,0,-1};

template<typename Monoid>
struct SegmentTree{
    int n;
    vector<Monoid> node;

    SegmentTree(vector<Monoid> v){
        int k = v.size();
        n = 1; while(n < k) n *= 2;
        node.resize(2*n-1,0); //単位元で初期化
        for(int i = 0; i < k; i++) node[i+n-1] = v[i];
        for(int i = n-2; i >= 0; i--) node[i] = node[2*i+1]^node[2*i+2]; //書き換える
    }

    SegmentTree(int k){
        n = 1; while(n < k) n *= 2;
        node.resize(2*n-1,0); //書き換える
    }

    void update(int k, Monoid x){
        k += n-1;
        node[k] = x;
        while(k > 0){
            k = (k-1)/2;
            node[k] = node[2*k+1]^node[2*k+2]; //書き換える
        }
    }

    //[a,b)のクエリに答える
    Monoid query(int a, int b, int k = 0, int l = 0, int r = -1){
        if(r < 0) r = n;
        if(b <= l || a >= r) return 0;
        if(a <= l && b >= r) return node[k];
        else{
            Monoid s = query(a,b,2*k+1,l,(l+r)/2);
            Monoid t = query(a,b,2*k+2,(l+r)/2,r);
            return s^t; //書き換える
        }
    }
};


int n,q;
vector<vector<int>> G;
vector<int> c;
int in[100010];
int out[100010];
int counter = 0;

void dfs(int u, int p = -1){
    in[u] = counter++;
    for(auto v : G[u]){
        if(v == p) continue;
        dfs(v,u);
    }
    out[u] = counter;
}

int main(){
    cin >> n >> q;
    G.resize(n);
    c.resize(n);
    rep(i,n) cin >> c[i];
    rep(i,n-1){
        int a,b; cin >> a >> b;
        a--; b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    dfs(0);

    SegmentTree<int> seg(n);
    rep(i,n){
        seg.update(in[i],c[i]);
    }
    while(q--){
        int t,x,y; cin >> t >> x >> y;
        x--;
        if(t == 1){
            int u = seg.query(in[x],in[x]+1);
            seg.update(in[x],u^y);
        }
        else{
            cout << seg.query(in[x],out[x]) << endl;
        }
    }
}

0