結果
問題 |
No.789 範囲の合計
|
ユーザー |
![]() |
提出日時 | 2019-07-07 17:05:13 |
言語 | Java (openjdk 23) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,845 bytes |
コンパイル時間 | 2,954 ms |
コンパイル使用メモリ | 79,544 KB |
実行使用メモリ | 103,868 KB |
最終ジャッジ日時 | 2024-10-04 23:09:16 |
合計ジャッジ時間 | 6,808 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 5 TLE * 10 |
ソースコード
import java.util.*; public class Main { static int N; static long[] dat; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[][] q = new long[n][3]; HashSet<Integer> set = new HashSet<Integer>(); ArrayList<Integer> list = new ArrayList<Integer>(); for(int i = 0; i < n; i++) { q[i][0] = sc.nextLong(); q[i][1] = sc.nextLong(); q[i][2] = sc.nextLong(); if(q[i][0] == 0) { set.add((int)q[i][1]); } else { set.add((int)q[i][1]); set.add((int)q[i][2]); } } for(Iterator<Integer> it = set.iterator(); it.hasNext();) { int t = it.next(); list.add(t); } Collections.sort(list); HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for(int i = 0; i < list.size(); i++) { map.put(list.get(i), i); } int len = list.size(); init(len); long ans = 0; for(int i = 0; i < n; i++) { if(q[i][0] == 0) { update(map.get((int)q[i][1]), q[i][2]); } else { ans += query(map.get((int)q[i][1]), map.get((int)q[i][2]) + 1, 0, 0, N); } } System.out.println(ans); } public static void init(int n_) { int n = 1; while(n < n_) { n *= 2; } N = n; dat = new long[2 * N - 1]; } public static void update(int k, long x) { int a = k + N - 1; dat[a] += x; while(a > 0) { a = (a - 1) / 2; dat[a] = dat[2 * a + 1] + dat[2 * a + 2]; } } public static long query(int a, int b, int k, int l, int r) { if((r <= a) || (l >= b)) return 0; if((a <= l) && (r <= b)) { return dat[k]; } else { long vl = query(a, b, 2 * k + 1, l, (l + r) / 2); long vr = query(a, b, 2 * k + 2, (l + r) / 2, r); return vl + vr; } } }