結果

問題 No.2338 Range AtCoder Query
ユーザー tentententen
提出日時 2023-06-05 13:26:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,219 ms / 4,000 ms
コード長 4,554 bytes
コンパイル時間 2,591 ms
コンパイル使用メモリ 82,496 KB
実行使用メモリ 106,684 KB
最終ジャッジ日時 2023-08-28 09:32:37
合計ジャッジ時間 32,997 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,640 KB
testcase_01 AC 46 ms
49,896 KB
testcase_02 AC 46 ms
49,780 KB
testcase_03 AC 45 ms
49,636 KB
testcase_04 AC 44 ms
49,648 KB
testcase_05 AC 44 ms
49,672 KB
testcase_06 AC 99 ms
51,912 KB
testcase_07 AC 101 ms
52,480 KB
testcase_08 AC 103 ms
51,880 KB
testcase_09 AC 104 ms
52,612 KB
testcase_10 AC 94 ms
50,008 KB
testcase_11 AC 958 ms
85,028 KB
testcase_12 AC 985 ms
87,128 KB
testcase_13 AC 993 ms
85,928 KB
testcase_14 AC 892 ms
81,468 KB
testcase_15 AC 1,068 ms
86,196 KB
testcase_16 AC 1,219 ms
105,564 KB
testcase_17 AC 1,197 ms
105,096 KB
testcase_18 AC 1,161 ms
104,768 KB
testcase_19 AC 1,158 ms
105,236 KB
testcase_20 AC 1,197 ms
105,504 KB
testcase_21 AC 1,190 ms
103,124 KB
testcase_22 AC 1,215 ms
104,968 KB
testcase_23 AC 1,176 ms
105,116 KB
testcase_24 AC 1,214 ms
104,996 KB
testcase_25 AC 1,187 ms
104,996 KB
testcase_26 AC 556 ms
85,440 KB
testcase_27 AC 561 ms
85,540 KB
testcase_28 AC 549 ms
87,360 KB
testcase_29 AC 1,142 ms
105,104 KB
testcase_30 AC 1,205 ms
104,784 KB
testcase_31 AC 1,041 ms
104,580 KB
testcase_32 AC 1,061 ms
105,100 KB
testcase_33 AC 1,081 ms
104,700 KB
testcase_34 AC 1,081 ms
106,684 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