結果
問題 | No.2336 Do you like typical problems? |
ユーザー |
![]() |
提出日時 | 2023-06-02 23:13:20 |
言語 | C (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 665 ms / 2,000 ms |
コード長 | 3,173 bytes |
コンパイル時間 | 1,954 ms |
コンパイル使用メモリ | 33,408 KB |
実行使用メモリ | 40,064 KB |
最終ジャッジ日時 | 2024-12-28 22:05:13 |
合計ジャッジ時間 | 6,200 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 18 |
ソースコード
#include <stdio.h>#include <stdlib.h>int cmp_ll (const void *ap, const void *bp) {long long a = *(long long *)ap;long long b = *(long long *)bp;if (a < b) {return -1;}if (a > b) {return 1;}return 0;}void add_segt (long long *t, int idx, long long val, int size) {idx = idx+size-1;t[idx] += val;while (idx > 0) {idx = (idx-1)/2;t[idx] += val;}return;}long long sum_segt_rec (long long *t, int a, int b, int k, int l, int r) {long long ans = 0LL;if (r <= a || b <= l) {return 0LL;}if (a <= l && r <= b) {return t[k];}ans += sum_segt_rec(t, a, b, 2*k+1, l, (l+r)/2);ans += sum_segt_rec(t, a, b, 2*k+2, (l+r)/2, r);return ans;}long long sum_segt (long long *t, int a, int b, int size) {return sum_segt_rec(t, a, b, 0, 0, size);}long long power_mod (long long a, long long b, long long mod_num) {long long ans = 1LL;if (b > 0LL) {ans = power_mod(a, b/2LL, mod_num);ans = (ans * ans) % mod_num;if (b%2LL == 1LL) {ans = (ans * (a % mod_num)) % mod_num;}}return ans;}int main () {int n = 0;long long b[200000] = {};long long c[200000] = {};int res = 0;long long ans = 0LL;long long mod_num = 998244353LL;long long t[2][1048576] = {};int size = 1;long long map[400000] = {};int ucnt = 1;long long bc[400000][2] = {};long long p[200000][2] = {};res = scanf("%d", &n);for (int i = 0; i < n; i++) {res = scanf("%lld", b+i);res = scanf("%lld", c+i);bc[i][0] = b[i];bc[i][1] = (long long)i;bc[n+i][0] = c[i];bc[n+i][1] = (long long)(n+i);}qsort(bc, 2*n, sizeof(long long)*2, cmp_ll);map[0] = bc[0][0];if (bc[0][1] < (long long)n) {b[(int)bc[0][1]] = 0LL;} else {c[((int)bc[0][1])-n] = 0LL;}for (int i = 1; i < 2*n; i++) {if (bc[i-1][0] != bc[i][0]) {map[ucnt] = bc[i][0];ucnt++;}if (bc[i][1] < (long long)n) {b[(int)bc[i][1]] = (long long)(ucnt-1);} else {c[((int)bc[i][1])-n] = (long long)(ucnt-1);}}while (size <= ucnt) {size <<= 1;}for (int i = 0; i < n; i++) {p[i][0] = b[i];p[i][1] = c[i];}qsort(p, n, sizeof(long long)*2, cmp_ll);for (int i = 0; i < n; i++) {long long tmp = sum_segt(t[0], (int)p[i][0], (int)p[i][1], size)%mod_num;long long cnt = sum_segt(t[1], (int)p[i][0], (int)p[i][1], size)%mod_num;long long div = power_mod(map[(int)p[i][1]]-map[(int)p[i][0]]+1LL, mod_num-2LL, mod_num);tmp += (sum_segt(t[1], (int)p[i][1], ucnt, size)%mod_num)*(map[(int)p[i][1]]-map[(int)p[i][0]]+1LL);tmp %= mod_num;tmp += mod_num-(cnt*(map[(int)p[i][0]]-1LL))%mod_num;tmp %= mod_num;tmp *= div;ans += mod_num-tmp%mod_num;ans += (long long)i;add_segt(t[0], (int)p[i][1], (map[(int)p[i][1]]*div)%mod_num, size);add_segt(t[1], (int)p[i][1], div, size);}ans %= mod_num;for (int i = 2; i < n; i++) {ans *= (long long)(i+1);ans %= mod_num;}printf("%lld\n", ans%mod_num);return 0;}