結果
問題 | No.2439 Fragile Apple Tree |
ユーザー | tatyam |
提出日時 | 2023-08-18 18:55:39 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 4,497 ms / 10,000 ms |
コード長 | 3,196 bytes |
コンパイル時間 | 220 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 438,060 KB |
最終ジャッジ日時 | 2024-11-28 12:19:20 |
合計ジャッジ時間 | 71,657 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2,265 ms
172,480 KB |
testcase_01 | AC | 4,227 ms
193,100 KB |
testcase_02 | AC | 4,497 ms
195,668 KB |
testcase_03 | AC | 1,792 ms
438,060 KB |
testcase_04 | AC | 2,475 ms
437,884 KB |
testcase_05 | AC | 4,357 ms
197,760 KB |
testcase_06 | AC | 3,908 ms
188,184 KB |
testcase_07 | AC | 3,984 ms
184,500 KB |
testcase_08 | AC | 4,276 ms
183,788 KB |
testcase_09 | AC | 45 ms
69,800 KB |
testcase_10 | AC | 45 ms
69,800 KB |
testcase_11 | AC | 45 ms
69,648 KB |
testcase_12 | AC | 97 ms
69,968 KB |
testcase_13 | AC | 46 ms
69,672 KB |
testcase_14 | AC | 4,333 ms
179,948 KB |
testcase_15 | AC | 2,774 ms
185,088 KB |
testcase_16 | AC | 3,628 ms
185,368 KB |
testcase_17 | AC | 3,582 ms
186,084 KB |
testcase_18 | AC | 2,433 ms
176,520 KB |
testcase_19 | AC | 1,604 ms
136,512 KB |
testcase_20 | AC | 933 ms
125,936 KB |
testcase_21 | AC | 482 ms
98,108 KB |
testcase_22 | AC | 2,303 ms
148,288 KB |
testcase_23 | AC | 1,264 ms
118,988 KB |
testcase_24 | AC | 1,434 ms
131,168 KB |
testcase_25 | AC | 1,593 ms
157,084 KB |
testcase_26 | AC | 2,069 ms
169,356 KB |
testcase_27 | AC | 1,648 ms
143,300 KB |
testcase_28 | AC | 49 ms
70,020 KB |
testcase_29 | AC | 45 ms
69,460 KB |
testcase_30 | AC | 53 ms
75,660 KB |
testcase_31 | AC | 1,395 ms
195,516 KB |
testcase_32 | AC | 1,397 ms
195,680 KB |
ソースコード
# パス減算 → 破壊チェック → 破壊された辺に掛かっていた重みを上にパス加算 # 必要な操作: 辺属性, パス加算, 区間 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) def F(l: int, r: int): 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 F(l, r) 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].insert(0, 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 def dfs(i: int): global idx index[i] = idx add(idx, idx + 1, C[i] - INF) idx += 1 if not g[i]: return heavy[g[i][0]] = heavy[i] dfs(g[i][0]) for j in g[i][1:]: heavy[j] = j dfs(j) dfs(0) 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): global vertices if erased[i]: return erased[i] = True vertices -= 1 x = C[i] - get(index[i]) add(index[i], index[i] + 1, INF) for j in g[i]: dfs(j) 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)