結果
問題 | No.1054 Union add query |
ユーザー | Navier_Boltzmann |
提出日時 | 2022-10-08 16:10:43 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 649 ms / 2,000 ms |
コード長 | 2,474 bytes |
コンパイル時間 | 184 ms |
コンパイル使用メモリ | 82,376 KB |
実行使用メモリ | 138,336 KB |
最終ジャッジ日時 | 2024-06-22 19:10:31 |
合計ジャッジ時間 | 5,710 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 44 ms
57,516 KB |
testcase_01 | AC | 41 ms
56,072 KB |
testcase_02 | AC | 41 ms
56,016 KB |
testcase_03 | AC | 553 ms
99,956 KB |
testcase_04 | AC | 649 ms
138,336 KB |
testcase_05 | AC | 446 ms
90,384 KB |
testcase_06 | AC | 282 ms
106,928 KB |
testcase_07 | AC | 249 ms
106,804 KB |
testcase_08 | AC | 274 ms
107,316 KB |
testcase_09 | AC | 331 ms
129,076 KB |
testcase_10 | AC | 224 ms
125,964 KB |
ソースコード
# import pypyjit # pypyjit.set_param('max_unroll_recursion=-1') from collections import * from itertools import * from functools import * from heapq import * import sys,math input = sys.stdin.readline class DSU: def __init__(self, n): self._n = n self.parent_or_size = [-1] * n self.member = [[i] for i in range(n)] def merge(self, a, b): assert 0 <= a < self._n assert 0 <= b < self._n x, y = self.leader(a), self.leader(b) if x == y: return x if -self.parent_or_size[x] < -self.parent_or_size[y]: x, y = y, x self.parent_or_size[x] += self.parent_or_size[y] for tmp in self.member[y]: self.member[x].append(tmp) self.parent_or_size[y] = x return x def same(self, a, b): assert 0 <= a < self._n assert 0 <= b < self._n return self.leader(a) == self.leader(b) def leader(self, a): assert 0 <= a < self._n if self.parent_or_size[a] < 0: return a self.parent_or_size[a] = self.leader(self.parent_or_size[a]) return self.parent_or_size[a] def size(self, a): assert 0 <= a < self._n return -self.parent_or_size[self.leader(a)] def groups(self): leader_buf = [self.leader(i) for i in range(self._n)] result = [[] for _ in range(self._n)] for i in range(self._n): result[leader_buf[i]].append(i) return [r for r in result if r != []] N,Q = map(int,input().split()) D = DSU(N) X = [0]*N for _ in range(Q): t,a,b = map(int,input().split()) if t==1: a -= 1 b -= 1 if D.same(a,b): continue la = D.leader(a) lb = D.leader(b) ga = D.member[la] gb = D.member[lb] D.merge(a,b) lp = D.leader(la) if lp==la: for y in gb: if y!=lb: X[y] = X[lb] + X[y] for y in gb: X[y] = X[y] - X[lp] else: for y in ga: if y!=la: X[y] = X[la] + X[y] for y in ga: X[y] = X[y] - X[lp] elif t==2: a -= 1 X[D.leader(a)] += b else: a -= 1 ans = X[a] if D.leader(a) != a: ans += X[D.leader(a)] print(ans)