結果

問題 No.900 aδδitivee
ユーザー maspymaspy
提出日時 2020-03-22 04:09:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,167 ms / 2,000 ms
コード長 2,354 bytes
コンパイル時間 253 ms
コンパイル使用メモリ 11,164 KB
実行使用メモリ 55,556 KB
最終ジャッジ日時 2023-08-26 00:26:14
合計ジャッジ時間 28,618 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
8,380 KB
testcase_01 AC 13 ms
8,412 KB
testcase_02 AC 14 ms
8,424 KB
testcase_03 AC 15 ms
8,488 KB
testcase_04 AC 16 ms
8,580 KB
testcase_05 AC 15 ms
8,336 KB
testcase_06 AC 15 ms
8,384 KB
testcase_07 AC 1,089 ms
49,524 KB
testcase_08 AC 1,096 ms
49,648 KB
testcase_09 AC 1,114 ms
49,644 KB
testcase_10 AC 1,059 ms
49,672 KB
testcase_11 AC 1,079 ms
49,740 KB
testcase_12 AC 1,095 ms
49,460 KB
testcase_13 AC 1,133 ms
49,536 KB
testcase_14 AC 1,136 ms
49,628 KB
testcase_15 AC 1,077 ms
49,576 KB
testcase_16 AC 1,167 ms
49,304 KB
testcase_17 AC 1,082 ms
49,576 KB
testcase_18 AC 1,086 ms
49,176 KB
testcase_19 AC 1,116 ms
49,572 KB
testcase_20 AC 1,106 ms
49,632 KB
testcase_21 AC 1,123 ms
49,556 KB
testcase_22 AC 1,001 ms
55,556 KB
testcase_23 AC 969 ms
55,436 KB
testcase_24 AC 932 ms
55,432 KB
testcase_25 AC 952 ms
55,416 KB
testcase_26 AC 975 ms
55,380 KB
testcase_27 AC 933 ms
55,436 KB
testcase_28 AC 937 ms
55,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/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])
0