結果

問題 No.789 範囲の合計
ユーザー hiromi_ayasehiromi_ayase
提出日時 2019-02-09 14:31:21
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 5,665 bytes
コンパイル時間 2,841 ms
コンパイル使用メモリ 90,420 KB
実行使用メモリ 541,256 KB
最終ジャッジ日時 2024-07-01 19:11:16
合計ジャッジ時間 12,952 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 74 ms
38,348 KB
testcase_02 MLE -
testcase_03 MLE -
testcase_04 MLE -
testcase_05 MLE -
testcase_06 MLE -
testcase_07 MLE -
testcase_08 MLE -
testcase_09 MLE -
testcase_10 MLE -
testcase_11 MLE -
testcase_12 MLE -
testcase_13 AC 77 ms
38,136 KB
testcase_14 AC 75 ms
38,152 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 * 20, 20);
    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