結果

問題 No.2336 Do you like typical problems?
ユーザー chro_96chro_96
提出日時 2023-06-02 23:13:20
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 659 ms / 2,000 ms
コード長 3,173 bytes
コンパイル時間 1,476 ms
コンパイル使用メモリ 33,024 KB
実行使用メモリ 40,064 KB
最終ジャッジ日時 2024-06-09 01:16:15
合計ジャッジ時間 5,940 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
33,768 KB
testcase_01 AC 11 ms
33,772 KB
testcase_02 AC 10 ms
33,792 KB
testcase_03 AC 10 ms
33,896 KB
testcase_04 AC 12 ms
33,792 KB
testcase_05 AC 9 ms
33,792 KB
testcase_06 AC 9 ms
33,792 KB
testcase_07 AC 11 ms
33,792 KB
testcase_08 AC 14 ms
33,900 KB
testcase_09 AC 15 ms
33,772 KB
testcase_10 AC 18 ms
33,768 KB
testcase_11 AC 16 ms
33,920 KB
testcase_12 AC 15 ms
33,772 KB
testcase_13 AC 659 ms
39,936 KB
testcase_14 AC 648 ms
40,064 KB
testcase_15 AC 645 ms
39,808 KB
testcase_16 AC 647 ms
39,936 KB
testcase_17 AC 646 ms
39,936 KB
testcase_18 AC 278 ms
36,864 KB
testcase_19 AC 297 ms
36,992 KB
testcase_20 AC 468 ms
39,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0