結果

問題 No.2295 Union Path Query (Medium)
ユーザー t98slidert98slider
提出日時 2023-02-12 04:25:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 102 ms / 4,000 ms
コード長 2,106 bytes
コンパイル時間 4,103 ms
コンパイル使用メモリ 230,616 KB
実行使用メモリ 5,760 KB
最終ジャッジ日時 2024-05-02 03:27:23
合計ジャッジ時間 13,733 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

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

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

    //ここからUnion Find
    vector<int> parent_or_size(N, -1);
    vector<int> weight(N, 1 << 30); //親への辺の重み,初期値は大きい値を入れておく
    vector<mint> ans(N); //連結成分内の頂点対の距離の総和
    auto leader = [&](int v){
        while(parent_or_size[v] >= 0) v = parent_or_size[v];
        return v;
    };

    //distanceは頂点u, 頂点v 間のmax距離を求める関数
    //頂点u, 頂点v から開始して親への辺の重みが小さい順に親を辿る
    //parent_or_size[u] が負の値のときに代表元を越えた判定になるので負の値になったら非連結
    auto distance = [&](int u, int v){
        int ans = 0;
        while(u != v){
            if (weight[u] > weight[v]) swap(u, v);
            ans = weight[u], u = parent_or_size[u];
            if(u < 0) return -1;
        }
        return ans;
    };

    //頂点u, 頂点v を重み w の辺でマージ
    auto 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(parent_or_size[x]) * parent_or_size[y] * w;
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        weight[y] = w; //親への辺の重みを w に更新
    };
    //ここまでUnion Find



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