結果

問題 No.2338 Range AtCoder Query
ユーザー 👑 ygussanyygussany
提出日時 2023-06-02 23:40:45
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 972 ms / 4,000 ms
コード長 3,490 bytes
コンパイル時間 1,115 ms
コンパイル使用メモリ 31,988 KB
実行使用メモリ 10,972 KB
最終ジャッジ日時 2023-08-28 06:23:24
合計ジャッジ時間 21,902 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,944 KB
testcase_01 AC 1 ms
5,020 KB
testcase_02 AC 1 ms
5,012 KB
testcase_03 AC 1 ms
5,136 KB
testcase_04 AC 1 ms
4,948 KB
testcase_05 AC 1 ms
5,012 KB
testcase_06 AC 2 ms
4,948 KB
testcase_07 AC 2 ms
4,944 KB
testcase_08 AC 2 ms
4,948 KB
testcase_09 AC 2 ms
5,012 KB
testcase_10 AC 2 ms
4,944 KB
testcase_11 AC 743 ms
7,144 KB
testcase_12 AC 684 ms
7,092 KB
testcase_13 AC 777 ms
7,256 KB
testcase_14 AC 813 ms
6,484 KB
testcase_15 AC 885 ms
7,856 KB
testcase_16 AC 963 ms
9,440 KB
testcase_17 AC 958 ms
9,364 KB
testcase_18 AC 959 ms
9,420 KB
testcase_19 AC 965 ms
9,464 KB
testcase_20 AC 963 ms
9,484 KB
testcase_21 AC 970 ms
9,516 KB
testcase_22 AC 964 ms
9,532 KB
testcase_23 AC 967 ms
9,444 KB
testcase_24 AC 970 ms
9,480 KB
testcase_25 AC 972 ms
9,364 KB
testcase_26 AC 32 ms
8,324 KB
testcase_27 AC 31 ms
7,804 KB
testcase_28 AC 33 ms
7,876 KB
testcase_29 AC 545 ms
9,540 KB
testcase_30 AC 849 ms
9,432 KB
testcase_31 AC 475 ms
9,464 KB
testcase_32 AC 437 ms
9,536 KB
testcase_33 AC 328 ms
10,904 KB
testcase_34 AC 318 ms
10,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

typedef struct {
	int key, id;
} data;

typedef struct {
	data obj[200001];
	int size;
} min_heap;

void push(min_heap* h, data x)
{
	int i = ++(h->size), j = i >> 1;
	data tmp;
	h->obj[i] = x;
	while (j > 0) {
		if (h->obj[i].key < h->obj[j].key) {
			tmp = h->obj[j];
			h->obj[j] = h->obj[i];
			h->obj[i] = tmp;
			i = j;
			j >>= 1;
		} else break;
	}
}

data pop(min_heap* h)
{
	int i = 1, j = 2;
	data output = h->obj[1], tmp;
	h->obj[1] = h->obj[(h->size)--];
	while (j <= h->size) {
		if (j < h->size && h->obj[j^1].key < h->obj[j].key) j ^= 1;
		if (h->obj[j].key < h->obj[i].key) {
			tmp = h->obj[j];
			h->obj[j] = h->obj[i];
			h->obj[i] = tmp;
			i = j;
			j <<= 1;
		} else break;
	}
	return output;
}

const int THR = 300;

int main()
{
	int i, N, M, Q, P[200001], L[200001], R[200001];
	char S[200001][3];
	data d;
	min_heap h[2];
	h[0].size = 0;
	h[1].size = 0;
	scanf("%d %d %d", &N, &M, &Q);
	for (i = 1; i <= N; i++) scanf("%d %s", &(P[i]), S[i]);
	for (i = 1; i <= Q; i++) {
		scanf("%d %d", &(L[i]), &(R[i]));
		d.key = L[i];
		d.id = i;
		push(&(h[0]), d);
	}
	
	int j, k, l, r, block_l[1001], block_r[1001], ans[200001][2], tmp_ans[2], tmp[200001][2], q[1001][3], head, tmpp_ans[2];
	for (k = 0; 1; k++) {
		block_l[k] = k * THR + 1;
		block_r[k] = block_l[k] + THR - 1;
		if (block_r[k] > N) block_r[k] = N;
		if (block_r[k] == N) break;
	}
	for (i = 0; i <= k; i++) {
		while (h[0].size > 0 && h[0].obj[1].key <= block_r[i]) {
			d = pop(&(h[0]));
			d.key = R[d.id];
			push(&(h[1]), d);
		}
		if (h[1].size == 0) continue;
		for (j = 1; j <= M; j++) {
			tmp[j][0] = 0;
			tmp[j][1] = 0;
		}
		tmp_ans[0] = 0;
		tmp_ans[1] = 0;
		while (h[1].size > 0 && h[1].obj[1].key < block_r[i]) {
			d = pop(&(h[1]));
			tmpp_ans[0] = tmp_ans[0];
			tmpp_ans[1] = tmp_ans[1];
			for (l = d.key + 1, r = d.key, head = 0; l > L[d.id]; ) {
				l--;
				q[head][0] = tmp[P[l]][0];
				q[head][1] = tmp[P[l]][1];
				q[head++][2] = P[l];
				if (S[l][0] == 'A') {
					if (tmp[P[l]][0] > 0) tmpp_ans[1] -= tmp[P[l]][1];
					tmp[P[l]][0]++;
					tmp[P[l]][1] = 0;
					if (tmp[P[l]][0] == 1) tmpp_ans[0]++;
				} else {
					if (tmp[P[l]][0] > 0) tmpp_ans[1]++;
					tmp[P[l]][1]++;
				}
			}
			for (head--; head >= 0; head--) {
				tmp[q[head][2]][0] = q[head][0];
				tmp[q[head][2]][1] = q[head][1];
			}
			ans[d.id][0] = tmpp_ans[0];
			ans[d.id][1] = tmpp_ans[1];
		}
		for (l = block_r[i], r = l; h[1].size > 0; r++) {
			if (S[r][0] == 'A') {
				tmp[P[r]][0]++;
				if (tmp[P[r]][0] == 1) {
					tmp_ans[0]++;
					tmp_ans[1] += tmp[P[r]][1];
				}
			} else if (tmp[P[r]][0] == 0) tmp[P[r]][1]++;
			
			while (h[1].size > 0 && h[1].obj[1].key == r) {
				d = pop(&(h[1]));
				tmpp_ans[0] = tmp_ans[0];
				tmpp_ans[1] = tmp_ans[1];
				for (head = 0; l > L[d.id]; ) {
					l--;
					q[head][0] = tmp[P[l]][0];
					q[head][1] = tmp[P[l]][1];
					q[head++][2] = P[l];
					if (S[l][0] == 'A') {
						if (tmp[P[l]][0] > 0) tmpp_ans[1] -= tmp[P[l]][1];
						tmp[P[l]][0]++;
						tmp[P[l]][1] = 0;
						if (tmp[P[l]][0] == 1) tmpp_ans[0]++;
					} else {
						if (tmp[P[l]][0] > 0) tmpp_ans[1]++;
						tmp[P[l]][1]++;
					}
				}
				for (head--, l = block_r[i]; head >= 0; head--) {
					tmp[q[head][2]][0] = q[head][0];
					tmp[q[head][2]][1] = q[head][1];
				}
				ans[d.id][0] = tmpp_ans[0];
				ans[d.id][1] = tmpp_ans[1];
			}
		}
	}
	for (i = 1; i <= Q; i++) printf("%d %d\n", ans[i][0], ans[i][1]);
	fflush(stdout);
	return 0;
}
0