結果

問題 No.2338 Range AtCoder Query
ユーザー 37zigen37zigen
提出日時 2023-06-02 22:59:38
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,683 ms / 4,000 ms
コード長 3,587 bytes
コンパイル時間 2,907 ms
コンパイル使用メモリ 86,988 KB
実行使用メモリ 148,076 KB
最終ジャッジ日時 2024-06-11 01:10:50
合計ジャッジ時間 64,600 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
42,368 KB
testcase_01 AC 139 ms
42,712 KB
testcase_02 AC 143 ms
42,712 KB
testcase_03 AC 147 ms
42,548 KB
testcase_04 AC 130 ms
42,120 KB
testcase_05 AC 144 ms
42,348 KB
testcase_06 AC 224 ms
47,520 KB
testcase_07 AC 249 ms
47,540 KB
testcase_08 AC 243 ms
47,488 KB
testcase_09 AC 240 ms
48,192 KB
testcase_10 AC 247 ms
48,028 KB
testcase_11 AC 2,122 ms
109,028 KB
testcase_12 AC 2,121 ms
107,172 KB
testcase_13 AC 2,110 ms
110,464 KB
testcase_14 AC 1,996 ms
110,668 KB
testcase_15 AC 2,321 ms
115,032 KB
testcase_16 AC 2,526 ms
131,744 KB
testcase_17 AC 2,498 ms
129,656 KB
testcase_18 AC 2,683 ms
131,368 KB
testcase_19 AC 2,461 ms
129,676 KB
testcase_20 AC 2,544 ms
129,892 KB
testcase_21 AC 2,251 ms
128,072 KB
testcase_22 AC 2,547 ms
148,076 KB
testcase_23 AC 2,615 ms
131,580 KB
testcase_24 AC 2,169 ms
127,876 KB
testcase_25 AC 2,411 ms
129,964 KB
testcase_26 AC 1,403 ms
122,040 KB
testcase_27 AC 1,392 ms
125,540 KB
testcase_28 AC 1,431 ms
125,972 KB
testcase_29 AC 2,357 ms
126,660 KB
testcase_30 AC 2,565 ms
130,832 KB
testcase_31 AC 2,182 ms
129,160 KB
testcase_32 AC 2,069 ms
123,452 KB
testcase_33 AC 2,256 ms
131,400 KB
testcase_34 AC 2,308 ms
128,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		new Main().run();
	}
	
	final long p=998244353;
	final long g=3;
	int MAX = 5000;
	long[] fac = new long[MAX];
	long[] ifac = new long[MAX];
	long[] inv = new long[MAX];
	{
		Arrays.fill(fac, 1);
		Arrays.fill(ifac, 1);
		Arrays.fill(inv, 1);
		for (int i = 2; i < MAX; ++i) {
			fac[i] = i * fac[i - 1] % p;
			inv[i] = p - inv[(int) (p % i)] * (p / i) % p;
			ifac[i] = inv[i] * ifac[i - 1] % p;
		}
	}
	
	class Tree {
		int[] v;
		int n;
		
		public Tree(int n_) {
			n = Integer.highestOneBit(n_) * 2;
			v = new int[2 * n];
		}
		
		void add(int k, int val) {
			int pos = id(k, k + 1);
			v[pos] += val;
			while (pos != id(0, n)) {
				pos /= 2;
				v[pos] = v[2 * pos] + v[2 * pos + 1];
			}
		}
		
		void set(int k, int val) {
			int pos = id(k, k + 1);
			v[pos] = val;
			while (pos != id(0, n)) {
				pos /= 2;
				v[pos] = v[2 * pos] + v[2 * pos + 1];
			}
		}
		
		int query(int a, int b) {
			if (b - a <= 0) return 0;
			int ma = a + Integer.lowestOneBit(a);
			int mb = b - Integer.lowestOneBit(b);
			if (a < ma && ma <= b) {
				return v[id(a, ma)] + query(ma, b);
			} else {
				return query(a, mb) + v[id(mb, b)];
			}
		}
		
		int id(int a, int b) {
			int w = b - a;
			return a / w + n / w;
		}
	}

	
	
    void run() {
    	Scanner sc = new Scanner(System.in);
    	int N = sc.nextInt();
    	int M = sc.nextInt();
    	int Q = sc.nextInt();
    	int[] P = new int[N];
    	boolean[] ac = new boolean[N];
    	for (int i = 0; i < N; ++i) {
    		P[i] = sc.nextInt() - 1;
    		ac[i] = sc.next().equals("AC");
    	}
    	int[][] query = new int[Q][3];
    	int[] ans1 = new int[Q];
    	int[] ans2 = new int[Q];
    	for (int i = 0; i < Q; ++i) {
    		query[i][0] = sc.nextInt() - 1;
    		query[i][1] = sc.nextInt() - 1;
    		query[i][2] = i;
    	}
    	Arrays.sort(query, (x, y) -> -Integer.compare(x[0], y[0]));
    	Tree watree = new Tree(N);
    	Tree actree = new Tree(N);
    	Tree petree = new Tree(N);
    	ArrayDeque<Integer>[] walist = new ArrayDeque[M];
    	ArrayDeque<Integer>[] aclist = new ArrayDeque[M];
    	for (int i = 0; i < M; ++i) {
    		aclist[i] = new ArrayDeque<>();
    		walist[i] = new ArrayDeque<>();
    	}
    	int p = 0;
    	for (int src = N - 1; src >= 0; --src) {
    		if (!ac[src]) {
    			walist[P[src]].addFirst(src);
    			watree.set(src, 1);
    			petree.set(src, -1);
    			if (!aclist[P[src]].isEmpty()) {
    				petree.add(aclist[P[src]].peekFirst(), 1);
    			}
    		} else {
    			while (!walist[P[src]].isEmpty()) {
    				int pos = walist[P[src]].pollLast();
    				watree.set(pos, 0);
    				petree.set(pos, 0);
    				if (!aclist[P[src]].isEmpty()) {
    					petree.add(aclist[P[src]].peekFirst(), -1);
    				}
    			
    			}
    			while (!aclist[P[src]].isEmpty()) {
    				int pos = aclist[P[src]].pollLast();
    				actree.set(pos, 0);
    			}
    			actree.set(src, 1);
    			aclist[P[src]].addFirst(src);
    		}
    		while (p < Q && query[p][0] == src) {
    			var qu = query[p];
	    		ans1[qu[2]] = actree.query(qu[0], qu[1] + 1);
	    		ans2[qu[2]] = watree.query(qu[0], qu[1] + 1) + petree.query(qu[0], qu[1] + 1);
	    		++p;
    		}
    	}
    	PrintWriter pw = new PrintWriter(System.out);
    	for (int i = 0; i < Q; ++i) {
    		pw.println(ans1[i] + " " + ans2[i]);
    	}
    	pw.close();
    }
	
    static void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));}
}


	
0