結果

問題 No.1054 Union add query
ユーザー kotatsugame
提出日時 2020-05-15 22:10:58
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 673 ms / 2,000 ms
コード長 580 bytes
コンパイル時間 522 ms
コンパイル使用メモリ 64,312 KB
実行使用メモリ 9,472 KB
最終ジャッジ日時 2024-09-19 10:46:21
合計ジャッジ時間 5,926 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 8
権限があれば一括ダウンロードができます
コンパイルメッセージ
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