結果

問題 No.900 aδδitivee
ユーザー Kiri8128Kiri8128
提出日時 2019-10-05 16:09:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 702 ms / 2,000 ms
コード長 2,214 bytes
コンパイル時間 151 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 117,496 KB
最終ジャッジ日時 2024-04-15 21:42:05
合計ジャッジ時間 16,459 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
56,832 KB
testcase_01 AC 45 ms
56,960 KB
testcase_02 AC 50 ms
59,008 KB
testcase_03 AC 49 ms
58,752 KB
testcase_04 AC 50 ms
59,008 KB
testcase_05 AC 48 ms
59,008 KB
testcase_06 AC 50 ms
58,880 KB
testcase_07 AC 684 ms
112,200 KB
testcase_08 AC 664 ms
112,000 KB
testcase_09 AC 676 ms
112,256 KB
testcase_10 AC 675 ms
111,872 KB
testcase_11 AC 691 ms
112,384 KB
testcase_12 AC 683 ms
112,256 KB
testcase_13 AC 677 ms
112,128 KB
testcase_14 AC 685 ms
112,128 KB
testcase_15 AC 682 ms
111,984 KB
testcase_16 AC 686 ms
111,872 KB
testcase_17 AC 702 ms
112,000 KB
testcase_18 AC 702 ms
112,128 KB
testcase_19 AC 648 ms
112,000 KB
testcase_20 AC 679 ms
112,484 KB
testcase_21 AC 671 ms
112,032 KB
testcase_22 AC 583 ms
117,248 KB
testcase_23 AC 587 ms
117,212 KB
testcase_24 AC 582 ms
117,496 KB
testcase_25 AC 597 ms
117,352 KB
testcase_26 AC 590 ms
117,192 KB
testcase_27 AC 594 ms
117,248 KB
testcase_28 AC 584 ms
117,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

NN = 17
BIT1=[0]*(2**NN+1)
BIT2=[0]*(2**NN+1)
 
def addrange(l0, r0, x=1):
    l, r = l0, r0
    while l <= 2**NN:
        BIT1[l] -= x * l0
        BIT2[l] -= x
        l += l & (-l)
    while r <= 2**NN:
        BIT1[r] += x * r0
        BIT2[r] += x
        r += r & (-r)

def rangesum(l, r):
    l0, r0, a, b, c = l, r, 0, 0, 0
    while l != 0:
        a -= BIT1[l]
        b += BIT2[l]
        l -= l&(-l)
    while r != 0:
        a += BIT1[r]
        c += BIT2[r]
        r -= r&(-r)
    return a + b * l0 - c * r0

BIT3=[0]*(2**NN+1)
BIT4=[0]*(2**NN+1)
 
def addrange2(l0, r0, x=1):
    l, r = l0, r0
    while l <= 2**NN:
        BIT3[l] -= x * l0
        BIT4[l] -= x
        l += l & (-l)
    while r <= 2**NN:
        BIT3[r] += x * r0
        BIT4[r] += x
        r += r & (-r)

def rangesum2(l, r):
    l0, r0, a, b, c = l, r, 0, 0, 0
    while l != 0:
        a -= BIT3[l]
        b += BIT4[l]
        l -= l&(-l)
    while r != 0:
        a += BIT3[r]
        c += BIT4[r]
        r -= r&(-r)
    return a + b * l0 - c * r0

N = int(input())
X = [[] for i in range(N)]
for i in range(N-1):
    x, y, d = map(int, input().split())
    X[x].append((y, d))
    X[y].append((x, d))

P = [(-1, 0)] * N
Q = [(-1, 0), (0, 0)]
ET = []
ct = 0
dd = 0
CTA = [0] * N
CTB = [0] * N
DE = [0] * N
D = [0] * N
de = -1
while Q:
    i, d = Q.pop()
    if i < 0:
        CTB[~i] = ct
        de -= 1
        dd -= d
        continue
    if i >= 0:
        ct += 1
        if CTA[i] == 0: CTA[i] = ct
        dd += d
        de += 1
        DE[i] = de
        D[i] = dd
    
    for a, d in X[i][::-1]:
        if a != P[i][0]:
            ET.append((i, a))
            P[a] = (i, d)
            for k in range(len(X[a])):
                if X[a][k][0] == i:
                    del X[a][k]
                    break
            Q.append((~a, d))
            Q.append((a, d))

for _ in range(int(input())):
    tmp = [int(a) for a in input().split()]
    if tmp[0] == 1:
        a, x = tmp[1:]
        addrange(CTA[a]+1, CTB[a]+1, x)
        addrange2(CTA[a]+1, CTB[a]+1, x * DE[a])
        
    else:
        b = tmp[1]
        print(D[b] + rangesum(CTA[b], CTA[b]+1) * DE[b] - rangesum2(CTA[b], CTA[b]+1))
        
0