結果
問題 | No.1054 Union add query |
ユーザー |
|
提出日時 | 2023-02-15 03:38:17 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 520 ms / 2,000 ms |
コード長 | 1,058 bytes |
コンパイル時間 | 2,310 ms |
コンパイル使用メモリ | 193,524 KB |
最終ジャッジ日時 | 2025-02-10 15:33:43 |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 8 |
ソースコード
#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'; } }