結果

問題 No.2296 Union Path Query (Hard)
ユーザー t98slidert98slider
提出日時 2023-02-28 20:27:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 443 ms / 7,000 ms
コード長 3,867 bytes
コンパイル時間 2,363 ms
コンパイル使用メモリ 191,320 KB
実行使用メモリ 37,012 KB
最終ジャッジ日時 2023-08-15 01:29:38
合計ジャッジ時間 20,265 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 19 ms
28,068 KB
testcase_02 AC 19 ms
27,952 KB
testcase_03 AC 19 ms
28,052 KB
testcase_04 AC 206 ms
31,160 KB
testcase_05 AC 204 ms
31,296 KB
testcase_06 AC 275 ms
32,784 KB
testcase_07 AC 293 ms
34,148 KB
testcase_08 AC 300 ms
34,148 KB
testcase_09 AC 224 ms
20,856 KB
testcase_10 AC 237 ms
20,896 KB
testcase_11 AC 232 ms
20,920 KB
testcase_12 AC 216 ms
18,848 KB
testcase_13 AC 93 ms
4,592 KB
testcase_14 AC 88 ms
4,380 KB
testcase_15 AC 316 ms
33,272 KB
testcase_16 AC 277 ms
26,540 KB
testcase_17 AC 152 ms
10,880 KB
testcase_18 AC 412 ms
34,936 KB
testcase_19 AC 272 ms
19,040 KB
testcase_20 AC 414 ms
34,996 KB
testcase_21 AC 367 ms
33,436 KB
testcase_22 AC 358 ms
33,412 KB
testcase_23 AC 223 ms
35,904 KB
testcase_24 AC 179 ms
19,176 KB
testcase_25 AC 188 ms
34,356 KB
testcase_26 AC 190 ms
34,436 KB
testcase_27 AC 388 ms
34,408 KB
testcase_28 AC 385 ms
34,404 KB
testcase_29 AC 443 ms
35,360 KB
testcase_30 AC 438 ms
35,372 KB
testcase_31 AC 400 ms
32,720 KB
testcase_32 AC 383 ms
32,512 KB
testcase_33 AC 345 ms
31,664 KB
testcase_34 AC 279 ms
31,232 KB
testcase_35 AC 246 ms
31,064 KB
testcase_36 AC 239 ms
29,964 KB
testcase_37 AC 370 ms
23,772 KB
testcase_38 AC 368 ms
23,280 KB
testcase_39 AC 373 ms
23,736 KB
testcase_40 AC 377 ms
23,628 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 1 ms
4,384 KB
testcase_43 AC 1 ms
4,384 KB
testcase_44 AC 358 ms
36,256 KB
testcase_45 AC 365 ms
35,900 KB
testcase_46 AC 368 ms
37,012 KB
testcase_47 AC 378 ms
36,792 KB
testcase_48 AC 370 ms
36,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct problem_H {
    int MAX_LOGV, N;
    vector<vector<pair<int, int>>> G;
    vector<vector<int>> parent;
    vector<ll> dp;
    vector<int> depth;
    vector<int> parent_or_size;
    vector<ll> diameter;
    vector<array<int, 2>> diameter_vertex;
    queue<int> que;

    problem_H(int n) : N(n) {
        MAX_LOGV = __lg(N) + 1;
        G.resize(N);
        parent.resize(MAX_LOGV, vector<int>(N, -1));
        parent_or_size.resize(N, -1);
        dp.resize(N), depth.resize(N);
        diameter.resize(N);
        diameter_vertex.resize(N);
        for(int i = 0; i < N; i++) diameter_vertex[i] = {i, i};
    }

    int leader(int v){ return parent_or_size[v] < 0 ? v : parent_or_size[v]; }

    void merge(int u, int v, int w){
        int x = leader(u), y = leader(v);
        if(-parent_or_size[x] < -parent_or_size[y]) {
            swap(x, y);
            swap(u, v);
        }
        parent_or_size[x] += parent_or_size[y];

        int log_limit = __lg(-parent_or_size[x]) + 1;

        G[u].emplace_back(v, w);
        G[v].emplace_back(u, w);
        parent[0][v] = u;
        depth[v] = depth[u] + 1;
        dp[v] = dp[u] + w;

        que.emplace(v);

        while(!que.empty()){
            v = que.front();
            que.pop();
            parent_or_size[v] = x;
            for(int i = 0; i + 1 < log_limit; i++){
                if(parent[i][v] == -1) parent[i + 1][v] = -1;
                else parent[i + 1][v] = parent[i][parent[i][v]];
            }
            for(auto &edge : G[v]){
                tie(u, w) = edge;
                if(parent[0][v] == u) continue;
                que.emplace(u);
                parent[0][u] = v;
                depth[u] = depth[v] + 1;
                dp[u] = dp[v] + w;
            }
        }

        update_diameter(x, y);
    }

    void update_diameter(int x, int y){
        ll mx;
        int u, v;
        if(diameter[x] >= diameter[y]) {
            mx = diameter[x];
            u = diameter_vertex[x][0], v = diameter_vertex[x][1];
        } else {
            mx = diameter[y];
            u = diameter_vertex[y][0], v = diameter_vertex[y][1];
        }

        for(int i = 0; i < 2; i++){
            int v1 = diameter_vertex[x][i];
            for(int j = 0; j < 2; j++){
                int v2 = diameter_vertex[y][j];
                ll d = dist(v1, v2);
                if(d > mx){
                    mx = d;
                    u = v1, v = v2;
                }
            }
        }

        diameter[x] = mx;
        diameter_vertex[x] = {u, v};
    }

    long long dist(int u, int v){
        if(leader(u) != leader(v)) return -1;
        if(depth[v] < depth[u]) swap(v, u);
        long long res = dp[u] + dp[v];
        int d = depth[v] - depth[u];
        for(int i = MAX_LOGV - 1; i >= 0; i--){
            if(d >> i & 1) v = parent[i][v];
        }
        if(u == v) return res - 2 * dp[v];
        for(int i = MAX_LOGV - 1; i >= 0; i--){
            if(parent[i][v] != parent[i][u]){
                v = parent[i][v];
                u = parent[i][u];
            }
        }
        v = parent[0][v];
        return res - 2 * dp[v];
    }
};

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

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

    problem_H 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;
            ll d = uf.dist(u, v);
            cout << d << '\n';
            if(d != -1) (X += d) %= N;
        }else if(type == 3){
            cin >> v;
            cout << uf.diameter[uf.leader(v)] << '\n';
        }else{
            cin >> v;
            X += v;
            if(X >= N) X -= N;
        }
    }
}
0