結果

問題 No.2336 Do you like typical problems?
ユーザー 👑 chro_96chro_96
提出日時 2023-06-02 23:10:19
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 3,163 bytes
コンパイル時間 802 ms
コンパイル使用メモリ 33,664 KB
実行使用メモリ 40,196 KB
最終ジャッジ日時 2024-06-09 01:12:45
合計ジャッジ時間 6,390 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
33,772 KB
testcase_01 AC 11 ms
33,900 KB
testcase_02 AC 9 ms
33,896 KB
testcase_03 AC 9 ms
33,896 KB
testcase_04 AC 10 ms
33,896 KB
testcase_05 AC 10 ms
33,896 KB
testcase_06 AC 10 ms
33,900 KB
testcase_07 AC 11 ms
33,896 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 267 ms
38,028 KB
testcase_19 WA -
testcase_20 AC 451 ms
39,844 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)*(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