結果

問題 No.2336 Do you like typical problems?
ユーザー 👑 ygussanyygussany
提出日時 2023-06-02 22:20:52
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 532 ms / 2,000 ms
コード長 1,930 bytes
コンパイル時間 1,180 ms
コンパイル使用メモリ 32,000 KB
実行使用メモリ 6,612 KB
最終ジャッジ日時 2023-08-28 04:06:15
合計ジャッジ時間 5,035 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 0 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 6 ms
4,376 KB
testcase_09 AC 5 ms
4,376 KB
testcase_10 AC 5 ms
4,376 KB
testcase_11 AC 5 ms
4,376 KB
testcase_12 AC 6 ms
4,376 KB
testcase_13 AC 526 ms
6,608 KB
testcase_14 AC 529 ms
6,532 KB
testcase_15 AC 530 ms
6,404 KB
testcase_16 AC 530 ms
6,584 KB
testcase_17 AC 532 ms
6,596 KB
testcase_18 AC 55 ms
6,612 KB
testcase_19 AC 87 ms
6,612 KB
testcase_20 AC 135 ms
6,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

const int Mod = 998244353, half = 499122177;

long long div_mod(long long x, long long y, long long z)
{
	if (x % y == 0) return x / y;
	else return (div_mod((1 + x / y) * y - x, (z % y), y) * z + x) / y;
}

typedef struct {
	int key, id;
} data;

typedef struct {
	data obj[400001];
	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;
}

int main()
{
	int i, N, B[200001], C[200001];
	scanf("%d", &N);
	for (i = 1; i <= N; i++) scanf("%d %d", &(B[i]), &(C[i]));
	if (N == 1) {
		printf("0\n");
		return 0;
	}
	
	int k;
	long long ans = (long long)N * (N - 1) / 2 % Mod, sum = 0, tmp = 0;
	data d;
	min_heap h;
	h.size = 0;
	for (i = 1; i <= N; i++) {
		d.key = B[i];
		d.id = i;
		push(&h, d);
		d.key = C[i] + 1;
		d.id = -i;
		push(&h, d);
	}
	i = 0;
	while (h.size > 0) {
		k = h.obj[1].key;
		sum += tmp * tmp % Mod * (k - i) % Mod;
		while (h.size > 0 && h.obj[1].key == k) {
			d = pop(&h);
			if (d.id > 0) tmp += div_mod(1, C[d.id] - B[d.id] + 1, Mod);
			else tmp -= div_mod(1, C[-d.id] - B[-d.id] + 1, Mod);
			if (tmp >= Mod) tmp -= Mod;
			else if (tmp < 0) tmp += Mod;
		}
		i = k;
	}
	for (i = 1; i <= N; i++) sum += Mod - div_mod(1, C[i] - B[i] + 1, Mod);
	for (i = 3, ans = (ans - sum % Mod * half % Mod + Mod) % Mod; i <= N; i++) ans = ans * i % Mod;
	printf("%lld\n", ans);
	fflush(stdout);
	return 0;
}
0