結果

問題 No.1641 Tree Xor Query
ユーザー sotanishysotanishy
提出日時 2021-08-06 22:18:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 251 ms / 5,000 ms
コード長 1,418 bytes
コンパイル時間 164 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 112,540 KB
最終ジャッジ日時 2023-10-17 03:42:14
合計ジャッジ時間 3,310 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,460 KB
testcase_01 AC 35 ms
53,460 KB
testcase_02 AC 35 ms
53,460 KB
testcase_03 AC 36 ms
53,460 KB
testcase_04 AC 36 ms
53,460 KB
testcase_05 AC 37 ms
53,460 KB
testcase_06 AC 37 ms
53,460 KB
testcase_07 AC 36 ms
53,460 KB
testcase_08 AC 35 ms
53,460 KB
testcase_09 AC 35 ms
53,460 KB
testcase_10 AC 36 ms
53,460 KB
testcase_11 AC 38 ms
53,460 KB
testcase_12 AC 36 ms
53,460 KB
testcase_13 AC 251 ms
109,472 KB
testcase_14 AC 237 ms
109,472 KB
testcase_15 AC 78 ms
76,464 KB
testcase_16 AC 104 ms
77,620 KB
testcase_17 AC 100 ms
76,932 KB
testcase_18 AC 91 ms
77,264 KB
testcase_19 AC 77 ms
76,008 KB
testcase_20 AC 225 ms
112,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class FenwickTree:
    def __init__(self, size):
        self.data = [0] * (size + 1)
        self.size = size

    # i is exclusive
    def prefix_sum(self, i):
        s = 0
        while i > 0:
            s ^= self.data[i]
            i -= i & -i
        return s

    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.data[i] ^= x
            i += i & -i

def euler_tour(G, root):
    N = len(G)
    stack = [(root, -1, 1), (root, -1, 0)]
    et = []
    first = [-1] * N
    last = [-1] * N
    k = 0
    while stack:
        v, p, t = stack.pop()
        if t == 0:
            et.append(v)
            first[v] = k
            k += 1
            for c in G[v]:
                if c != p:
                    stack.append((c, v, 1))
                    stack.append((c, v, 0))
        else:
            last[v] = k
    return et, first, last

N, Q = map(int, input().split())
C = list(map(int, input().split()))
G = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(lambda x: int(x) - 1, input().split())
    G[a].append(b)
    G[b].append(a)
et, first, last = euler_tour(G, 0)
ft = FenwickTree(N)
for i in range(N):
    ft.add(first[i], C[i])
for _ in range(Q):
    T, x, y = map(int, input().split())
    x -= 1
    if T == 1:
        ft.add(first[x], y)
    else:
        print(ft.prefix_sum(last[x]) ^ ft.prefix_sum(first[x]))
0