結果

問題 No.1054 Union add query
ユーザー c-yanc-yan
提出日時 2020-07-30 05:03:28
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 480 bytes
コンパイル時間 265 ms
コンパイル使用メモリ 86,968 KB
実行使用メモリ 252,544 KB
最終ジャッジ日時 2023-09-14 18:29:02
合計ジャッジ時間 7,346 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,324 KB
testcase_01 AC 73 ms
71,228 KB
testcase_02 AC 76 ms
71,180 KB
testcase_03 AC 768 ms
99,040 KB
testcase_04 AC 615 ms
101,788 KB
testcase_05 AC 754 ms
100,160 KB
testcase_06 AC 769 ms
183,976 KB
testcase_07 RE -
testcase_08 AC 848 ms
214,040 KB
testcase_09 AC 473 ms
106,976 KB
testcase_10 AC 306 ms
84,956 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import setrecursionlimit
def f(p,a,i):
	t=p[i]
	if t<0:
		return i,0
	t,u=f(p,a,t)
	p[i]=t
	a[i]+=u
	return t,a[i]
def u(p,a,i,j):
	i,_=f(p,a,i)
	j,_=f(p,a,j)
	if i==j:
		return
	p[j]+=p[i]
	p[i]=j
	a[i]-=a[j]
setrecursionlimit(100000)
N,Q=map(int,input().split())
p=[-1]*(N+1)
a=[0]*(N+1)
r=[]
for _ in range(Q):
	T,A,B=map(int,input().split())
	if T==1:
		u(p,a,A,B)
	elif T==2:
		j,_=f(p,a,A)
		a[j]+=B
	elif T==3:
		j,k=f(p,a,A)
		r.append(a[j]+k)
print(*r,sep='\n')
0