結果

問題 No.789 範囲の合計
ユーザー tentententen
提出日時 2021-03-08 18:35:09
言語 Java21
(openjdk 21)
結果
AC  
実行時間 901 ms / 1,000 ms
コード長 2,001 bytes
コンパイル時間 2,640 ms
コンパイル使用メモリ 78,604 KB
実行使用メモリ 76,556 KB
最終ジャッジ日時 2024-04-18 13:35:50
合計ジャッジ時間 11,792 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
50,416 KB
testcase_01 AC 52 ms
50,332 KB
testcase_02 AC 814 ms
72,900 KB
testcase_03 AC 415 ms
50,556 KB
testcase_04 AC 901 ms
76,448 KB
testcase_05 AC 762 ms
76,556 KB
testcase_06 AC 771 ms
64,284 KB
testcase_07 AC 385 ms
60,464 KB
testcase_08 AC 565 ms
65,488 KB
testcase_09 AC 572 ms
66,232 KB
testcase_10 AC 833 ms
74,188 KB
testcase_11 AC 742 ms
63,040 KB
testcase_12 AC 763 ms
74,216 KB
testcase_13 AC 52 ms
37,296 KB
testcase_14 AC 53 ms
37,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int[][] queries = new int[n][3];
        TreeMap<Integer, Integer> compress = new TreeMap<>();
        for (int i = 0; i < n; i++) {
            String[] line = br.readLine().split(" ", 3);
            for (int j = 0; j < 3; j++) {
                queries[i][j] = Integer.parseInt(line[j]);
                if (j > 0) {
                    compress.put(queries[i][j], null);
                }
            }
        }
        int idx = 1;
        for (int x : compress.keySet()) {
            compress.put(x, idx);
            idx++;
        }
        long ans = 0;
        BinaryIndexedTree bit = new BinaryIndexedTree(idx);
        for (int i = 0; i < n; i++) {
            if (queries[i][0] == 0) {
                bit.add(compress.get(queries[i][1]), queries[i][2]);
            } else {
                ans += bit.getSum(compress.get(queries[i][1]), compress.get(queries[i][2]));
            }
        }
        System.out.println(ans);
    }
}



class BinaryIndexedTree {
    int size;
    int[] tree;
    
    public BinaryIndexedTree(int size) {
        this.size = size;
        tree = new int[size];
    }
    
    public void add(int idx, int value) {
        int mask = 1;
        while (idx < size) {
            if ((idx & mask) != 0) {
                tree[idx] += value;
                idx += mask;
            }
            mask <<= 1;
        }
    }
    
    public int getSum(int from, int to) {
        return getSum(to) - getSum(from - 1);
    }
    
    public int getSum(int x) {
        int mask = 1;
        int ans = 0;
        while (x > 0) {
            if ((x & mask) != 0) {
                ans += tree[x];
                x -= mask;
            }
            mask <<= 1;
        }
        return ans;
    }
}
0