結果

問題 No.877 Range ReLU Query
ユーザー hiromi_ayasehiromi_ayase
提出日時 2019-09-06 22:14:54
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,923 ms / 2,000 ms
コード長 5,548 bytes
コンパイル時間 2,833 ms
コンパイル使用メモリ 82,972 KB
実行使用メモリ 110,828 KB
最終ジャッジ日時 2024-11-08 10:05:39
合計ジャッジ時間 23,653 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
38,120 KB
testcase_01 AC 136 ms
40,516 KB
testcase_02 AC 133 ms
40,796 KB
testcase_03 AC 156 ms
40,772 KB
testcase_04 AC 88 ms
38,712 KB
testcase_05 AC 122 ms
40,440 KB
testcase_06 AC 119 ms
40,412 KB
testcase_07 AC 101 ms
39,324 KB
testcase_08 AC 155 ms
41,052 KB
testcase_09 AC 95 ms
38,860 KB
testcase_10 AC 141 ms
40,604 KB
testcase_11 AC 1,794 ms
100,172 KB
testcase_12 AC 1,829 ms
99,932 KB
testcase_13 AC 1,490 ms
86,536 KB
testcase_14 AC 1,603 ms
85,172 KB
testcase_15 AC 1,923 ms
109,864 KB
testcase_16 AC 1,900 ms
108,620 KB
testcase_17 AC 1,867 ms
109,732 KB
testcase_18 AC 1,786 ms
109,896 KB
testcase_19 AC 1,427 ms
110,828 KB
testcase_20 AC 1,683 ms
110,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;

class SegmentTreeSeqSumGE {
  public int M, H, N;
  public long[][] cums;
  public int[][] seqs;

  public SegmentTreeSeqSumGE(int[] a) {
    N = a.length;
    M = Integer.highestOneBit(Math.max(N - 1, 1)) << 2;
    H = M >>> 1;

    cums = new long[M][0];
    seqs = new int[M][0];
    for (int i = 0; i < H; i++) {
      if (i < N) {
        seqs[H + i] = new int[] {a[i]};
        cums[H + i] = new long[seqs[H + i].length + 1];
        for (int j = 0; j < seqs[H + i].length; j++) {
          cums[H + i][j + 1] = cums[H + i][j] + seqs[H + i][j];
        }
      }
    }
    for (int i = H - 1; i >= 1; i--) {
      seqs[i] = mergeSeq(seqs[2 * i], seqs[2 * i + 1]);
      cums[i] = new long[seqs[i].length + 1];
      for (int j = 0; j < seqs[i].length; j++) {
        cums[i][j + 1] = cums[i][j] + seqs[i][j];
      }
    }
  }

  private int[] mergeSeq(int[] L, int[] R) {
    if (L == null)
      return null;
    if (R == null)
      return Arrays.copyOf(L, L.length);
    int[] C = new int[L.length + R.length];
    int p = 0, q = 0, r = 0;
    while (p < L.length && q < R.length)
      C[r++] = L[p] < R[q] ? L[p++] : R[q++];
    while (p < L.length)
      C[r++] = L[p++];
    while (q < R.length)
      C[r++] = R[q++];
    return C;
  }

  public long sumgex(int l, int r, int v) {
    if (l >= r)
      return 0;
    long ret = 0;
    while (l != 0) {
      int f = l & -l;
      if (l + f > r)
        break;

      int cur = (H + l) / f;
      int ind = lowerBound(seqs[cur], v);
      long sum = cums[cur][cums[cur].length - 1] - cums[cur][ind] + (long) ind * v;
      ret += sum;

      l += f;
    }

    while (l < r) {
      int f = r & -r;

      int cur = (H + r) / f - 1;
      int ind = lowerBound(seqs[cur], v);
      long sum = cums[cur][cums[cur].length - 1] - cums[cur][ind] + (long) ind * v;
      ret += sum;

      r -= f;
    }
    return ret;
  }

  public long sumge(int l, int r, int v) {
    return sumge(l, r, v, 0, H, 1);
  }

  private long sumge(int l, int r, int v, int cl, int cr, int cur) {
    if (seqs[cur] == null)
      return 0;
    if (l <= cl && cr <= r) {
      int ind = lowerBound(seqs[cur], v);
      long sum = cums[cur][cums[cur].length - 1] - cums[cur][ind] + (long) ind * v;
      return sum;
    } else {
      int mid = cl + cr >>> 1;
      long sum = 0;
      if (cl < r && l < mid) {
        sum += sumge(l, r, v, cl, mid, 2 * cur);
      }
      if (mid < r && l < cr) {
        sum += sumge(l, r, v, mid, cr, 2 * cur + 1);
      }
      return sum;
    }
  }

  public static int lowerBound(int[] a, int v) {
    int low = -1, high = a.length;
    while (high - low > 1) {
      int h = high + low >>> 1;
      if (a[h] >= v) {
        high = h;
      } else {
        low = h;
      }
    }
    return high;
  }
}


public class Main {

  private static void solve() {
    int n = ni();
    int q = ni();
    int[] a = na(n);
    SegmentTreeSeqSumGE st = new SegmentTreeSeqSumGE(a);

    for (int i = 0; i < q; i++) {
      int t = ni();
      int l = ni() - 1;
      int r = ni() - 1;
      int x = ni();

      long ret = st.sumge(l, r + 1, x);
      System.out.println(ret - (long) (r - l + 1) * x);
    }
  }

  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();
  }

  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