結果

問題 No.1054 Union add query
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2022-10-08 16:10:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 811 ms / 2,000 ms
コード長 2,474 bytes
コンパイル時間 443 ms
コンパイル使用メモリ 86,996 KB
実行使用メモリ 142,544 KB
最終ジャッジ日時 2023-09-04 21:42:05
合計ジャッジ時間 8,235 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 106 ms
72,576 KB
testcase_01 AC 106 ms
72,588 KB
testcase_02 AC 108 ms
72,528 KB
testcase_03 AC 731 ms
104,168 KB
testcase_04 AC 811 ms
142,544 KB
testcase_05 AC 628 ms
93,272 KB
testcase_06 AC 391 ms
110,580 KB
testcase_07 AC 350 ms
110,648 KB
testcase_08 AC 380 ms
110,896 KB
testcase_09 AC 428 ms
131,776 KB
testcase_10 AC 289 ms
129,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 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)
0