結果
問題 |
No.1054 Union add query
|
ユーザー |
![]() |
提出日時 | 2020-12-18 19:55:19 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 897 bytes |
コンパイル時間 | 193 ms |
コンパイル使用メモリ | 82,332 KB |
実行使用メモリ | 103,304 KB |
最終ジャッジ日時 | 2024-09-21 09:15:49 |
合計ジャッジ時間 | 5,514 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | TLE * 1 -- * 7 |
ソースコード
import sys input = sys.stdin.buffer.readline N, Q = map(int, input().split()) tmp = [0] * N num = [0] * N root = [[i] for i in range(N)] def find(x): if isinstance(root[x], list): return x res = find(root[x]) root[x] = res return res def merge(x, y): x = find(x) y = find(y) if x == y: return if len(root[x]) > len(root[y]): x, y = y, x tx = tmp[x] for i in root[x]: num[i] += tx tmp[x] = 0 ty = tmp[y] for i in root[y]: num[i] += ty tmp[y] = 0 root[y].extend(root[x]) root[x] = y ans = [] for _ in range(Q): t, a, b = map(int, input().split()) if t == 1: merge(a-1, b-1) elif t == 2: leader = find(a-1) tmp[leader] += b else: leader = find(a-1) ans.append(num[a-1] + tmp[leader]) print(*ans, sep="\n")