結果

問題 No.1054 Union add query
ユーザー rabimearabimea
提出日時 2023-02-15 03:25:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 929 bytes
コンパイル時間 1,962 ms
コンパイル使用メモリ 199,524 KB
実行使用メモリ 7,976 KB
最終ジャッジ日時 2023-09-24 13:42:51
合計ジャッジ時間 7,811 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 WA -
testcase_02 AC 1 ms
4,380 KB
testcase_03 WA -
testcase_04 AC 416 ms
7,472 KB
testcase_05 WA -
testcase_06 AC 356 ms
6,184 KB
testcase_07 AC 329 ms
7,976 KB
testcase_08 AC 252 ms
6,768 KB
testcase_09 AC 509 ms
7,488 KB
testcase_10 AC 109 ms
5,264 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]};
	
	int y = p[x];
	auto [root, sum] = find(y);
	
	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;
			s[u] -= s[v];
			p[u] = v;
		} else if(t == 2) {
			s[find(a).fi] += b;
		} else {
			cout << find(a).se << '\n';
		}
	}
}

0