結果

問題 No.1641 Tree Xor Query
ユーザー convexineqconvexineq
提出日時 2021-08-06 22:19:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 255 ms / 5,000 ms
コード長 2,477 bytes
コンパイル時間 352 ms
コンパイル使用メモリ 81,688 KB
実行使用メモリ 109,704 KB
最終ジャッジ日時 2023-10-17 03:44:00
合計ジャッジ時間 3,115 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,456 KB
testcase_01 AC 36 ms
53,456 KB
testcase_02 AC 37 ms
53,456 KB
testcase_03 AC 37 ms
53,456 KB
testcase_04 AC 37 ms
53,456 KB
testcase_05 AC 39 ms
53,456 KB
testcase_06 AC 38 ms
53,456 KB
testcase_07 AC 37 ms
53,456 KB
testcase_08 AC 37 ms
53,456 KB
testcase_09 AC 38 ms
53,456 KB
testcase_10 AC 38 ms
53,456 KB
testcase_11 AC 36 ms
53,456 KB
testcase_12 AC 37 ms
53,456 KB
testcase_13 AC 255 ms
101,812 KB
testcase_14 AC 255 ms
101,888 KB
testcase_15 AC 84 ms
76,652 KB
testcase_16 AC 132 ms
78,408 KB
testcase_17 AC 122 ms
77,616 KB
testcase_18 AC 88 ms
77,336 KB
testcase_19 AC 103 ms
76,452 KB
testcase_20 AC 225 ms
109,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class segment_tree:
    __slots__ = ["op_M", "e_M","N","N0","dat"]
    def __init__(self, N, operator_M, e_M):
        self.op_M = operator_M
        self.e_M = e_M
        self.N = N
        self.N0 = 1<<(N-1).bit_length()
        self.dat = [self.e_M]*(2*self.N0)
    
    # 長さNの配列 initial で初期化
    def build(self, initial):
        assert self.N == len(initial)
        self.dat[self.N0:self.N0+len(initial)] = initial[:]
        for k in range(self.N0-1,0,-1):
            self.dat[k] = self.op_M(self.dat[2*k], self.dat[2*k+1])

    # a_k の値を x に更新
    def update(self,k,x):
        k += self.N0
        self.dat[k] = x
        k >>= 1
        while k:
            self.dat[k] = self.op_M(self.dat[2*k], self.dat[2*k+1])
            k >>= 1

    # 区間[L,R]をopでまとめる
    def query(self,L,R):
        L += self.N0; R += self.N0 + 1 
        sl = sr = self.e_M
        while L < R:
            if R & 1:
                R -= 1
                sr = self.op_M(self.dat[R],sr)
            if L & 1:
                sl = self.op_M(sl,self.dat[L])
                L += 1
            L >>= 1; R >>= 1
        return self.op_M(sl,sr)

    def get(self, k): #k番目の値を取得。query[k,k]と同じ
        return self.dat[k+self.N0]

def Euler_tour_vertex(g,root):
    n = len(g)
    parent = [-1]*n
    ls = [0]*n
    rs = [-1]*n
    q = [root]
    order = []
    for cnt in range(n):
        v = q.pop()
        order.append(v)
        ls[v] = cnt
        for c in g[v]:
            if c != parent[v]:
                parent[c] = v
                q.append(c)

    for i in order[n:0:-1]:
        if rs[i] == -1:
            rs[i] = ls[i]+1
        if rs[parent[i]] < rs[i]:
            rs[parent[i]] = rs[i]
    return ls,rs,order

import sys
readline = sys.stdin.readline

n,Q = map(int,readline().split())
*C, = map(int,readline().split())
g = [[] for _ in range(n)]
for i in range(n-1):
    a,b = map(int,readline().split())
    g[a-1].append(b-1)
    g[b-1].append(a-1)

ls,rs,order = Euler_tour_vertex(g,0)
cost = [0]*n
pos = [0]*n
for i in range(n):
    cost[i] = C[order[i]]
    pos[order[i]] = i

seg = segment_tree(n, lambda x,y:x^y, 0)
seg.build(cost)
#v の部分木に属する頂点は [ls[v],rs[v]) までの頂点
for _ in range(Q):
    t,x,y = map(int,readline().split())
    x -= 1
    p = pos[x]
    if t==1:
        seg.update(p,seg.get(p)^y)
    else:
        ans = seg.query(ls[x],rs[x]-1)
        print(ans)
0