結果
| 問題 |
No.2338 Range AtCoder Query
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2023-06-05 13:26:44 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 1,360 ms / 4,000 ms |
| コード長 | 4,554 bytes |
| コンパイル時間 | 3,129 ms |
| コンパイル使用メモリ | 91,672 KB |
| 実行使用メモリ | 90,284 KB |
| 最終ジャッジ日時 | 2024-12-29 06:05:05 |
| 合計ジャッジ時間 | 36,317 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 34 |
ソースコード
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int n = sc.nextInt();
int m = sc.nextInt();
int q = sc.nextInt();
int[] questions = new int[n + 1];
boolean[] isAC = new boolean[n + 1];
for (int i = 1; i <= n; i++) {
questions[i] = sc.nextInt() - 1;
isAC[i] = sc.next().equals("AC");
}
Query[] queries = new Query[q];
for (int i = 0; i < q; i++) {
queries[i] = new Query(i, sc.nextInt(), sc.nextInt());
}
Arrays.sort(queries);
int[] ans1 = new int[q];
int[] ans2 = new int[q];
int[] prev = new int[m];
ArrayList<ArrayDeque<Integer>> wrongs = new ArrayList<>();
for (int i = 0; i < m; i++) {
wrongs.add(new ArrayDeque<>());
}
BinaryIndexedTree points = new BinaryIndexedTree(n + 1);
BinaryIndexedTree penalties = new BinaryIndexedTree(n + 1);
int idx = 0;
for (int i = 1; i <= n; i++) {
if (isAC[i]) {
if (prev[questions[i]] != 0) {
points.add(prev[questions[i]], -1);
penalties.add(prev[questions[i]], -wrongs.get(questions[i]).size());
}
prev[questions[i]] = i;
points.add(i, 1);
while (wrongs.get(questions[i]).size() > 0) {
penalties.add(wrongs.get(questions[i]).poll(), 1);
}
} else {
wrongs.get(questions[i]).add(i);
}
while (idx < q && queries[idx].right <= i) {
ans1[queries[idx].idx] = points.getSum(queries[idx].left, i);
ans2[queries[idx].idx] = penalties.getSum(queries[idx].left, i);
idx++;
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < q; i++) {
sb.append(ans1[i]).append(" ").append(ans2[i]).append("\n");
}
System.out.print(sb);
}
static class Query implements Comparable<Query> {
int idx;
int left;
int right;
public Query(int idx, int left, int right) {
this.idx = idx;
this.left = left;
this.right = right;
}
public int compareTo(Query another) {
return right - another.right;
}
}
}
class BinaryIndexedTree {
int size;
int[] tree;
public BinaryIndexedTree(int size) {
this.size = size;
tree = new int[size];
}
public void add(int idx, int value) {
int mask = 1;
while (idx < size) {
if ((idx & mask) != 0) {
tree[idx] += value;
idx += mask;
}
mask <<= 1;
}
}
public int getSum(int from, int to) {
return getSum(to) - getSum(from - 1);
}
public int getSum(int x) {
int mask = 1;
int ans = 0;
while (x > 0) {
if ((x & mask) != 0) {
ans += tree[x];
x -= mask;
}
mask <<= 1;
}
return ans;
}
}
class Utilities {
static String arrayToLineString(Object[] arr) {
return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
}
static String arrayToLineString(int[] arr) {
return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
}
}
class Scanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
StringBuilder sb = new StringBuilder();
public Scanner() throws Exception {
}
public int nextInt() throws Exception {
return Integer.parseInt(next());
}
public long nextLong() throws Exception {
return Long.parseLong(next());
}
public double nextDouble() throws Exception {
return Double.parseDouble(next());
}
public int[] nextIntArray() throws Exception {
return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
}
public String next() throws Exception {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten