結果

問題 No.2338 Range AtCoder Query
ユーザー tentententen
提出日時 2023-06-05 13:26:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,290 ms / 4,000 ms
コード長 4,554 bytes
コンパイル時間 2,775 ms
コンパイル使用メモリ 91,916 KB
実行使用メモリ 90,516 KB
最終ジャッジ日時 2024-06-09 05:08:42
合計ジャッジ時間 35,543 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
37,196 KB
testcase_01 AC 56 ms
37,500 KB
testcase_02 AC 56 ms
37,068 KB
testcase_03 AC 56 ms
37,388 KB
testcase_04 AC 57 ms
37,472 KB
testcase_05 AC 58 ms
37,540 KB
testcase_06 AC 110 ms
39,404 KB
testcase_07 AC 110 ms
39,416 KB
testcase_08 AC 118 ms
39,744 KB
testcase_09 AC 120 ms
39,560 KB
testcase_10 AC 117 ms
39,508 KB
testcase_11 AC 945 ms
74,376 KB
testcase_12 AC 1,077 ms
76,444 KB
testcase_13 AC 999 ms
72,664 KB
testcase_14 AC 983 ms
71,624 KB
testcase_15 AC 1,115 ms
72,984 KB
testcase_16 AC 1,251 ms
89,864 KB
testcase_17 AC 1,290 ms
89,708 KB
testcase_18 AC 1,214 ms
90,196 KB
testcase_19 AC 1,257 ms
90,208 KB
testcase_20 AC 1,162 ms
89,680 KB
testcase_21 AC 1,217 ms
89,812 KB
testcase_22 AC 1,277 ms
89,928 KB
testcase_23 AC 1,283 ms
89,624 KB
testcase_24 AC 1,239 ms
89,652 KB
testcase_25 AC 1,268 ms
89,824 KB
testcase_26 AC 630 ms
74,880 KB
testcase_27 AC 622 ms
75,052 KB
testcase_28 AC 595 ms
74,700 KB
testcase_29 AC 1,284 ms
89,980 KB
testcase_30 AC 1,290 ms
90,380 KB
testcase_31 AC 1,191 ms
89,904 KB
testcase_32 AC 1,086 ms
89,928 KB
testcase_33 AC 1,112 ms
90,516 KB
testcase_34 AC 1,090 ms
90,300 KB
権限があれば一括ダウンロードができます

ソースコード

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