結果

問題 No.2640 traO Stamps
ユーザー tentententen
提出日時 2024-02-29 14:23:24
言語 Java21
(openjdk 21)
結果
AC  
実行時間 729 ms / 2,000 ms
コード長 3,880 bytes
コンパイル時間 2,491 ms
コンパイル使用メモリ 78,956 KB
実行使用メモリ 72,384 KB
最終ジャッジ日時 2024-02-29 14:23:50
合計ジャッジ時間 25,286 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
54,000 KB
testcase_01 AC 53 ms
53,984 KB
testcase_02 AC 53 ms
53,988 KB
testcase_03 AC 53 ms
53,984 KB
testcase_04 AC 54 ms
53,976 KB
testcase_05 AC 55 ms
53,988 KB
testcase_06 AC 675 ms
71,376 KB
testcase_07 AC 596 ms
67,544 KB
testcase_08 AC 707 ms
68,852 KB
testcase_09 AC 578 ms
67,156 KB
testcase_10 AC 642 ms
68,300 KB
testcase_11 AC 639 ms
67,132 KB
testcase_12 AC 649 ms
69,768 KB
testcase_13 AC 612 ms
67,176 KB
testcase_14 AC 655 ms
68,396 KB
testcase_15 AC 642 ms
67,256 KB
testcase_16 AC 610 ms
69,188 KB
testcase_17 AC 651 ms
69,344 KB
testcase_18 AC 635 ms
67,488 KB
testcase_19 AC 659 ms
69,184 KB
testcase_20 AC 688 ms
68,880 KB
testcase_21 AC 603 ms
67,108 KB
testcase_22 AC 663 ms
67,796 KB
testcase_23 AC 679 ms
68,056 KB
testcase_24 AC 668 ms
66,416 KB
testcase_25 AC 680 ms
68,244 KB
testcase_26 AC 625 ms
71,312 KB
testcase_27 AC 628 ms
71,656 KB
testcase_28 AC 594 ms
71,648 KB
testcase_29 AC 621 ms
72,384 KB
testcase_30 AC 700 ms
69,280 KB
testcase_31 AC 690 ms
69,128 KB
testcase_32 AC 720 ms
66,956 KB
testcase_33 AC 729 ms
69,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        int k = sc.nextInt();
        int[] stamps = new int[k + 1];
        for (int i = 0; i <= k; i++) {
            stamps[i] = sc.nextInt() - 1;
        }
        long[][] distances = new long[n][n];
        for (int i = 0; i < n; i++) {
            Arrays.fill(distances[i], Long.MAX_VALUE / 2);
            distances[i][i] = 0;
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            int c = sc.nextInt();
            distances[a][b] = c;
            distances[b][a] = c;
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                for (int t = 0; t < n; t++) {
                    distances[j][t] = Math.min(distances[j][t], distances[j][i] + distances[i][t]);
                }
            }
        }
        BinaryIndexedTree bit = new BinaryIndexedTree(k + 1);
        long[] costs = new long[k + 1];
        for (int i = 1; i <= k; i++) {
            costs[i] = distances[stamps[i - 1]][stamps[i]];
            bit.add(i, costs[i]);
        }
        int q = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        while (q-- > 0) {
            if (sc.nextInt() == 1) {
                int x = sc.nextInt();
                int y = sc.nextInt() - 1;
                if (x > 0) {
                    bit.add(x, -costs[x]);
                    costs[x] = distances[stamps[x - 1]][y];
                    bit.add(x, costs[x]);
                }
                if (x < k) {
                    bit.add(x + 1, -costs[x + 1]);
                    costs[x + 1] = distances[y][stamps[x + 1]];
                    bit.add(x + 1, costs[x + 1]);
                }
                stamps[x] = y;
            } else {
                int x = sc.nextInt();
                int y = sc.nextInt();
                sb.append(bit.getSum(x + 1, y)).append("\n");
            }
        }
        System.out.print(sb);
    }
}
class BinaryIndexedTree {
    int size;
    long[] tree;
    
    public BinaryIndexedTree(int size) {
        this.size = size;
        tree = new long[size];
    }
    
    public void add(int idx, long value) {
        int mask = 1;
        while (idx < size) {
            if ((idx & mask) != 0) {
                tree[idx] += value;
                idx += mask;
            }
            mask <<= 1;
        }
    }
    
    public long getSum(int from, int to) {
        return getSum(to) - getSum(from - 1);
    }
    
    public long getSum(int x) {
        int mask = 1;
        long ans = 0;
        while (x > 0) {
            if ((x & mask) != 0) {
                ans += tree[x];
                x -= mask;
            }
            mask <<= 1;
        }
        return ans;
    }
}class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0