結果

問題 No.1054 Union add query
ユーザー kotatsugamekotatsugame
提出日時 2020-05-15 22:10:58
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 715 ms / 2,000 ms
コード長 580 bytes
コンパイル時間 522 ms
コンパイル使用メモリ 64,628 KB
実行使用メモリ 10,784 KB
最終ジャッジ日時 2023-10-19 14:37:32
合計ジャッジ時間 6,224 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,808 KB
testcase_01 AC 2 ms
7,808 KB
testcase_02 AC 3 ms
7,808 KB
testcase_03 AC 606 ms
10,244 KB
testcase_04 AC 637 ms
10,784 KB
testcase_05 AC 581 ms
10,048 KB
testcase_06 AC 580 ms
10,524 KB
testcase_07 AC 528 ms
10,524 KB
testcase_08 AC 475 ms
10,524 KB
testcase_09 AC 715 ms
10,784 KB
testcase_10 AC 277 ms
10,064 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:5:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
    5 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
using namespace std;
int N,Q;
int pr[5<<17],sz[5<<17],sum[5<<17];
main()
{
	cin>>N>>Q;
	for(int i=0;i<N;i++)
	{
		pr[i]=i;
		sz[i]=1;
	}
	for(;Q--;)
	{
		int t,a,b;cin>>t>>a>>b;
		if(t==1)
		{
			a--,b--;
			while(pr[a]!=a)a=pr[a];
			while(pr[b]!=b)b=pr[b];
			if(a==b)continue;
			if(sz[a]<sz[b])a^=b^=a^=b;
			pr[b]=a;
			sz[a]+=sz[b];
			sum[b]-=sum[a];
		}
		else if(t==2)
		{
			a--;
			while(pr[a]!=a)a=pr[a];
			sum[a]+=b;
		}
		else
		{
			a--;
			int ans=sum[a];
			while(pr[a]!=a)
			{
				a=pr[a];
				ans+=sum[a];
			}
			cout<<ans<<endl;
		}
	}
}
0