結果

問題 No.1054 Union add query
コンテスト
ユーザー rabimea
提出日時 2023-02-15 03:25:00
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 929 bytes
コンパイル時間 2,092 ms
コンパイル使用メモリ 192,160 KB
最終ジャッジ日時 2025-02-10 15:33:08
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 WA * 1
other AC * 6 WA * 2
権限があれば一括ダウンロードができます

ソースコード

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