結果
| 問題 |
No.2942 Sigma Music Game Level Problem
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2024-10-21 15:11:03 |
| 言語 | Java (openjdk 23) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 3,726 bytes |
| コンパイル時間 | 6,360 ms |
| コンパイル使用メモリ | 78,232 KB |
| 実行使用メモリ | 60,588 KB |
| 最終ジャッジ日時 | 2024-11-07 22:09:06 |
| 合計ジャッジ時間 | 12,844 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 11 RE * 13 |
ソースコード
import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int n = sc.nextInt();
int q = sc.nextInt();
sc.nextInt();
SegmentTree st = new SegmentTree(20001);
StringBuilder sb = new StringBuilder();
while (n-- > 0) {
st.add(sc.nextInt());
}
while (q-- > 0) {
int type = sc.nextInt();
if (type == 1) {
st.add(sc.nextInt());
} else if (type == 2) {
int left = sc.nextInt();
int right = sc.nextInt();
sb.append(st.getCount(left, right + 1)).append(" ").append(st.getSum(left, right + 1)).append("\n");
} else {
sc.nextInt();
}
}
System.out.print(sb.length() == 0 ? "Not Found!\n" : sb);
}
static class SegmentTree {
int size;
int[] counts;
long[] sums;
public SegmentTree(int x) {
size = 2;
while (size < x) {
size <<= 1;
}
counts = new int[size * 2 - 1];
sums = new long[size * 2 - 1];
}
public void add(int x) {
int current = x + size - 1;
counts[current]++;
sums[current] += x;
calc((current - 1) / 2);
}
private void calc(int idx) {
counts[idx] = counts[idx * 2 + 1] + counts[idx * 2 + 2];
sums[idx] = sums[idx * 2 + 1] + sums[idx * 2 + 2];
if (idx > 0) {
calc((idx - 1) / 2);
}
}
public int getCount(int left, int right) {
return getCount(0, 0, size, left, right);
}
private int getCount(int idx, int min, int max, int left, int right) {
if (max <= left || right <= min) {
return 0;
} else if (left <= min && max <= right) {
return counts[idx];
} else {
return getCount(idx * 2 + 1, min, (min + max) / 2, left, right)
+ getCount(idx * 2 + 2, (min + max) / 2, max, left, right);
}
}
public long getSum(int left, int right) {
return getSum(0, 0, size, left, right);
}
private long getSum(int idx, int min, int max, int left, int right) {
if (max <= left || right <= min) {
return 0;
} else if (left <= min && max <= right) {
return sums[idx];
} else {
return getSum(idx * 2 + 1, min, (min + max) / 2, left, right)
+ getSum(idx * 2 + 2, (min + max) / 2, max, left, right);
}
}
}
}
class Scanner {
BufferedReader br;
StringTokenizer st = new StringTokenizer("");
public Scanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
} catch (Exception e) {
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String next() {
try {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
return st.nextToken();
}
}
}
tenten