結果

問題 No.1054 Union add query
ユーザー root__786root__786
提出日時 2020-05-22 15:04:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 308 ms / 2,000 ms
コード長 1,256 bytes
コンパイル時間 2,225 ms
コンパイル使用メモリ 200,792 KB
実行使用メモリ 44,648 KB
最終ジャッジ日時 2024-04-15 04:56:20
合計ジャッジ時間 5,366 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
18,388 KB
testcase_01 AC 6 ms
19,692 KB
testcase_02 AC 5 ms
15,428 KB
testcase_03 AC 180 ms
23,032 KB
testcase_04 AC 308 ms
44,648 KB
testcase_05 AC 147 ms
19,140 KB
testcase_06 AC 158 ms
29,912 KB
testcase_07 AC 140 ms
30,124 KB
testcase_08 AC 154 ms
31,872 KB
testcase_09 AC 219 ms
38,632 KB
testcase_10 AC 123 ms
36,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define all(v) v.begin(),v.end()
#define P pair<int,int>
#define len(s) (int)s.size()
#define pb push_back

template<class T> inline bool chmin(T &a, T b){
	if(a>b){a=b;return true;}
	return false;
}
template<class T> inline bool chmax(T &a, T b){
	if(a<b){a=b;return true;}
	return false;
}
constexpr int mod = 1e9+7;
constexpr int inf = 3e18;

int N,Q;
vector<int>v[500005];
int ans[500005],add[500005],par[500005];
int find(int x){
	if(par[x]==x)return x;
	return par[x]=find(par[x]);
}
signed main(){
	cin.tie(0);ios::sync_with_stdio(false);
	cin>>N>>Q;
	rep(i,N){
		par[i]=i;v[i].pb(i);
	}
	while(Q--){
		int t,a,b;cin>>t>>a>>b;
		if(t==1){
			a--;b--;
			a=find(a);b=find(b);
			if(a==b)continue;
			if(len(v[a])<len(v[b])){
				for(int i:v[a])ans[i]+=add[a]-add[b];
				add[a]=0;
				v[b].insert(v[b].end(),all(v[a]));
				v[a].clear();
				par[a]=b;
			}else {
				for(int i:v[b])ans[i]+=add[b]-add[a];
				add[b]=0;
				v[a].insert(v[a].end(),all(v[b]));
				v[b].clear();
				par[b]=a;
			}
		}else if(t==2){
			a--;a=find(a);
			add[a]+=b;
		}else {
			a--;
			cout<<ans[a]+add[find(a)]<<"\n";
		}
	}
}
0