結果

問題 No.1054 Union add query
ユーザー c-yanc-yan
提出日時 2020-07-30 05:01:49
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 480 bytes
コンパイル時間 238 ms
コンパイル使用メモリ 10,840 KB
実行使用メモリ 44,488 KB
最終ジャッジ日時 2023-09-14 18:21:04
合計ジャッジ時間 7,800 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 AC 16 ms
8,008 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 AC 1,867 ms
31,284 KB
testcase_10 AC 1,510 ms
16,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import setrecursionlimit
def f(p,a,i):
	t=p[i]
	if t<0:
		return i,0
	t,a=f(p,a,t)
	p[i]=t
	a[i]+=a
	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