結果

問題 No.1054 Union add query
ユーザー c-yanc-yan
提出日時 2020-07-30 05:07:00
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,577 ms / 2,000 ms
コード長 500 bytes
コンパイル時間 93 ms
コンパイル使用メモリ 11,016 KB
実行使用メモリ 47,648 KB
最終ジャッジ日時 2023-09-14 20:53:23
合計ジャッジ時間 13,017 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,224 KB
testcase_01 AC 17 ms
8,216 KB
testcase_02 AC 16 ms
8,184 KB
testcase_03 AC 1,577 ms
22,344 KB
testcase_04 AC 1,238 ms
31,320 KB
testcase_05 AC 1,451 ms
20,724 KB
testcase_06 AC 1,088 ms
30,320 KB
testcase_07 AC 1,001 ms
47,648 KB
testcase_08 AC 1,051 ms
35,100 KB
testcase_09 AC 1,066 ms
31,524 KB
testcase_10 AC 771 ms
16,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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