結果
問題 | No.1641 Tree Xor Query |
ユーザー | wolgnik |
提出日時 | 2021-08-06 22:26:21 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 316 ms / 5,000 ms |
コード長 | 2,625 bytes |
コンパイル時間 | 164 ms |
コンパイル使用メモリ | 81,792 KB |
実行使用メモリ | 143,220 KB |
最終ジャッジ日時 | 2024-09-17 02:30:17 |
合計ジャッジ時間 | 3,291 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
52,096 KB |
testcase_01 | AC | 43 ms
52,480 KB |
testcase_02 | AC | 43 ms
52,352 KB |
testcase_03 | AC | 44 ms
52,480 KB |
testcase_04 | AC | 46 ms
52,864 KB |
testcase_05 | AC | 44 ms
53,120 KB |
testcase_06 | AC | 44 ms
53,504 KB |
testcase_07 | AC | 44 ms
52,992 KB |
testcase_08 | AC | 42 ms
52,224 KB |
testcase_09 | AC | 44 ms
52,864 KB |
testcase_10 | AC | 42 ms
52,736 KB |
testcase_11 | AC | 42 ms
52,736 KB |
testcase_12 | AC | 43 ms
52,352 KB |
testcase_13 | AC | 312 ms
127,852 KB |
testcase_14 | AC | 316 ms
127,988 KB |
testcase_15 | AC | 108 ms
77,168 KB |
testcase_16 | AC | 157 ms
80,256 KB |
testcase_17 | AC | 150 ms
79,040 KB |
testcase_18 | AC | 113 ms
78,336 KB |
testcase_19 | AC | 128 ms
77,200 KB |
testcase_20 | AC | 291 ms
143,220 KB |
ソースコード
import sys input = sys.stdin.readline N, Q = map(int, input().split()) a = list(map(int, input().split())) e = [[] for _ in range(N + 1)] for _ in range(N - 1): u, v = map(int, input().split()) e[u].append(v) e[v].append(u) eulbase = pow(10, 6) lime = [[] for _ in range(N + 1)] limevis = [0] * (N + 1) def limitedge(): global limevis global lime limevis[1] = 1 s = [1] while len(s): x = s.pop() for y in e[x]: if limevis[y]: continue lime[x].append(y) limevis[y] = 1 s.append(y) euler = [] depth = [0] * (N + 1) table = [0] * (N + 1) table2 = [0] * (N + 1) def eulerdfs(): global euler global depth global table s = [1] while len(s): x = s.pop() if x >= 0: euler.append(depth[x] * eulbase + x) for y in lime[x]: s.append(~x) s.append(y) depth[y] = depth[x] + 1 else: euler.append(depth[~x] * eulbase + ~x) limitedge() eulerdfs() for i in range(len(euler) - 1, -1, -1): table[euler[i] % eulbase] = i for i in range(len(euler)): table2[euler[i] % eulbase] = i eulera = [0] * len(euler) for x in range(1, N + 1): eulera[table[x]] = a[x - 1] #print(euler) #print(table, depth) class SegTree: def segfunc(self, x, y): return x ^ y def __init__(self, n, ide_ele, init_val): #####単位元###### self.ide_ele = ide_ele #num:n以上の最小の2のべき乗 self.num = 2 ** (n - 1).bit_length() self.seg = [self.ide_ele] * 2 * self.num #set_val for i in range(n): self.seg[i + self.num - 1] = init_val[i] #built for i in range(self.num - 2, -1, -1) : self.seg[i] = self.segfunc(self.seg[2 * i + 1], self.seg[2 * i + 2]) def update(self, k, x): k += self.num - 1 self.seg[k] = x while k + 1: k = (k - 1) // 2 self.seg[k] = self.segfunc(self.seg[k * 2 + 1], self.seg[k * 2 + 2]) def query(self, p, q): if q <= p: return self.ide_ele p += self.num - 1 q += self.num - 2 res = self.ide_ele while q - p > 1: if p & 1 == 0: res = self.segfunc(res, self.seg[p]) if q & 1 == 1: res = self.segfunc(res, self.seg[q]) q -= 1 p = p // 2 q = (q - 1) // 2 if p == q: res = self.segfunc(res, self.seg[p]) else: res = self.segfunc(self.segfunc(res, self.seg[p]), self.seg[q]) return res #print(euler, table, table2) seg = SegTree(len(euler), 0, eulera) for _ in range(Q): t, x, y = map(int, input().split()) if t == 1: seg.update(table[x], seg.query(table[x], table[x] + 1) ^ y) if t == 2: print(seg.query(table[x], table2[x] + 1))