답안 #643649

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
643649 2022-09-22T17:37:13 Z jbolt Žarulje (COI15_zarulje) C++14
100 / 100
113 ms 42744 KB
#include <stdio.h>
#include <stdlib.h>
 
#define N	200000
#define MD	1000000007
 
unsigned int X = 12345;
 
int rand_() {
	return (X *= 3) >> 1;
}
 
int vv[N + 1], ff[N + 1], gg[N + 1];
 
void init() {
	int i;
 
	ff[0] = gg[0] = 1;
	for (i = 1; i <= N; i++) {
		vv[i] = i == 1 ? 1 : (long long) vv[i - MD % i] * (MD / i + 1) % MD;
		ff[i] = (long long) ff[i - 1] * i % MD;
		gg[i] = (long long) gg[i - 1] * vv[i] % MD;
	}
}
 
int choose(int n, int k) {
	return (long long) ff[n] * gg[k] % MD * gg[n - k] % MD;
}
 
int vchoose(int n, int k) {
	return (long long) gg[n] * ff[k] % MD * ff[n - k] % MD;
}
 
int main() {
	static int aa[N], qu[N], cc[N], nxt[N], *qul[N], *ccl[N], kkl[N], *qur[N], *ccr[N], kkr[N], ans[N], dd[N];
	int n, q, h, hl, hr, i, j, cnt;
 
	init();
	scanf("%d%d", &n, &q);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	cnt = 0;
	for (i = 0; i < n; i++) {
		int cnt_ = cnt;
 
		while (cnt && aa[qu[cnt - 1]] > aa[i])
			cnt--;
		if (cnt == 0 || aa[qu[cnt - 1]] != aa[i]) {
			kkl[i] = cnt_ - cnt;
			qul[i] = (int *) malloc(kkl[i] * sizeof *qul[i]);
			ccl[i] = (int *) malloc(kkl[i] * sizeof *ccl[i]);
			for (h = cnt; h < cnt_; h++)
				qul[i][cnt_ - 1 - h] = qu[h], ccl[i][cnt_ - 1 - h] = cc[h];
			qu[cnt] = i, cc[cnt] = 1, cnt++;
		} else {
			kkl[i] = cnt_ - cnt + 1;
			qul[i] = (int *) malloc(kkl[i] * sizeof *qul[i]);
			ccl[i] = (int *) malloc(kkl[i] * sizeof *ccl[i]);
			for (h = cnt - 1; h < cnt_; h++)
				qul[i][cnt_ - 1 - h] = qu[h], ccl[i][cnt_ - 1 - h] = cc[h];
			qu[cnt - 1] = i, cc[cnt - 1]++;
		}
	}
	cnt = 0;
	for (i = n - 1; i >= 0; i--) {
		int cnt_ = cnt;
 
		while (cnt && aa[qu[cnt - 1]] > aa[i])
			cnt--;
		nxt[i] = cnt == 0 ? n : qu[cnt - 1];
		if (cnt == 0 || aa[qu[cnt - 1]] != aa[i]) {
			kkr[i] = cnt_ - cnt;
			qur[i] = (int *) malloc(kkr[i] * sizeof *qur[i]);
			ccr[i] = (int *) malloc(kkr[i] * sizeof *ccr[i]);
			for (h = cnt; h < cnt_; h++)
				qur[i][cnt_ - 1 - h] = qu[h], ccr[i][cnt_ - 1 - h] = cc[h];
			qu[cnt] = i, cc[cnt] = 1, cnt++;
		} else {
			kkr[i] = cnt_ - cnt + 1;
			qur[i] = (int *) malloc(kkr[i] * sizeof *qur[i]);
			ccr[i] = (int *) malloc(kkr[i] * sizeof *ccr[i]);
			for (h = cnt - 1; h < cnt_; h++)
				qur[i][cnt_ - 1 - h] = qu[h], ccr[i][cnt_ - 1 - h] = cc[h];
			qu[cnt - 1] = i, cc[cnt - 1]++;
		}
	}
	for (i = 0; i < n; i++)
		ans[i] = 1;
	for (i = 0; i < n; i++) {
		hl = 0, hr = 0;
		while (hl < kkl[i] && hr < kkr[i])
			if (aa[qul[i][hl]] > aa[qur[i][hr]])
				hl++;
			else if (aa[qul[i][hl]] < aa[qur[i][hr]])
				hr++;
			else {
				ans[i] = (long long) ans[i] * choose(ccl[i][hl] + ccr[i][hr], ccl[i][hl]) % MD;
				hl++, hr++;
			}
	}
	for (i = 0; i < n; i++)
		dd[i] = 1;
	for (i = 0; i < n; i++)
		if (kkl[i] == 0 || aa[qul[i][kkl[i] - 1]] != aa[i]) {
			int c = kkr[i] == 0 || aa[qur[i][kkr[i] - 1]] != aa[i] ? 0 : ccr[i][kkr[i] - 1];
 
			for (h = 0, j = i; h < c; h++, j = nxt[j]) {
				dd[j + 1] = (long long) dd[j + 1] * choose(c + 1, h + 1) % MD;
				dd[nxt[j]] = (long long) dd[nxt[j]] * vchoose(c + 1, h + 1) % MD;
			}
		}
	for (i = 1; i < n; i++)
		dd[i] = (long long) dd[i] * dd[i - 1] % MD;
	for (i = 1; i < n; i++)
		ans[i] = (long long) ans[i] * dd[i] % MD;
	while (q--) {
		scanf("%d", &i), i--;
		printf("%d\n", ans[i]);
	}
	return 0;
}

Compilation message

zarulje.cpp: In function 'int main()':
zarulje.cpp:39:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |  scanf("%d%d", &n, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~
zarulje.cpp:41:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |   scanf("%d", &aa[i]);
      |   ~~~~~^~~~~~~~~~~~~~
zarulje.cpp:117:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 |   scanf("%d", &i), i--;
      |   ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2644 KB Output is correct
2 Correct 3 ms 2772 KB Output is correct
3 Correct 4 ms 3028 KB Output is correct
4 Correct 5 ms 3036 KB Output is correct
5 Correct 4 ms 3156 KB Output is correct
6 Correct 5 ms 3028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 20632 KB Output is correct
2 Correct 61 ms 38948 KB Output is correct
3 Correct 64 ms 39204 KB Output is correct
4 Correct 77 ms 39508 KB Output is correct
5 Correct 65 ms 39680 KB Output is correct
6 Correct 66 ms 39884 KB Output is correct
7 Correct 67 ms 39912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2644 KB Output is correct
2 Correct 3 ms 2772 KB Output is correct
3 Correct 4 ms 3028 KB Output is correct
4 Correct 5 ms 3036 KB Output is correct
5 Correct 4 ms 3156 KB Output is correct
6 Correct 5 ms 3028 KB Output is correct
7 Correct 32 ms 20632 KB Output is correct
8 Correct 61 ms 38948 KB Output is correct
9 Correct 64 ms 39204 KB Output is correct
10 Correct 77 ms 39508 KB Output is correct
11 Correct 65 ms 39680 KB Output is correct
12 Correct 66 ms 39884 KB Output is correct
13 Correct 67 ms 39912 KB Output is correct
14 Correct 11 ms 4564 KB Output is correct
15 Correct 57 ms 22612 KB Output is correct
16 Correct 113 ms 42252 KB Output is correct
17 Correct 84 ms 40980 KB Output is correct
18 Correct 111 ms 42744 KB Output is correct
19 Correct 100 ms 40752 KB Output is correct
20 Correct 106 ms 41528 KB Output is correct
21 Correct 107 ms 41680 KB Output is correct