結果
問題 | No.1641 Tree Xor Query |
ユーザー | nephrologist |
提出日時 | 2021-08-06 23:00:35 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,885 bytes |
コンパイル時間 | 137 ms |
コンパイル使用メモリ | 82,496 KB |
実行使用メモリ | 117,224 KB |
最終ジャッジ日時 | 2024-09-17 03:12:28 |
合計ジャッジ時間 | 2,624 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 34 ms
53,924 KB |
testcase_01 | AC | 35 ms
53,076 KB |
testcase_02 | AC | 33 ms
53,292 KB |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | AC | 35 ms
54,316 KB |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | AC | 35 ms
53,944 KB |
testcase_13 | AC | 223 ms
108,376 KB |
testcase_14 | AC | 225 ms
108,380 KB |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | AC | 203 ms
117,224 KB |
ソースコード
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(x, v ^ y) else: print(ST.query(left[x], right[x]))