結果

問題 No.2640 traO Stamps
ユーザー Today03Today03
提出日時 2024-02-19 22:49:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 262 ms / 2,000 ms
コード長 2,307 bytes
コンパイル時間 2,474 ms
コンパイル使用メモリ 209,552 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-02-19 22:49:52
合計ジャッジ時間 11,634 ms
ジャッジサーバーID
(参考情報)
judge16 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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 1 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 230 ms
6,676 KB
testcase_07 AC 223 ms
6,676 KB
testcase_08 AC 235 ms
6,676 KB
testcase_09 AC 235 ms
6,676 KB
testcase_10 AC 218 ms
6,676 KB
testcase_11 AC 216 ms
6,676 KB
testcase_12 AC 220 ms
6,676 KB
testcase_13 AC 186 ms
6,676 KB
testcase_14 AC 241 ms
6,676 KB
testcase_15 AC 206 ms
6,676 KB
testcase_16 AC 222 ms
6,676 KB
testcase_17 AC 209 ms
6,676 KB
testcase_18 AC 216 ms
6,676 KB
testcase_19 AC 237 ms
6,676 KB
testcase_20 AC 220 ms
6,676 KB
testcase_21 AC 211 ms
6,676 KB
testcase_22 AC 228 ms
6,676 KB
testcase_23 AC 216 ms
6,676 KB
testcase_24 AC 216 ms
6,676 KB
testcase_25 AC 231 ms
6,676 KB
testcase_26 AC 221 ms
6,676 KB
testcase_27 AC 249 ms
6,676 KB
testcase_28 AC 218 ms
6,676 KB
testcase_29 AC 245 ms
6,676 KB
testcase_30 AC 262 ms
6,676 KB
testcase_31 AC 249 ms
6,676 KB
testcase_32 AC 241 ms
6,676 KB
testcase_33 AC 236 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#ifdef LOCAL
#include "./debug.cpp"
#else
#define debug(...)
#define print_line
#endif
using namespace std;
using ll = long long;

template <typename T>
struct fenwick_tree {
    fenwick_tree(int n) {
        this->n = n;
        dat = vector<T>(n);
    }
    void add(int i, T x) {
        i++;
        while (i <= n) {
            dat[i - 1] += x;
            i += i & -i;
        }
    }
    T operator[](int i) {
        return sum(i, i + 1);
    }
    T sum(int l, int r) {
        return sum(r) - sum(l);
    }
    friend ostream &operator<<(ostream &os, fenwick_tree A) {
        int n = A.n;
        os << "[ ";
        for (int i = 0; i < n; i++) {
            os << A[i];
            if (i != n - 1) os << ", ";
        }
        os << " ]";
        return os;
    }

private:
    int n;
    vector<T> dat;
    T sum(int r) {
        T ret = 0;
        while (r > 0) {
            ret += dat[r - 1];
            r -= r & -r;
        }
        return ret;
    }
};

int main() {
    int N, M, K;
    cin >> N >> M >> K;
    vector<int> S(K + 1);
    for (int i = 0; i <= K; i++) cin >> S[i], S[i]--;
    vector<vector<ll>> G(N, vector<ll>(N, LLONG_MAX / 10));
    for (int i = 0; i < M; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        u--;
        v--;
        G[u][v] = G[v][u] = w;
    }
    for (int i = 0; i < N; i++) G[i][i] = 0;

    for (int k = 0; k < N; k++) {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
            }
        }
    }

    fenwick_tree<ll> fw(K);
    for (int i = 0; i < K; i++) fw.add(i, G[S[i]][S[i + 1]]);

    int Q;
    cin >> Q;
    while (Q--) {
        int t, x, y;
        cin >> t >> x >> y;

        if (t == 1) {
            y--;
            if (x > 0) {
                fw.add(x - 1, -G[S[x - 1]][S[x]]);
                fw.add(x - 1, G[S[x - 1]][y]);
            }
            if (x < K) {
                fw.add(x, -G[S[x]][S[x + 1]]);
                fw.add(x, G[y][S[x + 1]]);
            }
            S[x] = y;
        }

        else {
            if (x == y) {
                cout << 0 << '\n';
            } else {
                cout << fw.sum(x, y) << '\n';
            }
        }

        debug(fw);
    }
}
0