結果
問題 | No.877 Range ReLU Query |
ユーザー | htensai |
提出日時 | 2020-06-11 10:55:16 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 988 ms / 2,000 ms |
コード長 | 2,849 bytes |
コンパイル時間 | 2,936 ms |
コンパイル使用メモリ | 85,716 KB |
実行使用メモリ | 68,108 KB |
最終ジャッジ日時 | 2024-11-08 10:28:03 |
合計ジャッジ時間 | 14,729 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 56 ms
36,860 KB |
testcase_01 | AC | 83 ms
38,596 KB |
testcase_02 | AC | 92 ms
38,332 KB |
testcase_03 | AC | 99 ms
38,948 KB |
testcase_04 | AC | 60 ms
37,212 KB |
testcase_05 | AC | 69 ms
37,664 KB |
testcase_06 | AC | 70 ms
37,812 KB |
testcase_07 | AC | 63 ms
37,364 KB |
testcase_08 | AC | 97 ms
38,724 KB |
testcase_09 | AC | 59 ms
37,272 KB |
testcase_10 | AC | 82 ms
38,552 KB |
testcase_11 | AC | 946 ms
59,724 KB |
testcase_12 | AC | 885 ms
55,900 KB |
testcase_13 | AC | 805 ms
56,452 KB |
testcase_14 | AC | 836 ms
56,556 KB |
testcase_15 | AC | 967 ms
64,532 KB |
testcase_16 | AC | 926 ms
64,112 KB |
testcase_17 | AC | 988 ms
64,904 KB |
testcase_18 | AC | 967 ms
67,104 KB |
testcase_19 | AC | 945 ms
68,108 KB |
testcase_20 | AC | 957 ms
68,012 KB |
ソースコード
import java.util.*; import java.io.*; public class Main { public static void main (String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] first = br.readLine().split(" ", 2); int n = Integer.parseInt(first[0]); int q = Integer.parseInt(first[1]); BinaryIndexedTree bit = new BinaryIndexedTree(n + 1); BinaryIndexedTree counts = new BinaryIndexedTree(n + 1); ArrayList<Order> list = new ArrayList<>(); String[] second = br.readLine().split(" ", n); for (int i = 1; i <= n; i++) { list.add(new Order(i, Integer.parseInt(second[i - 1]))); } for (int i = 0; i < q; i++) { String[] line = br.readLine().split(" ", 4); list.add(new Order(i, Integer.parseInt(line[1]), Integer.parseInt(line[2]), Integer.parseInt(line[3]))); } Collections.sort(list); long[] ans = new long[q]; for (Order x : list) { if (x.isValue()) { bit.add(x.idx, x.value); counts.add(x.idx, 1); } else { long tmp = bit.getSum(x.left, x.right) - counts.getSum(x.left, x.right) * x.value; ans[x.idx] = tmp; } } StringBuilder sb = new StringBuilder(); for (long x : ans) { sb.append(x).append("\n"); } System.out.print(sb); } static class Order implements Comparable<Order> { int idx; int left; int right; int value; public Order(int idx, int left, int right, int value) { this.idx = idx; this.left = left; this.right = right; this.value = value; } public Order(int idx, int value) { this(idx, 0, 0, value); } public boolean isValue() { return right == 0; } public int compareTo(Order another) { if (value == another.value) { return right - another.right; } else { return another.value - value; } } public String toString() { return idx + ":" + left + ":" + right + ":" + value; } } } class BinaryIndexedTree { int size; long[] tree; public BinaryIndexedTree(int size) { this.size = size; tree = new long[size]; } public void add(int idx, long value) { int mask = 1; while (idx < size) { if ((idx & mask) != 0) { tree[idx] += value; idx += mask; } mask <<= 1; } } public long getSum(int from, int to) { return getSum(to) - getSum(from - 1); } public long getSum(int x) { int mask = 1; long ans = 0; while (x > 0) { if ((x & mask) != 0) { ans += tree[x]; x -= mask; } mask <<= 1; } return ans; } }