結果

問題 No.1641 Tree Xor Query
ユーザー nephrologistnephrologist
提出日時 2021-08-06 23:09:49
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,893 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 117,424 KB
最終ジャッジ日時 2024-09-17 03:25:10
合計ジャッジ時間 3,033 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 40 ms
52,608 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 40 ms
53,120 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 40 ms
52,608 KB
testcase_13 AC 257 ms
108,304 KB
testcase_14 AC 259 ms
108,112 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

input = sys.stdin.buffer.readline


class SegmentTree:
    # SEG木は1-index
    # Aに関しては0-index

    def __init__(self, n, ele, func):  # Aは0-idx
        self.n = n
        self.ele = ele
        self.func = func
        self.num = 2 ** ((self.n - 1).bit_length())
        self.SEG = [self.ele] * (2 * self.num)

    def search(self, idx):
        return self.SEG[idx + self.num]

    def initialize(self, A):
        for i in range(self.n):
            self.SEG[i + self.num] = A[i]
        for i in range(self.num - 1, 0, -1):
            self.SEG[i] = self.func(self.SEG[2 * i], self.SEG[2 * i + 1])

    # 1点更新
    def set(self, idx, val):
        idx += self.num
        self.SEG[idx] = val
        idx //= 2
        while idx:
            self.SEG[idx] = self.func(self.SEG[2 * idx], self.SEG[2 * idx + 1])
            idx //= 2

    # 区間取得
    def query(self, left, right):
        # 開区間
        resleft = self.ele
        resright = self.ele
        left += self.num
        right += self.num
        while right - left > 0:
            if left % 2 == 1:
                resleft = self.func(resleft, self.SEG[left])
                left += 1
            if right % 2 == 1:
                right -= 1
                resright = self.func(resright, self.SEG[right])
            left //= 2
            right //= 2
        return self.func(resleft, resright)


# 初期条件
def func(a, b):
    return a ^ b


ele = 0

n, q = map(int, input().split())
C = list(map(int, input().split()))
graph = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    graph[a].append(b)
    graph[b].append(a)

idx = 0
# graph and n is necessary
def dfs(start):
    global idx
    par = [-1] * n
    depth = [-1] * n
    size = [0] * n
    stack = []
    stack.append(~start)
    stack.append(start)
    depth[start] = 0
    left = [-1] * n
    right = [-1] * n
    euler = []
    while stack:
        v = stack.pop()
        if v >= 0:
            d = depth[v]
            left[v] = idx
            idx += 1
            euler.append(v)
            for u in graph[v]:
                if par[v] == u:
                    continue
                par[u] = v
                depth[u] = d + 1
                stack.append(~u)
                stack.append(u)
        else:
            a = ~v
            right[a] = idx
            size[a] = 1
            for u in graph[a]:
                if u == par[a]:
                    continue
                size[a] += size[u]
    return left, right, euler


left, right, euler = dfs(0)

moto = [C[i] for i in euler]

ST = SegmentTree(n, ele, func)

ST.initialize(moto)

for _ in range(q):
    t, x, y = map(int, input().split())
    x -= 1
    if t == 1:
        v = ST.search(x)
        ST.set(left[x], v ^ y)
    else:
        print(ST.query(left[x], right[x]))
0