結果

問題 No.1172 Add Recursive Sequence
ユーザー 👑 ygussanyygussany
提出日時 2020-08-29 09:35:13
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 316 ms / 4,000 ms
コード長 2,472 bytes
コンパイル時間 1,349 ms
コンパイル使用メモリ 33,900 KB
実行使用メモリ 6,656 KB
最終ジャッジ日時 2024-04-26 16:04:40
合計ジャッジ時間 4,059 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 11 ms
5,376 KB
testcase_11 AC 10 ms
5,376 KB
testcase_12 AC 13 ms
5,376 KB
testcase_13 AC 12 ms
5,376 KB
testcase_14 AC 78 ms
6,144 KB
testcase_15 AC 77 ms
6,528 KB
testcase_16 AC 316 ms
6,144 KB
testcase_17 AC 316 ms
6,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

const int Mod = 1000000007;

typedef struct {
	int key, id;
} data;

void merge_sort(data x[], int n)
{
	static data y[100001] = {};
	if (n <= 1) return;
	merge_sort(&(x[0]), n/2);
	merge_sort(&(x[n/2]), (n+1)/2);
	
	int i, p, q;
	for (i = 0, p = 0, q = n/2; i < n; i++) {
		if (p >= n/2) y[i] = x[q++];
		else if (q >= n) y[i] = x[p++];
		else y[i] = (x[p].key < x[q].key)? x[p++]: x[q++];
	}
	for (i = 0; i < n; i++) x[i] = y[i];
}

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

void push(data x, min_heap* h)
{
	int i = ++(h->size);
	data tmp;
	h->obj[i] = x;
	while (i > 1) {
		if (h->obj[i].key < h->obj[i>>1].key) {
			tmp = h->obj[i>>1];
			h->obj[i>>1] = h->obj[i];
			h->obj[i] = tmp;
			i >>= 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 = i << 1;
		} else break;
	}
	return output;
}

int main()
{
	int i, j, k, K, N, M;
	long long a[100001], c[201];
	data d[100001];
	scanf("%d %d %d", &K, &N, &M);
	for (i = 0; i < K; i++) scanf("%lld", &(a[i]));
	for (i = 1; i <= K; i++) scanf("%lld", &(c[i]));
	for (i = 0; i < M; i++) scanf("%d %d", &(d[i].key), &(d[i].id));
	for (i = K; i < N; i++) {
		for (k = 1, a[i] = 0; k <= K; k++) a[i] = (a[i] + c[k] * a[i-k]) % Mod;
	}
	merge_sort(d, M);
	
	long long x[100001] = {}, tmp[100001] = {};
	data e;
	min_heap h[2];
	h[0].size = 0;
	h[1].size = 0;
	for (i = 0, j = 0; i < N; i++) {
		for (; j < M && d[j].key == i; j++) {
			if (d[j].id <= i + K) for (k = i; k < d[j].id; k++) x[k] += a[k-i];
			else {
				for (k = i; k < i + K; k++) x[k] += a[k-i];
				e.key = i + K;
				e.id = d[j].id;
				push(e, &(h[0]));
			}
		}
		if (i >= K) {
			while (h[0].size > 0 && h[0].obj[1].key == i) {
				e = pop(&(h[0]));
				for (k = i - 1; k >= i - K; k--) tmp[k] += a[k-(i-K)];
				e.key = e.id;
				e.id = i - K;
				push(e, &(h[1]));
			}
			while (h[1].size > 0 && h[1].obj[1].key == i) {
				e = pop(&(h[1]));
				for (k = i - 1; k >= i - K; k--) tmp[k] -= a[k-e.id];
			}
			for (k = i - 1; k >= i - K; k--) {
				tmp[k] = (tmp[k] % Mod + Mod) % Mod;
				tmp[i] = (tmp[i] + c[i-k] * tmp[k]) % Mod;
			}
			x[i] += tmp[i];
		}
		
		printf("%lld\n", x[i] % Mod);
	}
	fflush(stdout);
	return 0;
}
0