結果

問題 No.1054 Union add query
ユーザー outlineoutline
提出日時 2021-01-12 23:11:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 308 ms / 2,000 ms
コード長 2,395 bytes
コンパイル時間 1,368 ms
コンパイル使用メモリ 141,128 KB
実行使用メモリ 38,920 KB
最終ジャッジ日時 2023-08-13 16:38:22
合計ジャッジ時間 4,244 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 166 ms
11,140 KB
testcase_04 AC 308 ms
38,920 KB
testcase_05 AC 138 ms
7,204 KB
testcase_06 AC 144 ms
18,232 KB
testcase_07 AC 132 ms
18,232 KB
testcase_08 AC 145 ms
18,308 KB
testcase_09 AC 198 ms
38,096 KB
testcase_10 AC 133 ms
38,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
// constexpr int mod = 1000000007;
constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

struct UnionFind{
    int sz;
    vector<int> par;
    vector<int> rank;
    vector<int> weight;
    vector<int> lazy;
    vector<vector<int>> vs;

    UnionFind(int n) : sz(n) {
        par.resize(sz);
        rank.assign(sz, 0);
        weight.resize(sz);
        lazy.resize(sz);
        vs.resize(sz);
        for(int i = 0; i < sz; ++i){
            par[i] = i;
            vs[i].emplace_back(i);
        }
    }

    int find(int x){
        if(par[x] == x) return x;
        else    return par[x] = find(par[x]);
    }

    bool same(int x, int y){
        return find(x) == find(y);
    }

    bool unite(int x, int y){
        x = find(x), y = find(y);
        if(x == y)  return false;
        if(rank[x] < rank[y])   swap(x, y);
        par[y] = x;
        for(int v : vs[y]){
            weight[v] += lazy[y] - lazy[x];
            vs[x].emplace_back(v);
        }
        vs[y].clear();
        lazy[y] = 0;
        if(rank[x] == rank[y])  ++rank[x];
        return true;
    }

    void update(int x, int y){
        lazy[find(x)] += y;
    }

    int prod(int x){
        return weight[x] + lazy[find(x)];
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, Q, T, A, B;
    cin >> N >> Q;
    UnionFind uf(N + 1);
    for(int q = 0; q < Q; ++q){
        cin >> T >> A >> B;
        if(T == 1)  uf.unite(A, B);
        else if(T == 2) uf.update(A, B);
        else    cout << uf.prod(A) << '\n';
    }

    return 0;
}
0