結果

問題 No.2338 Range AtCoder Query
ユーザー tentententen
提出日時 2023-06-05 13:18:00
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 4,465 bytes
コンパイル時間 2,992 ms
コンパイル使用メモリ 91,704 KB
実行使用メモリ 107,932 KB
最終ジャッジ日時 2024-06-09 05:07:51
合計ジャッジ時間 34,385 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
50,580 KB
testcase_01 AC 58 ms
50,464 KB
testcase_02 AC 58 ms
50,440 KB
testcase_03 AC 56 ms
50,196 KB
testcase_04 AC 57 ms
50,048 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 1,109 ms
103,248 KB
testcase_32 AC 1,104 ms
103,952 KB
testcase_33 WA -
testcase_34 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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);
                }
                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();
    }
    
}
0