結果

問題 No.1054 Union add query
ユーザー 👑 hitonanodehitonanode
提出日時 2020-05-17 16:07:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 298 ms / 2,000 ms
コード長 5,630 bytes
コンパイル時間 2,306 ms
コンパイル使用メモリ 208,812 KB
実行使用メモリ 34,568 KB
最終ジャッジ日時 2023-10-26 00:29:40
合計ジャッジ時間 6,964 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 165 ms
14,384 KB
testcase_04 AC 298 ms
34,568 KB
testcase_05 AC 153 ms
11,760 KB
testcase_06 AC 154 ms
19,368 KB
testcase_07 AC 140 ms
19,368 KB
testcase_08 AC 152 ms
19,368 KB
testcase_09 AC 223 ms
34,568 KB
testcase_10 AC 160 ms
34,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template<typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template<typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); }
template<typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
template<typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template<typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;


// UnionFind Tree (0-indexed), based on size of each disjoint set
struct UnionFind
{
    std::vector<int> par, cou;
    std::vector<int> L, R;
    vector<int> conn;
    UnionFind(int N = 0) : par(N), cou(N, 1), L(N), R(N), conn(N, -1) {
        iota(par.begin(), par.end(), 0);
        iota(L.begin(), L.end(), 0);
        iota(R.begin(), R.end(), 0);
    }
    int find(int x) { return (par[x] == x) ? x : (par[x] = find(par[x])); }
    bool unite(int x, int y) {
        x = find(x), y = find(y);
        if (x == y) return false;
        if (cou[x] < cou[y]) std::swap(x, y);
        par[y] = x, cou[x] += cou[y];
        conn[R[x]] = L[y];
        R[x] = R[y];
        return true;
    }
    int count(int x) { return cou[find(x)]; }
    bool same(int x, int y) { return find(x) == find(y); }
};

// 1-indexed BIT (i : [1, len])
template <typename T>
struct BIT : std::vector<T>
{
    BIT(int len = 0) : std::vector<T>(len + 1) {}
    void reset() { fill(this->begin(), this->end(), 0); }
    void add(int pos, T v) {
        while (pos > 0 and pos < (int)this->size()) (*this)[pos] += v, pos += pos & -pos;
    }
    T sum(int pos) const { // (0, pos]
        T res = 0;
        while (pos > 0) res += (*this)[pos], pos -= pos & -pos;
        return res;
    }
    friend std::ostream &operator<<(std::ostream &os, const BIT &bit) {
        T prv = 0;
        os << '[';
        for (int i = 1; i < (int)bit.size(); i++) {
            T now = bit.sum(i);
            os << now - prv << ",";
            prv = now;
        }
        os << ']';
        return os;
    }
};

int main()
{
    int N, Q;
    cin >> N >> Q;
    vector<int> T(Q), A(Q), B(Q);
    UnionFind uf(N);
    REP(i, Q)
    {
        cin >> T[i] >> A[i] >> B[i];
        A[i]--, B[i]--;
        if (T[i] == 1) uf.unite(A[i], B[i]);
    }
    vector<int> prv(N, -1);
    REP(i, N) if (uf.conn[i] >= 0) prv[uf.conn[i]] = i;
    vector<int> ord;
    REP(i, N) if (prv[i] == -1)
    {
        int now = i;
        while (true)
        {
            ord.emplace_back(now);
            now = uf.conn[now];
            if (now == -1) break;
        }
    }
    vector<int> ordinv(N);
    REP(i, N) ordinv[ord[i]] = i;

    uf = UnionFind(N);
    BIT<int> bit(N);

    REP(i, Q)
    {
        if (T[i] == 1)
        {
            uf.unite(A[i], B[i]);
        }
        if (T[i] == 2)
        {
            int a = uf.find(A[i]);
            int l = ordinv[uf.L[a]] + 1, r = ordinv[uf.R[a]] + 1;
            int ad = B[i] + 1;
            bit.add(l, ad);
            bit.add(r + 1, -ad);
        }
        if (T[i] == 3)
        {
            int p = ordinv[A[i]];
            cout << bit.sum(p + 1) << '\n';
        }
    }
}
0