結果

問題 No.1054 Union add query
ユーザー commycommy
提出日時 2021-12-05 02:08:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 4,795 bytes
コンパイル時間 2,677 ms
コンパイル使用メモリ 110,164 KB
実行使用メモリ 55,168 KB
最終ジャッジ日時 2023-09-21 13:13:23
合計ジャッジ時間 6,152 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 181 ms
20,972 KB
testcase_04 AC 271 ms
55,168 KB
testcase_05 AC 167 ms
15,172 KB
testcase_06 AC 175 ms
33,476 KB
testcase_07 AC 158 ms
33,528 KB
testcase_08 AC 171 ms
33,512 KB
testcase_09 AC 200 ms
38,708 KB
testcase_10 AC 115 ms
38,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<int, int>;

// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; debug_print(__VA_ARGS__); } while(false)
template<typename T, typename... Ts> void debug_print(const T &t, const Ts &...ts) { cerr << t; ((cerr << ", " << ts), ...); cerr << endl; }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, const T& b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, const T& b) { if (a < b) {a = b; return true; } return false; }
template<typename T, typename... Ts> void print(const T& t, const Ts&... ts) { cout << t; ((cout << ' ' << ts), ...); cout << '\n'; }
template<typename... Ts> void input(Ts&... ts) { (cin >> ... >> ts); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
// clang-format on

class UnionFind {
public:
    vector<int> par;
    int N;
    UnionFind(int n) {
        par.resize(n, -1);
        N = n;
    }
    int Find(int n) {
        return par[n] < 0 ? n : par[n] = Find(par[n]);
    }
    bool Union(int x, int y) {
        x = Find(x);
        y = Find(y);
        if (x == y) return false;
        if (-par[x] > -par[y]) swap(x, y);
        par[y] += par[x];
        par[x] = y;
        N--;
        return true;
    }
    bool Same(int x, int y) {
        return Find(x) == Find(y);
    }
    int size(int x) {
        return -par[Find(x)];
    }
    int size() {
        return N;
    }
};

#include <functional>
template<typename T>
class SegmentTreeAllOne {
    using Func = function<T(T, T)>;

public:
    vector<T> data;
    int n;
    T init;
    Func update_func;
    SegmentTreeAllOne(int _n, T _init, Func up) {
        init = _init;
        update_func = up;
        for (n = 1; n < _n; n *= 2)
            ;
        data.resize(2 * n - 1, init);
    }
    void update(int l, int r, T val) {
        for (l += n - 1, r += n - 1; l < r; l = l / 2, r = (r - 1) / 2) {
            if (!(l & 1)) {
                data[l] = update_func(data[l], val);
            }
            if (!(r & 1)) {
                data[r - 1] = update_func(data[r - 1], val);
            }
        }
    }
    T query(int pos) {
        pos += n - 1;
        T res = data[pos];
        while (pos > 0) {
            pos = (pos - 1) / 2;
            res = update_func(res, data[pos]);
        }
        return res;
    }
};

using Query = tuple<int, int, int>;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, q;
    cin, n, q;
    vector<Query> query(q);
    rep(i, 0, q) {
        int t, a, b;
        cin, t, a, b;
        if (t == 1) {
            b--;
        }
        a--;
        query[i] = Query(t, a, b);
    }
    UnionFind uf(n);
    vector<vector<int>> tree(n);
    rep(i, 0, q) {
        auto &[t, a, b] = query[i];
        if (t == 1) {
            a = uf.Find(a);
            b = uf.Find(b);
            if (a != b) {
                uf.Union(a, b);
                tree[a].push_back(b);
                tree[b].push_back(a);
            }
        } else if (t == 2) {
            a = uf.Find(a);
        }
    }

    vector<int> flatten;
    vector<P> ranges(n, P(-1, -1));
    auto dfs = [&](auto &f, int v, int p) -> void {
        ranges[v].first = flatten.size();
        flatten.push_back(v);
        ranges[v].second = flatten.size();
        for (auto nv : tree[v]) {
            if (nv == p) continue;
            f(f, nv, v);
            flatten.push_back(v);
        }
    };

    vector<int> used(n, false);
    rep(i, 0, n) {
        int p = uf.Find(i);
        if (!used[p]) {
            used[p] = true;
            dfs(dfs, p, -1);
        }
    }

    auto SUM = [&](ll a, ll b) { return a + b; };
    SegmentTreeAllOne<ll> seg(flatten.size(), 0LL, SUM);

    for (auto [t, a, b] : query) {
        if (t == 1) {
            P p1 = ranges[a], p2 = ranges[b];
            if (p1.first < p2.first) {
                ranges[a] = P(p1.first, max(p1.second, p2.second));
            } else {
                ranges[b] = P(p2.first, max(p1.second, p2.second));
            }

        } else if (t == 2) {
            auto [l, r] = ranges[a];
            seg.update(l, r, b);
        } else {
            auto [l, _] = ranges[a];
            print(l == -1 ? 0 : seg.query(l));
        }
    }

    return 0;
}
0