結果

問題 No.2640 traO Stamps
ユーザー ayataka5ayataka5
提出日時 2024-02-19 23:21:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 266 ms / 2,000 ms
コード長 2,454 bytes
コンパイル時間 2,311 ms
コンパイル使用メモリ 209,588 KB
実行使用メモリ 8,832 KB
最終ジャッジ日時 2024-02-19 23:22:07
合計ジャッジ時間 11,884 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 214 ms
7,680 KB
testcase_07 AC 235 ms
7,552 KB
testcase_08 AC 251 ms
8,448 KB
testcase_09 AC 219 ms
6,912 KB
testcase_10 AC 232 ms
8,320 KB
testcase_11 AC 203 ms
7,040 KB
testcase_12 AC 233 ms
8,448 KB
testcase_13 AC 197 ms
7,040 KB
testcase_14 AC 246 ms
8,320 KB
testcase_15 AC 221 ms
7,296 KB
testcase_16 AC 209 ms
7,936 KB
testcase_17 AC 223 ms
8,064 KB
testcase_18 AC 233 ms
7,296 KB
testcase_19 AC 240 ms
8,576 KB
testcase_20 AC 236 ms
8,448 KB
testcase_21 AC 217 ms
7,168 KB
testcase_22 AC 244 ms
8,448 KB
testcase_23 AC 220 ms
7,040 KB
testcase_24 AC 230 ms
7,168 KB
testcase_25 AC 234 ms
8,704 KB
testcase_26 AC 226 ms
8,576 KB
testcase_27 AC 230 ms
8,576 KB
testcase_28 AC 245 ms
8,576 KB
testcase_29 AC 250 ms
8,704 KB
testcase_30 AC 266 ms
8,832 KB
testcase_31 AC 264 ms
8,832 KB
testcase_32 AC 250 ms
8,192 KB
testcase_33 AC 252 ms
8,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const long long INF = (1LL << 60);

/* segment tree (noncommutative monoid-compatible) */
/*
    References:
    https://hcpc-hokudai.github.io/archive/structure_segtree_001.pdf
    https://hackmd.io/@tatyam-prime/rkA5wJMdo
    https://github.com/atcoder/ac-library/blob/master/atcoder/segtree.hpp  
*/

template<typename T>
struct SegmentTree {
    int n;
    vector<T> seg;

    /* ===== change here ===== */

    T op(T a, T b) { return a + b; }

    T e() { return (T)0; }

    /* ===== ===== ===== ===== */

    SegmentTree(int n): n(n), seg(n * 2, e()) {}

    T operator[](int i) const {
        return seg[n + i];
    }

    void update(int i, T x) {
        i += n;
        seg[i] = x;
        while(1 < i) {
            i >>= 1;
            seg[i] = op(seg[i << 1 | 0], seg[i << 1 | 1]);
        }
    }

    T prod(int l, int r) {
        T ansL(e()), ansR=(e());
        for(l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if(l & 1) ansL = op(ansL, seg[l++]);
            if(r & 1) ansR = op(seg[--r], ansR);
        }
        return op(ansL, ansR);
    }

    T all_prod() {
        return prod(0, n);
    }
};

int main() {
    int N, M, K;
    cin >> N >> M >> K;
    vector S(K+1, 0);
    for(int i = 0; i < K+1; i++) { cin >> S[i]; S[i]--; }
    vector A(M, 0), B(M, 0);
    vector C(M, 0LL);
    vector dist(N, vector(N, INF));
    for(int i = 0; i < M; i++) {
        cin >> A[i] >> B[i] >> C[i]; A[i]--, B[i]--;
        dist[A[i]][B[i]] = C[i];
        dist[B[i]][A[i]] = C[i];
    }
    for(int i = 0; i < N; i++) dist[i][i] = 0LL;
    for(int k = 0; k < N; k++) {
        for(int i = 0; i < N; i++) {
            for(int j = 0; j < N; j++) {
                dist[i][j] = min(dist[i][k] + dist[k][j], dist[i][j]);
            }
        }
    }
    int Q;
    cin >> Q;
    vector T(Q, 0), X(Q, 0), Y(Q, 0);
    for(int i = 0; i < Q; i++) cin >> T[i] >> X[i] >> Y[i];
    SegmentTree<long long> segtree(K);
    for(int i = 0; i < K; i++) { segtree.update(i, dist[S[i]][S[i+1]]); }
    for(int i = 0; i < Q; i++) {
        if(T[i] == 1) {
            Y[i]--; S[X[i]] = Y[i];
            if(0 <= X[i]-1) segtree.update(X[i]-1, dist[S[X[i]-1]][S[X[i]]]);
            if(X[i]+1 < K+1) segtree.update(X[i], dist[S[X[i]]][S[X[i]+1]]);
        }
        if(T[i] == 2) {
            long long ans(segtree.prod(X[i], Y[i]));
            cout << ans << endl;
        }
    }
    return 0;
}
0