結果
問題 | No.1641 Tree Xor Query |
ユーザー |
|
提出日時 | 2024-09-24 00:42:32 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 518 ms / 5,000 ms |
コード長 | 3,099 bytes |
コンパイル時間 | 2,923 ms |
コンパイル使用メモリ | 82,464 KB |
実行使用メモリ | 120,996 KB |
最終ジャッジ日時 | 2024-09-24 06:06:13 |
合計ジャッジ時間 | 4,594 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 18 |
ソースコード
## https://yukicoder.me/problems/no/1641from collections import dequeclass SegmentTree:"""非再帰版セグメント木。更新は「加法」、取得は「最大値」のもの限定。"""def __init__(self, init_array):n = 1while n < len(init_array):n *= 2self.size = nself.array = [0] * (2 * self.size)for i, a in enumerate(init_array):self.array[self.size + i] = aend_index = self.sizestart_index = end_index // 2while start_index >= 1:for i in range(start_index, end_index):self.array[i] = self.array[2 * i] ^ self.array[2 * i + 1]end_index = start_indexstart_index = end_index // 2def add(self, x, a):index = self.size + xself.array[index] ^= awhile index > 1:index //= 2self.array[index] = self.array[2 * index] ^ self.array[2 * index + 1]def get_max(self, l, r):L = self.size + l; R = self.size + r# 2. 区間[l, r)の最大値を求めるs = 0while L < R:if R & 1:R -= 1s ^= self.array[R]if L & 1:s ^= self.array[L]L += 1L >>= 1; R >>= 1return sdef main():N, Q = map(int, input().split())C = list(map(int, input().split()))next_nodes = [[] for _ in range(N)]for _ in range(N - 1):u, v = map(int, input().split())next_nodes[u - 1].append(v - 1)next_nodes[v - 1].append(u - 1)txy = []for _ in range(Q):t, x, y = map(int, input().split())txy.append((t, x - 1, y))# オイラーツアーeular_tour_index_map = [[-1, -1] for _ in range(N)]eular_tour_index = 0parents = [-2] * Nparents[0] = -1stack = deque()stack.append((0, 0))while len(stack) > 0:v, index = stack.pop()if index == 0:eular_tour_index_map[v][0] = eular_tour_indexeular_tour_index += 1while index < len(next_nodes[v]):w = next_nodes[v][index]if w == parents[v]:index +=1continueparents[w] = vstack.append((v, index + 1))stack.append((w, 0))breakif index == len(next_nodes[v]):eular_tour_index_map[v][1] = eular_tour_indexeular_tour_index += 1# オイラーツアーベースのセグメントツリーeular_tour = [0] * (2 * N)for i in range(N):eular_tour[eular_tour_index_map[i][0]] = C[i]seg_tree = SegmentTree(eular_tour)for t, x, y in txy:if t == 1:x_ = eular_tour_index_map[x][0]seg_tree.add(x_, y)else:# t= 2sin_, out_ = eular_tour_index_map[x]answer = seg_tree.get_max(in_, out_)print(answer)if __name__ == '__main__':main()