結果

問題 No.1054 Union add query
ユーザー minatominato
提出日時 2020-05-15 23:26:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 385 ms / 2,000 ms
コード長 1,798 bytes
コンパイル時間 3,294 ms
コンパイル使用メモリ 207,816 KB
実行使用メモリ 64,832 KB
最終ジャッジ日時 2023-10-19 17:20:44
合計ジャッジ時間 6,581 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 292 ms
23,204 KB
testcase_04 AC 385 ms
64,832 KB
testcase_05 AC 193 ms
13,308 KB
testcase_06 AC 223 ms
33,832 KB
testcase_07 AC 202 ms
33,832 KB
testcase_08 AC 219 ms
33,832 KB
testcase_09 AC 237 ms
56,060 KB
testcase_10 AC 147 ms
56,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ull = unsigned long long; 
using pii =  pair<int, int>;
using pll =  pair<long long, long long>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007LL;
//constexpr long long MOD = 998244353LL;
template<class T, class U> inline bool chmax(T &a, U b) { if (a < b) { a = b; return true;} return false; }
template<class T, class U> inline bool chmin(T &a, U b) { if (a > b) { a = b; return true;} return false; }
////////////////////////////////////////////////////////////////////////////////////////////////////////////

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int N,Q; cin >> N >> Q;
    vector<ll> arr(N);
    vector<int> root(N);
    vector<set<int>> dic(N);
    iota(all(root),0);
    rep(i,N) dic[i].emplace(i);
    while (Q--) {
        int T,A,B; cin >> T >> A >> B;
        if (T==1) {
            A--; B--;
            if (root[A] == root[B]) continue;
            if (dic[root[A]].size() > dic[root[B]].size()) swap(A,B);
            int r = root[A];
            ll val = arr[root[A]];
            for (auto it : dic[root[A]]) {
                if (it==r) arr[it] -= arr[root[B]];
                else arr[it] += val - arr[root[B]];
                root[it] = root[B];
                dic[root[B]].emplace(it);
            }
        } else if (T==2) {
            A--; 
            arr[root[A]] += B;
        } else {
            A--;
            if (A==root[A]) cout << arr[A] << ln;
            else cout << arr[A] + arr[root[A]] << ln;
        }
    }
}
0