結果

問題 No.2294 Union Path Query (Easy)
ユーザー milanis48663220milanis48663220
提出日時 2023-05-05 23:18:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 3,792 bytes
コンパイル時間 3,776 ms
コンパイル使用メモリ 128,456 KB
実行使用メモリ 54,092 KB
最終ジャッジ日時 2023-08-15 06:29:50
合計ジャッジ時間 22,805 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 WA -
testcase_09 WA -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 523 ms
46,116 KB
testcase_17 AC 451 ms
54,092 KB
testcase_18 WA -
testcase_19 AC 478 ms
50,812 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 RE -
testcase_25 AC 2 ms
4,384 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 AC 266 ms
46,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#include <atcoder/modint>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

struct UnionFind {
    vector<int> data;
    UnionFind(int size) : data(size, -1) {}
    bool unionSet(int x, int y) {
        x = root(x); y = root(y);
        if (x != y) {
        if (data[y] < data[x]) swap(x, y);
        data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    bool findSet(int x, int y) {
        return root(x) == root(y);
    }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x) {
        return -data[root(x)];
    }
};

using mint = atcoder::modint998244353;

ostream& operator<<(ostream& os, const mint& m){
    os << m.val();
    return os;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, x, q; cin >> n >> x >> q;
    UnionFind uf(n);
    vector<int> xo(n);
    auto cnt = vec2d(n, 30, 0);
    vector<vector<int>> components(n);
    for(int i = 0; i < n; i++) components[i].push_back(i);
    while(q--){
        int t; cin >> t;
        // debug_value(x)
        // print_vector(xo);
        if(t == 1){
            int u, w; cin >> u >> w;
            int us = uf.size(u);
            int ux = uf.size(x);
            int cur_xor = xo[u]^xo[x];
            int diff = cur_xor^w;
            int rx = ux > us ? uf.root(x) : uf.root(u);
            int ru = ux > us ? uf.root(u) : uf.root(x);
            // cout << u << "->" << x << endl;
            for(int v: components[ru]){
                xo[v] ^= diff;
                for(int j = 0; j < 30; j++){
                    if(xo[v]&(1<<j)){
                        cnt[rx][j]++;
                    }
                }
                components[rx].push_back(v);
            }
            uf.unionSet(u, x);
            int r = uf.root(u);
            cnt[r] = cnt[rx];
            components[r] = components[rx];
            components[us].clear();
        }else if(t == 2){
            int u, v; cin >> u >> v;
            if(uf.findSet(u, v)){
                int dist = xo[u]^xo[v];
                cout << dist << endl;
                x = (x+dist)%n;
            }else{
                cout << -1 << endl;
            }
        }else if(t == 3){
            int v; cin >> v;
            int r = uf.root(v);
            mint ans = 0;
            int m = uf.size(r);
            for(int j = 0; j < 30; j++){
                int c1 = cnt[r][j];
                int c0 = m-c1;
                ans += mint(1<<j)*mint(c0)*mint(c1);
            }
            cout << ans << endl;
        }else{
            int v; cin >> v;
            x = (x+v)%n;
        }
    }
}
0