結果

問題 No.1641 Tree Xor Query
ユーザー H3PO4H3PO4
提出日時 2024-04-18 18:49:38
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 2,244 bytes
コンパイル時間 119 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 58,904 KB
最終ジャッジ日時 2024-04-18 18:49:43
合計ジャッジ時間 4,648 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
11,008 KB
testcase_01 AC 27 ms
10,880 KB
testcase_02 AC 29 ms
10,880 KB
testcase_03 AC 28 ms
10,880 KB
testcase_04 AC 27 ms
10,880 KB
testcase_05 AC 27 ms
11,008 KB
testcase_06 AC 29 ms
10,880 KB
testcase_07 AC 26 ms
11,008 KB
testcase_08 AC 27 ms
11,008 KB
testcase_09 AC 27 ms
10,880 KB
testcase_10 AC 27 ms
11,008 KB
testcase_11 AC 29 ms
11,008 KB
testcase_12 AC 27 ms
11,008 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 AC 49 ms
12,672 KB
testcase_16 AC 99 ms
15,488 KB
testcase_17 AC 72 ms
13,568 KB
testcase_18 AC 69 ms
14,592 KB
testcase_19 AC 53 ms
11,520 KB
testcase_20 AC 1,284 ms
58,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

input = sys.stdin.buffer.readline


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