結果

問題 No.1641 Tree Xor Query
ユーザー momoyuumomoyuu
提出日時 2022-08-03 03:01:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 593 ms / 5,000 ms
コード長 3,266 bytes
コンパイル時間 439 ms
コンパイル使用メモリ 86,972 KB
実行使用メモリ 218,620 KB
最終ジャッジ日時 2023-10-01 03:40:49
合計ジャッジ時間 5,142 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,112 KB
testcase_01 AC 78 ms
71,352 KB
testcase_02 AC 75 ms
71,356 KB
testcase_03 AC 75 ms
71,264 KB
testcase_04 AC 77 ms
71,164 KB
testcase_05 AC 73 ms
71,340 KB
testcase_06 AC 73 ms
71,268 KB
testcase_07 AC 73 ms
71,336 KB
testcase_08 AC 73 ms
71,020 KB
testcase_09 AC 72 ms
71,160 KB
testcase_10 AC 74 ms
71,492 KB
testcase_11 AC 72 ms
71,412 KB
testcase_12 AC 73 ms
71,472 KB
testcase_13 AC 593 ms
218,620 KB
testcase_14 AC 576 ms
218,376 KB
testcase_15 AC 127 ms
79,296 KB
testcase_16 AC 174 ms
83,456 KB
testcase_17 AC 172 ms
82,244 KB
testcase_18 AC 141 ms
81,888 KB
testcase_19 AC 146 ms
80,392 KB
testcase_20 AC 358 ms
130,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    sys.setrecursionlimit(10**8)
    input = sys.stdin.readline
    N,Q = map(int,input().split())
    C = list(map(int,input().split()))
    def f(n):
        return int(n) - 1
    ab = [list(map(f,input().split())) for _ in range(N-1)]
    txy = [list(map(int,input().split())) for _ in range(Q)]
    e = [[] for _ in range(N)]
    for i in range(N-1):
        a,b = ab[i]
        e[a].append(b)
        e[b].append(a)
    sum = [0 for _ in range(N)]
    d = [[0,0] for _ in range(N)]
    l = [0 for _ in range(N)]

    def segfunc(x, y):
        return x^y
    #################

    #####ide_ele#####
    ide_ele = 0
    #################

    class SegTree:
        """
        init(init_val, ide_ele): 配列init_valで初期化 O(N)
        update(k, x): k番目の値をxに更新 O(logN)
        query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
        """
        def __init__(self, init_val, segfunc, ide_ele):
            """
            init_val: 配列の初期値
            segfunc: 区間にしたい操作
            ide_ele: 単位元
            n: 要素数
            num: n以上の最小の2のべき乗
            tree: セグメント木(1-index)
            """
            n = len(init_val)
            self.segfunc = segfunc
            self.ide_ele = ide_ele
            self.num = 1 << (n - 1).bit_length()
            self.tree = [ide_ele] * 2 * self.num
            # 配列の値を葉にセット
            for i in range(n):
                self.tree[self.num + i] = init_val[i]
            # 構築していく
            for i in range(self.num - 1, 0, -1):
                self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

        def update(self, k, x):
            """
            k番目の値をxに更新
            k: index(0-index)
            x: update value
            """
            k += self.num
            self.tree[k] = x
            while k > 1:
                self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
                k >>= 1

        def query(self, l, r):
            """
            [l, r)のsegfuncしたものを得る
            l: index(0-index)
            r: index(0-index)
            """
            res = self.ide_ele

            l += self.num
            r += self.num
            while l < r:
                if l & 1:
                    res = self.segfunc(res, self.tree[l])
                    l += 1
                if r & 1:
                    res = self.segfunc(res, self.tree[r - 1])
                l >>= 1
                r >>= 1
            return res

    def euler(n,k):
        d[n][0] = k
        l[n] = 1
        now = k
        for i in e[n]:
            if l[i] == 1:
                continue
            qqq = euler(i,now+1)
            now = qqq
        d[n][1] = now + 1
        return (now + 1)
    euler(0,0)

    s = [0 for _ in range(2*N)]
    for i in range(N):
        a,b = d[i]
        s[a] = C[i]
    seg = SegTree(s,segfunc,0)

    for i in range(Q):
        t,x,y = txy[i]
        x -= 1
        if t == 1:
            C[x] ^= y
            seg.update(d[x][0],C[x])
        else:
            print(seg.query(d[x][0],d[x][1]))


if __name__ == '__main__':
    main()

0