結果

問題 No.1054 Union add query
ユーザー carrot46carrot46
提出日時 2020-05-15 22:31:06
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 391 ms / 2,000 ms
コード長 2,351 bytes
コンパイル時間 1,896 ms
コンパイル使用メモリ 176,536 KB
実行使用メモリ 52,180 KB
最終ジャッジ日時 2023-10-19 15:21:11
合計ジャッジ時間 5,138 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 279 ms
15,812 KB
testcase_04 AC 387 ms
48,372 KB
testcase_05 AC 233 ms
10,572 KB
testcase_06 AC 208 ms
25,060 KB
testcase_07 AC 160 ms
25,060 KB
testcase_08 AC 189 ms
25,060 KB
testcase_09 AC 391 ms
50,484 KB
testcase_10 AC 156 ms
52,180 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N,M,H,W,Q,K,A,B;
string S;
const ll MOD = 998244353;
//const ll MOD = (1e+9) + 7;
typedef pair<ll, ll> P;
const ll INF = (1LL<<60);

int main() {
    cin>>N>>Q;
    class union_find{
            ll nn;
            vector<long> parent, tree_size, when_parent;
            vector<vector<P>> v_lib;
        public:
            union_find(unsigned long _n) : parent(_n), tree_size(_n, 1),when_parent(_n, -1), v_lib(_n, vector<P>(1, P(-1,0))), nn(_n){
                for(unsigned long i = 0; i < _n; ++i)parent[i] = i;
            }
            ll find(ll x){
                while(parent[x] != x)x = parent[x];
                return x;
            }
            ll score(ll x, ll id){
                ll score = v_lib[x][v_lib[x].size() - 1].se;
                //cout<<id<<' '<<x<<endl;
                while(parent[x] != x){
                    auto ite = lower_bound(ALL(v_lib[parent[x]]), P(when_parent[x], -1));
                    x = parent[x];
                    --ite;
                    //cout<<score<<' '<<v_lib[x][v_lib[x].size() - 1].se<<' '<<ite->se<<endl;
                    score += v_lib[x][v_lib[x].size() - 1].se - ite->se;
                }
                return score;
            }
            void add(ll x, ll b, ll id){
                x = find(x);
                v_lib[x].emplace_back(id, b + v_lib[x][v_lib[x].size() - 1].se);
            }
            void merge(ll a, ll b, ll id){
                a = find(a);
                b = find(b);
    	        if(a==b) return;
                if(tree_size[a] < tree_size[b])swap(a, b);
                tree_size[a] += tree_size[b];
                parent[b] = a;
                when_parent[b] = id;
            }
        };
    union_find uf(N + 1);
    rep(i,Q){
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        if(a == 1){
            uf.merge(b, c, i);
        }else if(a == 2){
            uf.add(b, c, i);
        }else{
            printf("%lld\n", uf.score(b, i));
        }
    }
}
0