結果
問題 |
No.789 範囲の合計
|
ユーザー |
![]() |
提出日時 | 2021-03-08 18:22:39 |
言語 | Java (openjdk 23) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,840 bytes |
コンパイル時間 | 2,593 ms |
コンパイル使用メモリ | 79,108 KB |
実行使用メモリ | 75,616 KB |
最終ジャッジ日時 | 2024-10-10 05:34:27 |
合計ジャッジ時間 | 20,771 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 5 TLE * 10 |
ソースコード
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[][] queries = new int[n][3]; TreeMap<Integer, Integer> compress = new TreeMap<>(); for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { queries[i][j] = sc.nextInt(); 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; } }