結果

問題 No.1054 Union add query
ユーザー c-yanc-yan
提出日時 2020-07-30 05:10:34
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,474 ms / 2,000 ms
コード長 493 bytes
コンパイル時間 85 ms
コンパイル使用メモリ 10,988 KB
実行使用メモリ 47,660 KB
最終ジャッジ日時 2023-09-14 21:07:31
合計ジャッジ時間 10,867 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
8,272 KB
testcase_01 AC 16 ms
7,904 KB
testcase_02 AC 16 ms
8,320 KB
testcase_03 AC 1,474 ms
22,372 KB
testcase_04 AC 1,205 ms
31,452 KB
testcase_05 AC 1,383 ms
20,760 KB
testcase_06 AC 1,083 ms
30,252 KB
testcase_07 AC 992 ms
47,660 KB
testcase_08 AC 1,068 ms
35,124 KB
testcase_09 AC 1,078 ms
31,584 KB
testcase_10 AC 782 ms
16,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import setrecursionlimit,stdin
l=stdin.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