結果
| 問題 | No.877 Range ReLU Query |
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2020-12-16 09:18:57 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 1,531 ms / 2,000 ms |
| コード長 | 2,957 bytes |
| 記録 | |
| コンパイル時間 | 2,751 ms |
| コンパイル使用メモリ | 79,560 KB |
| 実行使用メモリ | 68,632 KB |
| 最終ジャッジ日時 | 2024-11-08 10:34:49 |
| 合計ジャッジ時間 | 20,355 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);
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);
}
ans[x.id] = bit.getSum(x.left, x.right) - bit.getCount(x.left, x.right) * (long)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;
int[] counts;
public BinaryIndexedTree(int size) {
this.size = size;
tree = new long[size];
counts = new int[size];
}
public void add(int idx, long value) {
int mask = 1;
while (idx < size) {
if ((idx & mask) != 0) {
tree[idx] += value;
counts[idx]++;
idx += mask;
}
mask <<= 1;
}
}
public int getCount(int from, int to) {
return getCount(to) - getCount(from - 1);
}
public int getCount(int x) {
int mask = 1;
int ans = 0;
while (x > 0) {
if ((x & mask) != 0) {
ans += counts[x];
x -= mask;
}
mask <<= 1;
}
return ans;
}
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;
}
}
tenten