結果

問題 No.2296 Union Path Query (Hard)
ユーザー t98slidert98slider
提出日時 2023-02-15 08:48:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,314 ms / 7,000 ms
コード長 5,041 bytes
コンパイル時間 3,007 ms
コンパイル使用メモリ 203,904 KB
実行使用メモリ 67,264 KB
最終ジャッジ日時 2023-08-15 01:29:16
合計ジャッジ時間 35,651 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 40 ms
54,660 KB
testcase_02 AC 42 ms
54,548 KB
testcase_03 AC 40 ms
54,700 KB
testcase_04 AC 328 ms
58,552 KB
testcase_05 AC 333 ms
58,492 KB
testcase_06 AC 468 ms
60,992 KB
testcase_07 AC 454 ms
62,468 KB
testcase_08 AC 434 ms
62,724 KB
testcase_09 AC 336 ms
36,680 KB
testcase_10 AC 334 ms
36,628 KB
testcase_11 AC 364 ms
36,648 KB
testcase_12 AC 318 ms
32,360 KB
testcase_13 AC 103 ms
5,776 KB
testcase_14 AC 94 ms
4,600 KB
testcase_15 AC 522 ms
61,908 KB
testcase_16 AC 454 ms
47,876 KB
testcase_17 AC 210 ms
17,468 KB
testcase_18 AC 630 ms
63,840 KB
testcase_19 AC 406 ms
32,920 KB
testcase_20 AC 637 ms
63,792 KB
testcase_21 AC 582 ms
62,152 KB
testcase_22 AC 570 ms
61,652 KB
testcase_23 AC 273 ms
64,108 KB
testcase_24 AC 212 ms
32,452 KB
testcase_25 AC 370 ms
65,664 KB
testcase_26 AC 373 ms
65,640 KB
testcase_27 AC 1,138 ms
65,744 KB
testcase_28 AC 1,128 ms
65,692 KB
testcase_29 AC 1,267 ms
67,264 KB
testcase_30 AC 1,258 ms
67,244 KB
testcase_31 AC 1,314 ms
62,928 KB
testcase_32 AC 1,215 ms
62,376 KB
testcase_33 AC 1,088 ms
61,336 KB
testcase_34 AC 780 ms
60,380 KB
testcase_35 AC 741 ms
59,600 KB
testcase_36 AC 618 ms
57,700 KB
testcase_37 AC 1,164 ms
44,012 KB
testcase_38 AC 1,084 ms
42,576 KB
testcase_39 AC 1,153 ms
44,048 KB
testcase_40 AC 1,157 ms
44,080 KB
testcase_41 AC 2 ms
4,384 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 1 ms
4,380 KB
testcase_44 AC 554 ms
64,332 KB
testcase_45 AC 602 ms
64,364 KB
testcase_46 AC 620 ms
65,428 KB
testcase_47 AC 657 ms
66,476 KB
testcase_48 AC 626 ms
65,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <class S, S (*op)(S, S), S (*e)()> struct Multiple_LCA_Tree{
    int MAX_LOGV, N;
    std::vector<std::vector<std::pair<int, S>>> G; //重み付きグラフ
    std::vector<std::vector<int>> parent; //親の頂点のダブリング配列
    std::vector<std::vector<S>> dp; //辺の重みのダブリング配列
    std::vector<int> depth; //連結成分の代表元からの深さ
    std::vector<int> parent_or_size; //Union Find
    std::vector<std::tuple<S, int, int>> diameter_and_vertex; //直径と頂点を入れる
    std::queue<int> que; //BFS用
    Multiple_LCA_Tree(int _n) : N(_n){
        MAX_LOGV = std::__lg(N) + 1;
        G.resize(N);
        parent.resize(MAX_LOGV, std::vector<int>(N, -1));
        dp.resize(MAX_LOGV, std::vector<S>(N, e()));
        depth.resize(N);
        parent_or_size.resize(N, -1);
        diameter_and_vertex.resize(N);
        for(int i = 0; i < N; i++) diameter_and_vertex[i] = std::make_tuple(0, i, i);
    }
    int leader(int v){
        if(parent_or_size[v] < 0) return v;
        return parent_or_size[v] = leader(parent_or_size[v]);
    }
    //頂点uと頂点vを重さwの辺で結ぶ
    void merge(int u, int v, S w){
        int x = leader(u), y = leader(v);
        if(x == y) return;
        if(-parent_or_size[x] < -parent_or_size[y]) {
            std::swap(x, y);
            std::swap(u, v);
        }
        //x-u の連結成分側を親とする
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;

        //辺を追加してDFSでダブリングを更新
        G[u].emplace_back(v, w);
        G[v].emplace_back(u, w);

        register_parent(v, u, w);
        que.push(v);
        while(!que.empty()){
            v = que.front();
            update_dp_table(v);
            que.pop();
            for(auto &edge : G[v]){
                std::tie(u, w) = edge;
                if(parent[0][v] == u) continue;
                register_parent(u, v, w);
                que.push(u);
            }
        }

        update_diameter(x, y);
    }

    S dist(int u, int v){
        if(leader(u) != leader(v)) return -1ll;
        if(depth[u] > depth[v]) std::swap(u, v);
        S result = e();
        //頂点 v を頂点 u と高さが同じになるようにする
        for(int i = 0; i < MAX_LOGV; i++){
            if((depth[v] - depth[u]) >> i & 1){
                result = op(result, dp[i][v]);
                v = parent[i][v];
            }
        }
        if(u == v) return result;
        for(int i = MAX_LOGV - 1; i >= 0; i--){
            if(parent[i][u] != parent[i][v]){
                result = op(result, dp[i][u]);
                result = op(result, dp[i][v]);
                u = parent[i][u];
                v = parent[i][v];
            }
        }
        result = op(result, dp[0][u]);
        result = op(result, dp[0][v]);
        return result;
    }

    private:
    void update_dp_table(int v){
        for(int i = 0; i + 1 < MAX_LOGV; i++){
            if(parent[i][v] == -1) {
                parent[i + 1][v] = -1;
                dp[i + 1][v] = dp[i][v];
            } else {
                parent[i + 1][v] = parent[i][parent[i][v]];
                dp[i + 1][v] = op(dp[i][parent[i][v]], dp[i][v]);
            }
        }
    }
    void register_parent(int v, int _parent, S weight){
        dp[0][v] = weight;
        depth[v] = depth[_parent] + 1;
        parent[0][v] = _parent;
    }
    //yの連結成分とxの連結成分の直径を更新
    void update_diameter(int x, int y){
        S temp;
        std::array<int, 4> candidate_vertex{};
        std::tie(temp, candidate_vertex[0], candidate_vertex[1]) = diameter_and_vertex[x];
        std::tie(temp, candidate_vertex[2], candidate_vertex[3]) = diameter_and_vertex[y];
        std::tuple<S, int, int> result = std::max(diameter_and_vertex[x], diameter_and_vertex[y]);
        for(int i = 0; i < 2; i++) {
            int v1 = candidate_vertex[i];
            for(int j = 2; j < 4; j++) {
                int v2 = candidate_vertex[j];
                S length = dist(v1, v2);
                result = std::max(result, std::make_tuple(length, v1, v2));
            }
        }
        diameter_and_vertex[x] = result;
    }
};
ll op(ll lhs, ll rhs){ return lhs + rhs; }
ll e() { return 0ll; }

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll N, X, Q;
    cin >> N >> X >> Q;

    Multiple_LCA_Tree<ll, op, e> MLT(N);

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