結果

問題 No.2295 Union Path Query (Medium)
ユーザー t98slidert98slider
提出日時 2023-02-12 04:12:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 104 ms / 4,000 ms
コード長 4,186 bytes
コンパイル時間 2,209 ms
コンパイル使用メモリ 167,532 KB
実行使用メモリ 5,660 KB
最終ジャッジ日時 2023-08-14 15:22:01
合計ジャッジ時間 21,590 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 95 ms
4,376 KB
testcase_04 AC 92 ms
4,380 KB
testcase_05 AC 81 ms
4,432 KB
testcase_06 AC 76 ms
4,380 KB
testcase_07 AC 95 ms
4,380 KB
testcase_08 AC 93 ms
5,364 KB
testcase_09 AC 97 ms
5,484 KB
testcase_10 AC 93 ms
5,396 KB
testcase_11 AC 99 ms
5,480 KB
testcase_12 AC 97 ms
5,488 KB
testcase_13 AC 92 ms
5,372 KB
testcase_14 AC 78 ms
5,476 KB
testcase_15 AC 68 ms
5,360 KB
testcase_16 AC 71 ms
5,480 KB
testcase_17 AC 79 ms
5,352 KB
testcase_18 AC 94 ms
5,600 KB
testcase_19 AC 98 ms
5,356 KB
testcase_20 AC 91 ms
5,364 KB
testcase_21 AC 94 ms
5,560 KB
testcase_22 AC 100 ms
5,356 KB
testcase_23 AC 104 ms
5,660 KB
testcase_24 AC 95 ms
5,572 KB
testcase_25 AC 86 ms
5,368 KB
testcase_26 AC 92 ms
5,404 KB
testcase_27 AC 92 ms
5,360 KB
testcase_28 AC 92 ms
5,480 KB
testcase_29 AC 92 ms
5,404 KB
testcase_30 AC 92 ms
5,356 KB
testcase_31 AC 94 ms
5,488 KB
testcase_32 AC 92 ms
5,412 KB
testcase_33 AC 92 ms
5,348 KB
testcase_34 AC 92 ms
5,408 KB
testcase_35 AC 94 ms
5,476 KB
testcase_36 AC 94 ms
5,408 KB
testcase_37 AC 92 ms
5,484 KB
testcase_38 AC 91 ms
5,412 KB
testcase_39 AC 92 ms
5,416 KB
testcase_40 AC 91 ms
5,356 KB
testcase_41 AC 92 ms
5,404 KB
testcase_42 AC 92 ms
5,352 KB
testcase_43 AC 92 ms
5,348 KB
testcase_44 AC 90 ms
5,480 KB
testcase_45 AC 92 ms
5,412 KB
testcase_46 AC 97 ms
5,412 KB
testcase_47 AC 96 ms
5,360 KB
testcase_48 AC 101 ms
5,360 KB
testcase_49 AC 99 ms
5,480 KB
testcase_50 AC 85 ms
5,352 KB
testcase_51 AC 88 ms
5,412 KB
testcase_52 AC 87 ms
5,400 KB
testcase_53 AC 92 ms
5,564 KB
testcase_54 AC 95 ms
5,488 KB
testcase_55 AC 92 ms
5,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

template<const unsigned int MOD> struct prime_modint {
    using mint = prime_modint;
    unsigned int v;
    prime_modint() : v(0) {}
    prime_modint(unsigned int a) { a %= MOD; v = a; }
    prime_modint(int a) { a %= (int)(MOD); if(a < 0)a += MOD; v = a; }
    prime_modint(long long a) { a %= (int)(MOD); if(a < 0)a += MOD; v = a; }
    static constexpr int mod() { return MOD; }
    mint& operator++() {v++; if(v == MOD)v = 0; return *this;}
    mint& operator--() {if(v == 0)v = MOD; v--; return *this;}
    mint operator++(int) { mint result = *this; ++*this; return result; }
    mint operator--(int) { mint result = *this; --*this; return result; }
    mint& operator+=(const mint& rhs) { v += rhs.v; if(v >= MOD) v -= MOD; return *this; }
    mint& operator-=(const mint& rhs) { if(v < rhs.v) v += MOD; v -= rhs.v; return *this; }
    mint& operator*=(const mint& rhs) {
        v = (unsigned int)((unsigned long long)(v) * rhs.v % MOD);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }
    mint pow(long long n) const {
        assert(0 <= n);
        mint r = 1, x = *this;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const { assert(v); return pow(MOD - 2); }
    friend mint operator+(const mint& lhs, const mint& rhs) { return mint(lhs) += rhs; }
    friend mint operator-(const mint& lhs, const mint& rhs) { return mint(lhs) -= rhs; }
    friend mint operator*(const mint& lhs, const mint& rhs) { return mint(lhs) *= rhs; }
    friend mint operator/(const mint& lhs, const mint& rhs) { return mint(lhs) /= rhs; }
    friend bool operator==(const mint& lhs, const mint& rhs) { return (lhs.v == rhs.v); }
    friend bool operator!=(const mint& lhs, const mint& rhs) { return (lhs.v != rhs.v); }
    friend std::ostream& operator << (std::ostream &os, const mint& rhs) noexcept { return os << rhs.v; }
};
//using mint = prime_modint<1000000007>;
using mint = prime_modint<998244353>;

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)] << '\n';
        }else{
            cin >> v;
            (X += v) %= N;
        }
    }
}
0