結果

問題 No.2336 Do you like typical problems?
ユーザー 👑 chro_96chro_96
提出日時 2023-06-02 23:13:20
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 661 ms / 2,000 ms
コード長 3,173 bytes
コンパイル時間 923 ms
コンパイル使用メモリ 32,524 KB
実行使用メモリ 39,864 KB
最終ジャッジ日時 2023-08-28 05:43:54
合計ジャッジ時間 7,238 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
33,728 KB
testcase_01 AC 9 ms
33,820 KB
testcase_02 AC 9 ms
33,816 KB
testcase_03 AC 9 ms
33,724 KB
testcase_04 AC 10 ms
33,868 KB
testcase_05 AC 10 ms
33,740 KB
testcase_06 AC 9 ms
33,836 KB
testcase_07 AC 10 ms
33,740 KB
testcase_08 AC 14 ms
33,940 KB
testcase_09 AC 13 ms
33,784 KB
testcase_10 AC 15 ms
33,852 KB
testcase_11 AC 13 ms
33,784 KB
testcase_12 AC 14 ms
33,860 KB
testcase_13 AC 649 ms
39,836 KB
testcase_14 AC 653 ms
39,784 KB
testcase_15 AC 661 ms
39,728 KB
testcase_16 AC 650 ms
39,864 KB
testcase_17 AC 651 ms
39,812 KB
testcase_18 AC 280 ms
37,136 KB
testcase_19 AC 295 ms
37,504 KB
testcase_20 AC 484 ms
39,820 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