結果

問題 No.1054 Union add query
ユーザー tapienrutapienru
提出日時 2020-05-16 19:50:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 326 ms / 2,000 ms
コード長 1,025 bytes
コンパイル時間 2,420 ms
コンパイル使用メモリ 208,780 KB
実行使用メモリ 37,092 KB
最終ジャッジ日時 2023-10-23 23:25:54
合計ジャッジ時間 5,390 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 180 ms
11,016 KB
testcase_04 AC 326 ms
37,092 KB
testcase_05 AC 148 ms
7,304 KB
testcase_06 AC 157 ms
17,604 KB
testcase_07 AC 142 ms
17,604 KB
testcase_08 AC 156 ms
17,604 KB
testcase_09 AC 210 ms
36,504 KB
testcase_10 AC 140 ms
36,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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';
        }
    }
}
0