結果

問題 No.789 範囲の合計
ユーザー hiromi_ayasehiromi_ayase
提出日時 2019-02-09 14:35:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 585 ms / 1,000 ms
コード長 5,660 bytes
コンパイル時間 2,814 ms
コンパイル使用メモリ 82,708 KB
実行使用メモリ 95,712 KB
最終ジャッジ日時 2024-07-01 19:12:17
合計ジャッジ時間 10,285 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
38,304 KB
testcase_01 AC 78 ms
38,492 KB
testcase_02 AC 582 ms
95,708 KB
testcase_03 AC 505 ms
95,616 KB
testcase_04 AC 585 ms
95,692 KB
testcase_05 AC 569 ms
95,384 KB
testcase_06 AC 556 ms
95,540 KB
testcase_07 AC 470 ms
95,328 KB
testcase_08 AC 566 ms
95,508 KB
testcase_09 AC 543 ms
95,700 KB
testcase_10 AC 566 ms
95,712 KB
testcase_11 AC 508 ms
95,300 KB
testcase_12 AC 521 ms
95,452 KB
testcase_13 AC 76 ms
38,016 KB
testcase_14 AC 76 ms
38,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;

class DynamicSegmentTreeRSQSimple {
  public long[] vals;
  public int[] ls;
  public int[] rs;
  public int gen;
  public int height;
  
  public DynamicSegmentTreeRSQSimple(int q, int height)
  {
      int bufsize = q * (height+1) + 1;
      this.vals = new long[bufsize];
      this.ls = new int[bufsize];
      this.rs = new int[bufsize];
      Arrays.fill(this.ls, -1);
      Arrays.fill(this.rs, -1);
      this.height = height;
      gen = 0;
      vals[gen++] = 0;
  }
  
  public void add(long x, long v)
  {
      addDfs(0, x, v, height-1);
  }
  
  private int addDfs(int id, long x, long v, int d)
  {
      if(id == -1)id = gen++;
      if(d == -1){
          vals[id] += v;
      }else{
          if(x<<~d<0){
              rs[id] = addDfs(rs[id], x, v, d-1);
          }else{
              ls[id] = addDfs(ls[id], x, v, d-1);
          }
          propagate(id);
      }
      return id;
  }
  
  private long val(int id){ return id == -1 ? 0 : vals[id]; }
  private int L(int id){ return id == -1 ? -1 : ls[id]; }
  private int R(int id){ return id == -1 ? -1 : rs[id]; }
  
  private void propagate(int id)
  {
      if(id == -1)return;
      vals[id] = val(ls[id]) + val(rs[id]);
  }
  
  // sum of a[pos] which (t xor pos) >= v
  public long sumge(long t, long v)
  {
      if(v >= 1L<<height)return 0L;
      return sumge(0, t, v, height-1, 0);
  }
  
  private long sumge(long cur, long t, long v, int d, int id)
  {
      if(id == -1)return 0L;
      if(d == -1){
          return vals[id];
      }
      
      long ret = 0;
      if(v<<~d<0){
          if(t<<~d<0){
              ret += sumge(cur|0L<<d, t, v, d-1, L(id));
          }else{
              ret += sumge(cur|1L<<d, t, v, d-1, R(id));
          }
      }else{
          if(t<<~d<0){
              ret += sumge(cur|1L<<d, t, v, d-1, R(id));
              ret += val(L(id));
          }else{
              ret += sumge(cur|0L<<d, t, v, d-1, L(id));
              ret += val(R(id));
          }
      }
      return ret;
  }
  
  public long sum(long l, long r)
  {
      return sum(l, r, height-1, 0);
  }
  
  private long sum(long l, long r, int d, int id)
  {
      if(l <= 0 && r >= 1L<<d+1){
          return vals[id];
      }else{
          // [l,r), [0,1<<d-1)
          long ret = 0;
          if(l < 1L<<d && 0 < r && L(id) != -1){
              ret += sum(l, r, d-1, L(id));
          }
          if(l < 1L<<d+1 && 1L<<d < r && R(id) != -1){
              ret += sum(l-(1L<<d), r-(1L<<d), d-1, R(id));
          }
          return ret;
      }
  }
}


public class Main {


  private static void solve() {
    int n = ni();
    int[][] q = ntable(n, 3);
    


    DynamicSegmentTreeRSQSimple st = new DynamicSegmentTreeRSQSimple(n, 30);
    long ret = 0;
    for (int[] v : q) {
      if (v[0] == 0) {
        st.add(v[1], v[2]);
      } else {
        ret += st.sum(v[1], v[2] + 1);
      }
    }
    System.out.println(ret);
    
  }

  public static void main(String[] args) {
    new Thread(null, new Runnable() {
      @Override
      public void run() {
        long start = System.currentTimeMillis();
        String debug = args.length > 0 ? args[0] : null;
        if (debug != null) {
          try {
            is = java.nio.file.Files.newInputStream(java.nio.file.Paths.get(debug));
          } catch (Exception e) {
            throw new RuntimeException(e);
          }
        }
        reader = new java.io.BufferedReader(new java.io.InputStreamReader(is), 32768);
        solve();
        out.flush();
        tr((System.currentTimeMillis() - start) + "ms");
      }
    }, "", 64000000).start();
  }

  public static int[] shrink(int[] a)
  {
      int n = a.length;
      long[] b = new long[n];
      for(int i = 0;i < n;i++)b[i] = (long)a[i]<<32|i;
      Arrays.sort(b);
      int[] ret = new int[n];
      int p = 0;
      for(int i = 0;i < n;i++) {
          if(i>0 && (b[i]^b[i-1])>>32!=0)p++;
          ret[(int)b[i]] = p;
      }
      return ret;
  }
  
  private static java.io.InputStream is = System.in;
  private static java.io.PrintWriter out = new java.io.PrintWriter(System.out);
  private static java.util.StringTokenizer tokenizer = null;
  private static java.io.BufferedReader reader;

  public static String next() {
    while (tokenizer == null || !tokenizer.hasMoreTokens()) {
      try {
        tokenizer = new java.util.StringTokenizer(reader.readLine());
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
    return tokenizer.nextToken();
  }

  private static double nd() {
    return Double.parseDouble(next());
  }

  private static long nl() {
    return Long.parseLong(next());
  }

  private static int[] na(int n) {
    int[] a = new int[n];
    for (int i = 0; i < n; i++)
      a[i] = ni();
    return a;
  }

  private static char[] ns() {
    return next().toCharArray();
  }

  private static long[] nal(int n) {
    long[] a = new long[n];
    for (int i = 0; i < n; i++)
      a[i] = nl();
    return a;
  }

  private static int[][] ntable(int n, int m) {
    int[][] table = new int[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        table[i][j] = ni();
      }
    }
    return table;
  }

  private static int[][] nlist(int n, int m) {
    int[][] table = new int[m][n];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        table[j][i] = ni();
      }
    }
    return table;
  }

  private static int ni() {
    return Integer.parseInt(next());
  }

  private static void tr(Object... o) {
    if (is != System.in)
      System.out.println(java.util.Arrays.deepToString(o));
  }
}
0