結果

問題 No.2439 Fragile Apple Tree
ユーザー 👑 tatyamtatyam
提出日時 2023-08-18 19:50:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 4,167 ms / 10,000 ms
コード長 3,178 bytes
コンパイル時間 314 ms
コンパイル使用メモリ 86,876 KB
実行使用メモリ 197,828 KB
最終ジャッジ日時 2023-08-18 23:15:58
合計ジャッジ時間 63,831 ms
ジャッジサーバーID
(参考情報)
judge10 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,060 ms
173,776 KB
testcase_01 AC 3,713 ms
186,156 KB
testcase_02 AC 3,287 ms
186,088 KB
testcase_03 AC 1,074 ms
185,268 KB
testcase_04 AC 1,714 ms
185,040 KB
testcase_05 AC 3,433 ms
185,884 KB
testcase_06 AC 3,419 ms
186,448 KB
testcase_07 AC 3,536 ms
184,748 KB
testcase_08 AC 3,923 ms
184,584 KB
testcase_09 AC 82 ms
87,420 KB
testcase_10 AC 99 ms
87,600 KB
testcase_11 AC 101 ms
87,596 KB
testcase_12 AC 99 ms
87,664 KB
testcase_13 AC 98 ms
87,528 KB
testcase_14 AC 4,167 ms
180,556 KB
testcase_15 AC 2,746 ms
186,284 KB
testcase_16 AC 3,359 ms
186,128 KB
testcase_17 AC 3,235 ms
186,188 KB
testcase_18 AC 1,909 ms
174,508 KB
testcase_19 AC 1,401 ms
135,052 KB
testcase_20 AC 914 ms
126,864 KB
testcase_21 AC 455 ms
98,068 KB
testcase_22 AC 1,906 ms
143,108 KB
testcase_23 AC 1,085 ms
118,240 KB
testcase_24 AC 1,220 ms
130,488 KB
testcase_25 AC 1,463 ms
157,880 KB
testcase_26 AC 1,748 ms
169,528 KB
testcase_27 AC 1,195 ms
139,228 KB
testcase_28 AC 84 ms
87,732 KB
testcase_29 AC 84 ms
87,532 KB
testcase_30 AC 86 ms
91,620 KB
testcase_31 AC 1,432 ms
197,800 KB
testcase_32 AC 1,427 ms
197,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# パス減算 → 破壊チェック → 破壊された辺に掛かっていた重みを上にパス加算
# 必要な操作: 辺属性, パス加算, 区間 min の max-right, 1 辺取得

import sys
sys.setrecursionlimit(2 ** 20)
input = sys.stdin.readline
INF = 1 << 61
SIZ = 1 << 19
LOG = 19

d = [INF] * (SIZ * 2)
lz = [0] * (SIZ * 2)

def all_apply(k: int, f: int):
    d[k] += f
    lz[k] += f

def push(k: int):
    if lz[k]:
        all_apply(2 * k, lz[k])
        all_apply(2 * k + 1, lz[k])
        lz[k] = 0

def update(k: int):
    d[k] = min(d[k << 1], d[k << 1 | 1])

def get(p: int):
    p += SIZ
    for i in range(LOG, 0, -1): push(p >> i)
    return d[p]

def add(l: int, r: int, x: int):
    if l == r: return
    l += SIZ
    r += SIZ
    for i in range(LOG, 0, -1):
        if l >> i << i != l: push(l >> i)
        if r >> i << i != r: push((r - 1) >> i)
    L = l
    R = r
    while L < R:
        if L & 1:
            all_apply(L, x)
            L += 1
        if R & 1:
            R -= 1
            all_apply(R, x)
        L >>= 1
        R >>= 1
    for i in range(1, LOG + 1):
        if l >> i << i != l: update(l >> i)
        if r >> i << i != r: update((r - 1) >> i)

def broken_edge() -> int:
    r = 2
    for _ in range(LOG):
        push(r - 1)
        r <<= 1
        if d[r - 1] > 0:
            r -= 1
    return r - SIZ - 1

N, Q = map(int, input().split())
edge: list[list[int]] = [list(map(int, input().split())) for _ in range(N - 1)]
g: list[list[int]] = [[] for _ in range(N)]

for e in edge:
    e[0] -= 1
    e[1] -= 1
    a, b, c = e
    g[a].append(b)
    g[b].append(a)

siz = [1] * N
parent = [-1] * N
C = [0] * N

q = [0]
for i in q:
    for j in g[i]:
        parent[j] = i
        q.append(j)
        g[j].remove(i)

for i in reversed(q):
    p = parent[i]
    if p != -1:
        siz[p] += siz[i]
    if g[i]:
        j = max(g[i], key=lambda x: siz[x])
        g[i].remove(j)
        g[i].append(j)

C[0] = INF
for a, b, c in edge:
    a = min(a, b, key=lambda x: siz[x])
    C[a] = c

heavy = [0] * N
index = [0] * N

idx = 0
q = [0]
while q:
    i = q.pop()
    index[i] = idx
    add(idx, idx + 1, C[i] - INF)
    idx += 1
    if not g[i]:
        continue
    for j in g[i]:
        heavy[j] = j
    heavy[g[i][-1]] = heavy[i]
    q.extend(g[i])

rev = [-1] * N
for i in range(N):
    rev[index[i]] = i

vertices = N
erased = [False] * N

def path_add(i: int, x: int):
    while i > 0:
        h = heavy[i]
        add(index[h], index[i] + 1, x)
        i = parent[h]

def dfs(i: int):
    q = [i]
    global vertices
    while q:
        i = q.pop()
        if erased[i]:
            continue
        erased[i] = True
        vertices -= 1
        x = C[i] - get(index[i])
        add(index[i], index[i] + 1, INF)
        q.extend(g[i])

for _ in range(Q):
    query = input()
    if query[0] == '1':
        v, x = map(int, query[2:].split())
        v -= 1
        # 0 - v パスに x を減算
        path_add(v, -x)
        # <= 0 の辺を切る
        while d[1] <= 0:
            i = rev[broken_edge()]
            path_add(i, C[i] - get(index[i]))
            dfs(i)
    else:
        print(vertices)
0