結果

問題 No.1054 Union add query
ユーザー pockynypockyny
提出日時 2022-09-22 14:42:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 337 ms / 2,000 ms
コード長 948 bytes
コンパイル時間 639 ms
コンパイル使用メモリ 76,356 KB
実行使用メモリ 42,968 KB
最終ジャッジ日時 2023-08-23 20:31:37
合計ジャッジ時間 3,606 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
18,600 KB
testcase_01 AC 6 ms
18,836 KB
testcase_02 AC 6 ms
18,652 KB
testcase_03 AC 182 ms
26,156 KB
testcase_04 AC 337 ms
42,968 KB
testcase_05 AC 149 ms
23,336 KB
testcase_06 AC 162 ms
29,148 KB
testcase_07 AC 139 ms
29,140 KB
testcase_08 AC 156 ms
29,184 KB
testcase_09 AC 248 ms
40,380 KB
testcase_10 AC 129 ms
38,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;
vector<int> uf[500010];
ll w[500010] = {},par[500010],parw[500010] = {};
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int i,n,q; cin >> n >> q;
    for(i=0;i<n;i++){
        par[i] = i; uf[i].push_back(i);
    }
    for(i=0;i<q;i++){
        int t,a,b; cin >> t >> a >> b;
        if(t==1){
            a--; b--;
            a = par[a]; b = par[b];
            if(a==b) continue;
            if(uf[a].size()<uf[b].size()) swap(a,b);
            for(int x:uf[b]){
                w[x] -= parw[a];
                w[x] += parw[b];
                par[x] = a;
            }
            uf[a].insert(uf[a].end(),uf[b].begin(),uf[b].end());
            uf[b].clear();
        }
        if(t==2){
            a--; parw[par[a]] += b;
        }
        if(t==3){
            a--;
            cout << parw[par[a]] + w[a] << "\n";
        }
    }
}
0