結果

問題 No.1378 Flattening
ユーザー 👑 ygussanyygussany
提出日時 2022-08-15 00:40:09
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 1,562 ms / 2,000 ms
コード長 2,343 bytes
コンパイル時間 270 ms
コンパイル使用メモリ 34,688 KB
実行使用メモリ 290,844 KB
最終ジャッジ日時 2024-04-09 03:27:24
合計ジャッジ時間 11,841 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,820 KB
testcase_01 AC 1 ms
6,948 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,948 KB
testcase_05 AC 1 ms
6,948 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,948 KB
testcase_08 AC 1 ms
6,948 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 1 ms
6,948 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 1 ms
6,948 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 1 ms
6,948 KB
testcase_16 AC 2 ms
6,948 KB
testcase_17 AC 1 ms
6,944 KB
testcase_18 AC 1 ms
6,948 KB
testcase_19 AC 1 ms
6,944 KB
testcase_20 AC 1 ms
6,948 KB
testcase_21 AC 1 ms
6,948 KB
testcase_22 AC 1 ms
6,948 KB
testcase_23 AC 1 ms
6,944 KB
testcase_24 AC 1 ms
6,944 KB
testcase_25 AC 1 ms
6,944 KB
testcase_26 AC 1 ms
6,948 KB
testcase_27 AC 1 ms
6,944 KB
testcase_28 AC 1 ms
6,948 KB
testcase_29 AC 1 ms
6,948 KB
testcase_30 AC 1 ms
6,948 KB
testcase_31 AC 1,562 ms
269,604 KB
testcase_32 AC 1,408 ms
270,188 KB
testcase_33 AC 889 ms
278,596 KB
testcase_34 AC 939 ms
277,828 KB
testcase_35 AC 1,420 ms
290,844 KB
testcase_36 AC 853 ms
268,760 KB
testcase_37 AC 226 ms
274,724 KB
testcase_38 AC 225 ms
274,328 KB
testcase_39 AC 228 ms
274,636 KB
testcase_40 AC 256 ms
274,404 KB
testcase_41 AC 224 ms
274,956 KB
testcase_42 AC 253 ms
267,148 KB
testcase_43 AC 245 ms
266,836 KB
testcase_44 AC 246 ms
267,160 KB
testcase_45 AC 32 ms
83,036 KB
testcase_46 AC 45 ms
102,052 KB
testcase_47 AC 11 ms
35,632 KB
testcase_48 AC 181 ms
239,052 KB
testcase_49 AC 202 ms
255,096 KB
testcase_50 AC 28 ms
75,244 KB
testcase_51 AC 1 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

const int Mod = 998244353;

const int inf = 0;
int leaf[5001];

typedef struct {
	int left, right, max;
} seg_node;

void init_node(seg_node v[], int k, int l, int r)
{
	v[k].left = l;
	v[k].right = r;
	v[k].max = inf;
	if (l < r) {
		init_node(v, k << 1, l, (l + r) / 2);
		init_node(v, (k << 1) ^ 1, (l + r) / 2 + 1, r);
	} else leaf[l] = k;
}

void update_node(seg_node v[], int k, int x)
{
	int i, j = leaf[k];
	v[j].max = x;
	for (i = j >> 1; i > 0; j = i, i >>= 1) v[i].max = (v[j].max > v[j^1].max)? v[j].max: v[j^1].max;
}

int BS_left(seg_node v[], int k, int l, int r, int x)
{
	int tmp;
	if (v[k].max < x || v[k].right < l || v[k].left > r) return r + 1;
	else if (v[k].left == v[k].right) return v[k].left;
	else {
		tmp = BS_left(v, k << 1, l, r, x);
		if (tmp <= r) return tmp;
		else return BS_left(v, (k << 1) ^ 1, l, r, x);
	}
}

int BS_right(seg_node v[], int k, int l, int r, int x)
{
	int tmp;
	if (v[k].max < x || v[k].right < l || v[k].left > r) return l - 1;
	else if (v[k].left == v[k].right) return v[k].left;
	else {
		tmp = BS_right(v, (k << 1) ^ 1, l, r, x);
		if (tmp >= l) return tmp;
		else return BS_right(v, k << 1, l, r, x);
	}
}

int main()
{
	int i, N, P[5001];
	scanf("%d", &N);
	for (i = 1, P[0] = 0; i <= N; i++) { scanf("%d", &(P[i])); P[i] = N + 1 - P[i]; }
	
	seg_node v[100000];
	init_node(v, 1, 1, N);
	for (i = 1; i <= N; i++) update_node(v, i, P[i]);
	
	int j, k;
	static unsigned int dp[3][5001][5001] = {};
	for (i = 1, dp[0][0][0] = 1; i <= N; i++) {
		dp[0][i][P[i]] = (dp[0][i-1][P[i-1]] + dp[1][i-1][0] + dp[2][i-1][P[i]]) % Mod;
		for (j = P[i] - 1; j >= 0; j--) dp[0][i][j] = dp[0][i][j+1];
		
		j = BS_right(v, 1, 1, i - 1, P[i]);
		while (j >= 1) {
			dp[1][i][P[j]] = (dp[0][i-1][P[j]] + dp[1][i-1][P[j]]) % Mod;
			j = BS_right(v, 1, 1, j - 1, P[j]);
		}
		for (j = N - 1; j >= 0; j--) {
			dp[1][i][j] += dp[1][i][j+1];
			if (dp[1][i][j] >= Mod) dp[1][i][j] -= Mod;
		}

		j = BS_left(v, 1, i + 1, N, P[i]);
		while (j <= N) {
			dp[2][i][P[j]] = (dp[0][i-1][P[i-1]] + dp[1][i-1][0] + dp[2][i-1][P[j]]) % Mod;
			j = BS_left(v, 1, j + 1, N, P[j]);
		}
		for (j = 1; j <= N; j++) {
			dp[2][i][j] += dp[2][i][j-1];
			if (dp[2][i][j] >= Mod) dp[2][i][j] -= Mod;
		}
	}
	printf("%u\n", (dp[0][N][0] + dp[1][N][0] + dp[2][N][N]) % Mod);
	fflush(stdout);
	return 0;
}
0