結果

問題 No.2640 traO Stamps
ユーザー tentententen
提出日時 2024-02-29 14:23:24
言語 Java21
(openjdk 21)
結果
AC  
実行時間 586 ms / 2,000 ms
コード長 3,880 bytes
コンパイル時間 2,033 ms
コンパイル使用メモリ 79,180 KB
実行使用メモリ 64,176 KB
最終ジャッジ日時 2024-09-29 12:41:48
合計ジャッジ時間 20,394 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
44,332 KB
testcase_01 AC 47 ms
36,828 KB
testcase_02 AC 45 ms
37,196 KB
testcase_03 AC 46 ms
37,292 KB
testcase_04 AC 45 ms
36,796 KB
testcase_05 AC 45 ms
36,928 KB
testcase_06 AC 582 ms
57,012 KB
testcase_07 AC 514 ms
64,176 KB
testcase_08 AC 584 ms
54,716 KB
testcase_09 AC 470 ms
55,668 KB
testcase_10 AC 557 ms
53,012 KB
testcase_11 AC 529 ms
52,284 KB
testcase_12 AC 509 ms
55,012 KB
testcase_13 AC 517 ms
52,628 KB
testcase_14 AC 586 ms
53,976 KB
testcase_15 AC 527 ms
53,104 KB
testcase_16 AC 531 ms
54,232 KB
testcase_17 AC 552 ms
54,584 KB
testcase_18 AC 516 ms
52,788 KB
testcase_19 AC 550 ms
54,000 KB
testcase_20 AC 544 ms
54,516 KB
testcase_21 AC 535 ms
52,856 KB
testcase_22 AC 579 ms
54,224 KB
testcase_23 AC 530 ms
53,168 KB
testcase_24 AC 552 ms
53,644 KB
testcase_25 AC 564 ms
53,616 KB
testcase_26 AC 513 ms
57,080 KB
testcase_27 AC 516 ms
57,336 KB
testcase_28 AC 527 ms
57,532 KB
testcase_29 AC 523 ms
57,884 KB
testcase_30 AC 585 ms
59,200 KB
testcase_31 AC 572 ms
55,188 KB
testcase_32 AC 574 ms
53,404 KB
testcase_33 AC 570 ms
53,376 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