結果

問題 No.1641 Tree Xor Query
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-08-06 21:41:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 537 ms / 5,000 ms
コード長 3,717 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 81,788 KB
実行使用メモリ 114,092 KB
最終ジャッジ日時 2023-10-17 02:58:01
合計ジャッジ時間 4,148 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,568 KB
testcase_01 AC 37 ms
53,568 KB
testcase_02 AC 37 ms
53,568 KB
testcase_03 AC 39 ms
53,568 KB
testcase_04 AC 42 ms
55,616 KB
testcase_05 AC 42 ms
55,616 KB
testcase_06 AC 41 ms
55,616 KB
testcase_07 AC 40 ms
55,616 KB
testcase_08 AC 38 ms
53,568 KB
testcase_09 AC 40 ms
53,568 KB
testcase_10 AC 41 ms
55,616 KB
testcase_11 AC 39 ms
53,568 KB
testcase_12 AC 39 ms
53,568 KB
testcase_13 AC 537 ms
98,220 KB
testcase_14 AC 517 ms
98,256 KB
testcase_15 AC 129 ms
77,596 KB
testcase_16 AC 199 ms
79,400 KB
testcase_17 AC 196 ms
78,508 KB
testcase_18 AC 142 ms
77,856 KB
testcase_19 AC 163 ms
77,004 KB
testcase_20 AC 436 ms
114,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#HLD
class HeavyLightDecomposition:
    def __init__(self, G):
        self.G = G
        self.n = len(G)
        self.par = [-1] * self.n
        self.size = [1] * self.n
        self.head = [0] * self.n
        self.preord = [0] * self.n
        self.k = 0
        for v in range(self.n):
            if self.par[v] == -1:
                self.dfs_sz(v)
                self.dfs_hld(v)
    
    def dfs_sz(self, v):
        G = self.G
        stack, order = [v], [v]
        while stack:
            p = stack.pop()
            for u in G[p]:
                if self.par[p] == u: continue
                self.par[u] = p
                stack.append(u)
                order.append(u)
        while order:
            p = order.pop()
            ch = G[p]
            if len(ch) and ch[0] == self.par[p]:
                ch[0], ch[-1] = ch[-1], ch[0]
            for i, u in enumerate(ch):
                if u == self.par[p]: continue
                self.size[p] += self.size[u]
                if self.size[u] > self.size[ch[0]]:
                    ch[i], ch[0] = ch[0], ch[i]
    
    def dfs_hld(self, v):
        G = self.G
        stack = [v]
        while stack:
            p = stack.pop()
            self.preord[p] = self.k
            self.k += 1
            top = self.G[p][0]
            for u in G[p][::-1]:
                if u == self.par[p]: continue
                if u == top:
                    self.head[u] = self.head[p]
                else:
                    self.head[u] = u
                stack.append(u)

    def enumerate_vertices(self, u, v):
        while True:
            if self.preord[u] > self.preord[v]: u, v = v, u
            l = max(self.preord[self.head[v]], self.preord[u])
            r = self.preord[v]
            yield l, r
            if self.head[u] != self.head[v]:
                v = self.par[self.head[v]]
            else:
                return
    
    def enumerate_edges(self, u, v):
        while True:
            if self.preord[u] > self.preord[v]: u, v = v, u
            if self.head[u] != self.head[v]:
                yield self.preord[self.head[v]], self.preord[v]
                v = self.par[self.head[v]]
            else:
                if u != v:
                    return self.preord[u] + 1, self.preord[v]

    def subtree(self, v):
        l = self.preord[v]
        r = self.preord[v] + self.size[v]
        return l, r

    def lowest_common_ancestor(self, u, v):
        while True:
            if self.preord[u] > self.preord[v]: u, v = v, u
            if self.head[u] == self.head[v]: return u
            v = self.par[self.head[v]]

#BIT
class BinaryIndexedTree():
    def __init__(self, n):
        self.n = 1 << (n.bit_length())
        self.BIT = [0] * (self.n + 1)

    def build(self, init_lis):
        for i, v in enumerate(init_lis):
            self.add(i, v)

    def add(self, i, x):
        i += 1
        while i <= self.n:
            self.BIT[i] ^= x
            i += i & -i
    
    def sum(self, l, r):
        return self._sum(r) ^ self._sum(l)

    def _sum(self, i):
        res = 0
        while i > 0:
            res ^= self.BIT[i]
            i -= i & -i
        return res

n, q = map(int, input().split())
A = list(map(int, input().split()))
G = [[] for i in range(n)]
for _ in range(n - 1):
    u, v = map(int, input().split())
    G[u - 1].append(v - 1)
    G[v - 1].append(u - 1)
HLD = HeavyLightDecomposition(G)
BIT = BinaryIndexedTree(n)
for i, a in enumerate(A):
    BIT.add(HLD.preord[i], a)
for _ in range(q):
    t, x, y = map(int, input().split())
    if t == 1:
        BIT.add(HLD.preord[x - 1], y)
    else:
        l, r = HLD.subtree(x - 1)
        ans = BIT.sum(l, r)
        print(ans)
0