結果

問題 No.1054 Union add query
ユーザー c-yanc-yan
提出日時 2020-07-31 14:52:21
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 1,541 ms / 2,000 ms
コード長 489 bytes
コンパイル時間 95 ms
コンパイル使用メモリ 10,852 KB
実行使用メモリ 47,488 KB
最終ジャッジ日時 2023-09-20 14:23:32
合計ジャッジ時間 11,200 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,172 KB
testcase_01 AC 16 ms
8,196 KB
testcase_02 AC 16 ms
8,156 KB
testcase_03 AC 1,541 ms
22,432 KB
testcase_04 AC 1,222 ms
31,284 KB
testcase_05 AC 1,413 ms
20,580 KB
testcase_06 AC 1,089 ms
30,160 KB
testcase_07 AC 1,009 ms
47,488 KB
testcase_08 AC 1,062 ms
35,140 KB
testcase_09 AC 1,063 ms
31,428 KB
testcase_10 AC 785 ms
15,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import setrecursionlimit
l=open(0).readline
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(10**6)
N,Q=map(int,l().split())
p=[-1]*(N+1)
a=[0]*(N+1)
r=[]
for _ in range(Q):
	T,A,B=map(int,l().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