結果
| 問題 |
No.1054 Union add query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-05-16 19:50:16 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 405 ms / 2,000 ms |
| コード長 | 1,025 bytes |
| コンパイル時間 | 2,476 ms |
| コンパイル使用メモリ | 200,720 KB |
| 最終ジャッジ日時 | 2025-01-10 12:38:07 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 8 |
ソースコード
#pragma gcc optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, q; cin >> n >> q;
vector<int> p(n + 1, -1);
vector<int> sv(n + 1), dv(n + 1);
vector<vector<int>> s(n + 1);
for (int i = 1; i <= n; ++i) s[i].emplace_back(i);
function<int(int)> find = [&](int x) {
return p[x] < 0 ? x : p[x] = find(p[x]);
};
function<void(int, int)> merge = [&](int x, int y) {
if ((x = find(x)) == (y = find(y))) return;
if (p[x] > p[y]) swap(x, y);
p[x] += p[y];
p[y] = x;
for (auto e : s[y]) {
sv[e] += dv[y] - dv[x];
s[x].emplace_back(e);
}
s[y].clear();
};
while (q--) {
int t, a, b; cin >> t >> a >> b;
if (t == 1) {
merge(a, b);
}
if (t == 2) {
dv[find(a)] += b;
}
if (t == 3) {
cout << sv[a] + dv[find(a)] << '\n';
}
}
}