結果

問題 No.1641 Tree Xor Query
ユーザー H3PO4H3PO4
提出日時 2024-04-18 18:51:01
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,470 ms / 5,000 ms
コード長 2,274 bytes
コンパイル時間 293 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 63,252 KB
最終ジャッジ日時 2024-04-18 18:51:11
合計ジャッジ時間 6,228 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,752 KB
testcase_01 AC 30 ms
10,880 KB
testcase_02 AC 29 ms
10,880 KB
testcase_03 AC 29 ms
10,880 KB
testcase_04 AC 30 ms
10,752 KB
testcase_05 AC 30 ms
10,880 KB
testcase_06 AC 30 ms
10,880 KB
testcase_07 AC 30 ms
10,880 KB
testcase_08 AC 29 ms
10,752 KB
testcase_09 AC 31 ms
10,880 KB
testcase_10 AC 31 ms
10,880 KB
testcase_11 AC 29 ms
10,880 KB
testcase_12 AC 28 ms
10,752 KB
testcase_13 AC 1,430 ms
63,120 KB
testcase_14 AC 1,470 ms
63,252 KB
testcase_15 AC 51 ms
12,544 KB
testcase_16 AC 112 ms
15,488 KB
testcase_17 AC 81 ms
13,440 KB
testcase_18 AC 69 ms
14,336 KB
testcase_19 AC 62 ms
11,520 KB
testcase_20 AC 1,405 ms
58,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

input = sys.stdin.buffer.readline

sys.setrecursionlimit(10**9)


class SegmentTree:
    """
    https://qiita.com/dn6049949/items/afa12d5d079f518de368 から拝借しています。
    """

    def __init__(self, size, f=lambda x, y: x ^ y, default=0):
        self.size = 2 ** (size - 1).bit_length()
        self.default = default
        self.dat = [default] * (self.size * 2)
        self.f = f

    def initialize(self, A):
        for i, a in enumerate(A, self.size):
            self.dat[i] = a
        for i in range(self.size - 1, 0, -1):
            self.dat[i] = self.f(self.dat[i * 2], self.dat[i * 2 + 1])

    def update(self, i, x):
        i += self.size
        self.dat[i] = x
        while i > 0:
            i >>= 1
            self.dat[i] = self.f(self.dat[i * 2], self.dat[i * 2 + 1])

    def query(self, l, r):
        """半開区間[l,r)"""
        l += self.size
        r += self.size
        lres, rres = self.default, self.default
        while l < r:
            if l & 1:
                lres = self.f(lres, self.dat[l])
                l += 1
            if r & 1:
                r -= 1
                rres = self.f(self.dat[r], rres)
            l >>= 1
            r >>= 1
        res = self.f(lres, rres)
        return res

    def get(self, i):
        return self.dat[i + self.size]


N, Q = map(int, input().split())
C = list(map(int, input().split()))
T = [[] for _ in range(N)]
for _ in range(N - 1):
    a, b = map(lambda x: int(x) - 1, input().split())
    T[a].append(b)
    T[b].append(a)

# euler tour
tour = []
v_in_tour = [[] for _ in range(N)]


def dfs(v, p):
    v_in_tour[v].append(len(tour))
    tour.append(v)
    for u in T[v]:
        if u == p:
            continue
        dfs(u, v)
    v_in_tour[v].append(len(tour))
    tour.append(v)


dfs(0, -1)

init_c = [0] * (2 * N)
for i, c in enumerate(C):
    init_c[v_in_tour[i][0]] = c


# segment tree
st = SegmentTree(2 * N)
st.initialize(init_c)

for _ in range(Q):
    query = tuple(map(int, input().split()))
    if query[0] == 1:
        x = query[1] - 1
        y = query[2]
        st.update(v_in_tour[x][0], st.get(v_in_tour[x][0]) ^ y)
    elif query[0] == 2:
        x = query[1] - 1
        print(st.query(v_in_tour[x][0], v_in_tour[x][1]))
0