結果

問題 No.2294 Union Path Query (Easy)
ユーザー t98slidert98slider
提出日時 2023-02-16 07:30:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 200 ms / 4,000 ms
コード長 1,971 bytes
コンパイル時間 3,739 ms
コンパイル使用メモリ 232,980 KB
実行使用メモリ 52,480 KB
最終ジャッジ日時 2024-05-02 03:15:41
合計ジャッジ時間 15,957 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 190 ms
52,352 KB
testcase_04 AC 142 ms
31,232 KB
testcase_05 AC 127 ms
27,904 KB
testcase_06 AC 193 ms
52,480 KB
testcase_07 AC 196 ms
52,480 KB
testcase_08 AC 195 ms
52,352 KB
testcase_09 AC 181 ms
52,352 KB
testcase_10 AC 200 ms
52,480 KB
testcase_11 AC 196 ms
52,480 KB
testcase_12 AC 197 ms
52,480 KB
testcase_13 AC 175 ms
52,480 KB
testcase_14 AC 145 ms
52,480 KB
testcase_15 AC 165 ms
52,480 KB
testcase_16 AC 141 ms
52,480 KB
testcase_17 AC 170 ms
52,480 KB
testcase_18 AC 166 ms
52,480 KB
testcase_19 AC 160 ms
52,480 KB
testcase_20 AC 156 ms
52,480 KB
testcase_21 AC 158 ms
52,480 KB
testcase_22 AC 149 ms
52,480 KB
testcase_23 AC 159 ms
52,480 KB
testcase_24 AC 155 ms
52,352 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 73 ms
5,760 KB
testcase_29 AC 80 ms
8,192 KB
testcase_30 AC 86 ms
10,624 KB
testcase_31 AC 95 ms
13,056 KB
testcase_32 AC 99 ms
15,488 KB
testcase_33 AC 103 ms
18,048 KB
testcase_34 AC 108 ms
20,480 KB
testcase_35 AC 117 ms
23,040 KB
testcase_36 AC 122 ms
25,344 KB
testcase_37 AC 132 ms
27,904 KB
testcase_38 AC 131 ms
30,336 KB
testcase_39 AC 138 ms
32,768 KB
testcase_40 AC 146 ms
35,200 KB
testcase_41 AC 144 ms
37,632 KB
testcase_42 AC 158 ms
40,064 KB
testcase_43 AC 156 ms
42,624 KB
testcase_44 AC 161 ms
45,056 KB
testcase_45 AC 169 ms
47,488 KB
testcase_46 AC 175 ms
50,048 KB
testcase_47 AC 188 ms
52,480 KB
testcase_48 AC 167 ms
52,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct problem_F {
    int N;
    vector<int> weight;
    vector<int> parent_or_size;
    vector<array<array<mint, 2>, 30>> cnt;
    vector<mint> ans;

    problem_F(int n) : N(n), weight(N), parent_or_size(N, -1), ans(N), cnt(N) {
        for(int i = 0; i < N; i++)
            for(int j = 0; j < 30; j++) cnt[i][j][0]++;
    }

    int leader(int v){ 
        if(parent_or_size[v] < 0) return v;
        int root = leader(parent_or_size[v]);
        weight[v] ^= weight[parent_or_size[v]];
        return parent_or_size[v] = root;
    }

    void merge(int u, int v, int w){
        int x = leader(u), y = leader(v);
        w ^= weight[u] ^ weight[v];
        if(-parent_or_size[x] < -parent_or_size[y]) swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        weight[y] = w;
        ans[x] = 0;
        for(int i = 0; i < 30; i++){
            for(int j = 0; j < 2; j++){
                cnt[x][i][j ^ (w >> i & 1)] += cnt[y][i][j];
            }
            ans[x] += cnt[x][i][0] * cnt[x][i][1] * (1 << i);
        }
    }

    long long dist(int u, int v){
        if(leader(u) != leader(v)) return -1;
        return weight[u] ^ weight[v];
    }
};

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

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

    problem_F 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