結果
問題 | No.877 Range ReLU Query |
ユーザー |
![]() |
提出日時 | 2020-12-16 09:03:55 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 1,608 ms / 2,000 ms |
コード長 | 2,578 bytes |
コンパイル時間 | 2,685 ms |
コンパイル使用メモリ | 79,608 KB |
実行使用メモリ | 67,840 KB |
最終ジャッジ日時 | 2024-11-08 10:34:24 |
合計ジャッジ時間 | 21,540 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 20 |
ソースコード
import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int q = sc.nextInt(); PriorityQueue<Unit> units = new PriorityQueue<>(); for (int i = 1; i <= n; i++) { units.add(new Unit(i, sc.nextInt())); } PriorityQueue<Query> queries = new PriorityQueue<>(); for (int i = 0; i < q; i++) { sc.nextInt(); queries.add(new Query(i, sc.nextInt(), sc.nextInt(), sc.nextInt())); } BinaryIndexedTree bit = new BinaryIndexedTree(n + 1); BinaryIndexedTree bit2 = new BinaryIndexedTree(n + 1); long[] ans = new long[q]; while (queries.size() > 0) { Query x = queries.poll(); while (units.size() > 0 && units.peek().value >= x.value) { Unit u = units.poll(); bit.add(u.id, u.value); bit2.add(u.id, 1); } ans[x.id] = bit.getSum(x.left, x.right) - bit2.getSum(x.left, x.right) * x.value; } StringBuilder sb = new StringBuilder(); for (long x : ans) { sb.append(x).append("\n"); } System.out.print(sb); } static class Query implements Comparable<Query> { int id; int left; int right; int value; public Query(int id, int left, int right, int value) { this.id = id; this.left = left; this.right = right; this.value = value; } public int compareTo(Query another) { return another.value - value; } } static class Unit implements Comparable<Unit> { int id; int value; public Unit(int id, int value) { this.id = id; this.value = value; } public int compareTo(Unit another) { return another.value - 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; } }