結果
問題 | No.900 aδδitivee |
ユーザー | maspy |
提出日時 | 2020-03-22 04:09:14 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 1,400 ms / 2,000 ms |
コード長 | 2,354 bytes |
コンパイル時間 | 84 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 55,040 KB |
最終ジャッジ日時 | 2024-06-06 19:20:16 |
合計ジャッジ時間 | 31,329 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 30 ms
10,752 KB |
testcase_01 | AC | 31 ms
10,880 KB |
testcase_02 | AC | 28 ms
10,752 KB |
testcase_03 | AC | 27 ms
10,624 KB |
testcase_04 | AC | 26 ms
10,880 KB |
testcase_05 | AC | 26 ms
10,752 KB |
testcase_06 | AC | 27 ms
10,752 KB |
testcase_07 | AC | 1,296 ms
50,560 KB |
testcase_08 | AC | 1,255 ms
50,432 KB |
testcase_09 | AC | 1,400 ms
50,432 KB |
testcase_10 | AC | 1,356 ms
50,432 KB |
testcase_11 | AC | 1,270 ms
50,304 KB |
testcase_12 | AC | 1,281 ms
50,432 KB |
testcase_13 | AC | 1,266 ms
50,432 KB |
testcase_14 | AC | 1,276 ms
50,560 KB |
testcase_15 | AC | 1,248 ms
50,432 KB |
testcase_16 | AC | 1,321 ms
50,432 KB |
testcase_17 | AC | 1,333 ms
50,432 KB |
testcase_18 | AC | 1,327 ms
50,432 KB |
testcase_19 | AC | 1,323 ms
50,560 KB |
testcase_20 | AC | 1,268 ms
50,560 KB |
testcase_21 | AC | 1,321 ms
50,432 KB |
testcase_22 | AC | 1,196 ms
54,784 KB |
testcase_23 | AC | 1,184 ms
54,656 KB |
testcase_24 | AC | 1,128 ms
54,520 KB |
testcase_25 | AC | 1,154 ms
54,784 KB |
testcase_26 | AC | 1,192 ms
54,784 KB |
testcase_27 | AC | 1,186 ms
55,040 KB |
testcase_28 | AC | 1,144 ms
54,912 KB |
ソースコード
#!/usr/bin/ python3.8 import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines N = int(readline()) graph = [[] for _ in range(N)] for _ in range(N - 1): u, v, w = map(int, readline().split()) graph[u].append((v, w)) Q = int(readline()) par = [0] * N depth = [0] * N value = [0] * N tour = [0] stack = [0] Lv = [0] * N Rv = [0] * N while stack: x = stack[-1] if not graph[x]: stack.pop() Rv[x] = len(tour) - 1 tour.append(par[x]) continue y, c = graph[x].pop() par[y] = x depth[y] = depth[x] + 1 stack.append(y) Lv[y] = len(tour) tour.append(y) value[y] = value[x] + c class BinaryIndexedTree(): def __init__(self, seq): self.size = len(seq) self.depth = self.size.bit_length() self.build(seq) def build(self, seq): data = seq size = self.size for i, x in enumerate(data): j = i + (i & (-i)) if j < size: data[j] += data[i] self.data = data def __repr__(self): return self.data.__repr__() def get_sum(self, i): data = self.data s = 0 while i: s += data[i] i -= i & -i return s def add(self, i, x): data = self.data size = self.size while i < size: data[i] += x i += i & -i def find_kth_element(self, k): data = self.data size = self.size x, sx = 0, 0 dx = 1 << (self.depth) for i in range(self.depth - 1, -1, -1): dx = (1 << i) if x + dx >= size: continue y = x + dx sy = sx + data[y] if sy < k: x, sx = y, sy return x + 1 L = len(tour) bit0 = BinaryIndexedTree([0] * (L + 10)) bit1 = BinaryIndexedTree([0] * (L + 10)) for query in readlines(): if query.startswith(b'1'): _, v, x = map(int, query.split()) L = Lv[v] R = Rv[v] D = depth[v] bit1.add(L + 1, x) bit1.add(R + 2, -x) bit0.add(L + 1, -D * x) bit0.add(R + 2, D * x) else: _, v = map(int, query.split()) i = Lv[v] + 1 print(bit1.get_sum(i) * depth[v] + bit0.get_sum(i) + value[v])