結果

問題 No.1054 Union add query
ユーザー rabimearabimea
提出日時 2023-02-15 03:38:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 517 ms / 2,000 ms
コード長 1,058 bytes
コンパイル時間 2,062 ms
コンパイル使用メモリ 198,100 KB
実行使用メモリ 8,060 KB
最終ジャッジ日時 2023-09-24 13:58:38
合計ジャッジ時間 6,402 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 385 ms
4,380 KB
testcase_04 AC 415 ms
7,520 KB
testcase_05 AC 364 ms
4,376 KB
testcase_06 AC 347 ms
6,264 KB
testcase_07 AC 322 ms
8,060 KB
testcase_08 AC 254 ms
6,980 KB
testcase_09 AC 517 ms
7,264 KB
testcase_10 AC 107 ms
5,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
 
#define fi first
#define se second
#define pb push_back
using vi = vector <int>;
using ll = long long;
using vl = vector <ll>;
using pii = pair <int, int>;
const ll mod = 998244353;
//~ const ll mod = 1e9 + 7;
ll qpow(ll a, ll b, ll m = mod) { ll r = 1, t = a;
	for(; b; b /= 2) { if(b & 1) r = r * t % m; t = t * t % m; } return r; }

const int N = 5e5 + 11;
int p[N], s[N];

pii find(int x) {
	if(p[x] == x) return {x, s[x]};
	
	auto [root, sum] = find(p[x]);
	
	p[x] = root;
	s[x] = sum + s[x] - s[root];
	
	return {root, s[root] + s[x]};
}

int main() {
	ios::sync_with_stdio(0);
	int n, q; cin >> n >> q;
	
	iota(p, p + n + 1, 0);
	
	while(q --) {
		int t, a, b; cin >> t >> a >> b;
		if(t == 1) {
			int u = find(a).fi, v = find(b).fi;
			if(u != v) {
				s[u] -= s[v];
				p[u] = v;
			}
		} else if(t == 2) {
			s[find(a).fi] += b;
		} else {
			cout << find(a).se << '\n';
		}
		
		//~ cout << "------\n";
		//~ for(int i = 1; i <= n; i ++)
			//~ cout << find(i).se << ' ';
		//~ cout << '\n';
	}
}

0