結果

問題 No.789 範囲の合計
ユーザー takeya_okinotakeya_okino
提出日時 2019-07-07 14:04:32
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,845 bytes
コンパイル時間 2,077 ms
コンパイル使用メモリ 79,944 KB
実行使用メモリ 95,680 KB
最終ジャッジ日時 2024-04-15 04:08:15
合計ジャッジ時間 6,216 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
56,496 KB
testcase_01 AC 104 ms
52,752 KB
testcase_02 TLE -
testcase_03 AC 926 ms
75,784 KB
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 AC 761 ms
75,832 KB
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 AC 124 ms
54,108 KB
testcase_14 AC 110 ms
53,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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;
    }
  }
}
0