結果

問題 No.2295 Union Path Query (Medium)
ユーザー t98slidert98slider
提出日時 2023-02-16 07:44:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 96 ms / 4,000 ms
コード長 1,673 bytes
コンパイル時間 4,302 ms
コンパイル使用メモリ 230,164 KB
実行使用メモリ 5,760 KB
最終ジャッジ日時 2024-05-02 03:27:07
合計ジャッジ時間 13,604 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 96 ms
5,376 KB
testcase_04 AC 90 ms
5,376 KB
testcase_05 AC 76 ms
5,376 KB
testcase_06 AC 72 ms
5,376 KB
testcase_07 AC 92 ms
5,376 KB
testcase_08 AC 83 ms
5,632 KB
testcase_09 AC 82 ms
5,632 KB
testcase_10 AC 82 ms
5,760 KB
testcase_11 AC 89 ms
5,760 KB
testcase_12 AC 86 ms
5,632 KB
testcase_13 AC 84 ms
5,632 KB
testcase_14 AC 72 ms
5,632 KB
testcase_15 AC 61 ms
5,632 KB
testcase_16 AC 62 ms
5,760 KB
testcase_17 AC 69 ms
5,632 KB
testcase_18 AC 85 ms
5,760 KB
testcase_19 AC 86 ms
5,504 KB
testcase_20 AC 79 ms
5,760 KB
testcase_21 AC 82 ms
5,632 KB
testcase_22 AC 84 ms
5,632 KB
testcase_23 AC 89 ms
5,632 KB
testcase_24 AC 83 ms
5,504 KB
testcase_25 AC 84 ms
5,504 KB
testcase_26 AC 90 ms
5,760 KB
testcase_27 AC 87 ms
5,504 KB
testcase_28 AC 85 ms
5,632 KB
testcase_29 AC 85 ms
5,632 KB
testcase_30 AC 86 ms
5,760 KB
testcase_31 AC 87 ms
5,504 KB
testcase_32 AC 85 ms
5,632 KB
testcase_33 AC 89 ms
5,632 KB
testcase_34 AC 86 ms
5,632 KB
testcase_35 AC 85 ms
5,760 KB
testcase_36 AC 83 ms
5,632 KB
testcase_37 AC 84 ms
5,632 KB
testcase_38 AC 82 ms
5,632 KB
testcase_39 AC 83 ms
5,760 KB
testcase_40 AC 84 ms
5,632 KB
testcase_41 AC 85 ms
5,632 KB
testcase_42 AC 82 ms
5,632 KB
testcase_43 AC 84 ms
5,504 KB
testcase_44 AC 83 ms
5,632 KB
testcase_45 AC 86 ms
5,632 KB
testcase_46 AC 87 ms
5,632 KB
testcase_47 AC 87 ms
5,504 KB
testcase_48 AC 91 ms
5,632 KB
testcase_49 AC 94 ms
5,632 KB
testcase_50 AC 79 ms
5,504 KB
testcase_51 AC 83 ms
5,632 KB
testcase_52 AC 85 ms
5,632 KB
testcase_53 AC 84 ms
5,504 KB
testcase_54 AC 90 ms
5,632 KB
testcase_55 AC 85 ms
5,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using ll = long long;
using mint = atcoder::modint998244353;

struct problem_G{
    int N;
    vector<int> weight;
    vector<int> parent_or_size;
    vector<mint> ans;

    problem_G(int n) : N(n), weight(N, 1 << 30), parent_or_size(N, -1), ans(N) {}

    int leader(int v){
        while(parent_or_size[v] >= 0) v = parent_or_size[v];
        return v;
    }

    void merge(int u, int v, int w){
        int x = leader(u), y = leader(v);
        if(x == y) return;
        if(-parent_or_size[x] < -parent_or_size[y]) swap(x, y);
        ans[x] += ans[y] + mint(w) * parent_or_size[x] * parent_or_size[y];
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        weight[y] = w;
    }

    int dist(int u, int v){
        int ans = 0;
        while(u != v){
            if(weight[u] < weight[v]) swap(u, v);
            ans = weight[v], v = parent_or_size[v];
            if(v < 0) return -1;
        }
        return ans;
    }
};

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

    int N, X, Q;
    cin >> N >> X >> Q;

    problem_G uf(N);

    int type, u, v, w;
    while(Q--){
        cin >> type;
        if(type == 1){
            cin >> v >> w;
            uf.merge(v, X, w);
        }else if(type == 2){
            cin >> u >> v;
            int d = uf.dist(u, v);
            cout << d << '\n';
            if(d != -1) (X += d) %= N;
        }else if(type == 3){
            cin >> v;
            cout << uf.ans[uf.leader(v)].val() << '\n';
        }else{
            cin >> v;
            X += v;
            if(X >= N) X -= N;
        }
    }
}
0