結果

問題 No.2616 中央番目の中央値
ユーザー 👑 chro_96chro_96
提出日時 2024-01-26 23:30:52
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 171 ms / 2,000 ms
コード長 1,984 bytes
コンパイル時間 406 ms
コンパイル使用メモリ 31,872 KB
実行使用メモリ 10,668 KB
最終ジャッジ日時 2024-01-26 23:30:57
合計ジャッジ時間 4,657 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
10,668 KB
testcase_01 AC 5 ms
10,668 KB
testcase_02 AC 5 ms
10,668 KB
testcase_03 AC 5 ms
10,668 KB
testcase_04 AC 5 ms
10,668 KB
testcase_05 AC 4 ms
10,668 KB
testcase_06 AC 4 ms
10,668 KB
testcase_07 AC 4 ms
10,668 KB
testcase_08 AC 4 ms
10,668 KB
testcase_09 AC 4 ms
10,668 KB
testcase_10 AC 5 ms
10,668 KB
testcase_11 AC 5 ms
10,668 KB
testcase_12 AC 5 ms
10,668 KB
testcase_13 AC 5 ms
10,668 KB
testcase_14 AC 6 ms
10,668 KB
testcase_15 AC 7 ms
10,668 KB
testcase_16 AC 11 ms
10,668 KB
testcase_17 AC 14 ms
10,668 KB
testcase_18 AC 22 ms
10,668 KB
testcase_19 AC 46 ms
10,668 KB
testcase_20 AC 44 ms
10,668 KB
testcase_21 AC 102 ms
10,668 KB
testcase_22 AC 159 ms
10,668 KB
testcase_23 AC 163 ms
10,668 KB
testcase_24 AC 85 ms
10,668 KB
testcase_25 AC 85 ms
10,668 KB
testcase_26 AC 165 ms
10,668 KB
testcase_27 AC 166 ms
10,668 KB
testcase_28 AC 164 ms
10,668 KB
testcase_29 AC 166 ms
10,668 KB
testcase_30 AC 166 ms
10,668 KB
testcase_31 AC 162 ms
10,668 KB
testcase_32 AC 168 ms
10,668 KB
testcase_33 AC 163 ms
10,668 KB
testcase_34 AC 166 ms
10,668 KB
testcase_35 AC 171 ms
10,668 KB
testcase_36 AC 166 ms
10,668 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

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 > 0LL) {
      ans = (ans * (a % mod_num)) % mod_num;
    }
  }
  
  return ans;
}

void add_segt (int *t, int idx, int val, int size) {
  idx = idx+size-1;
  t[idx] += val;
  
  while (idx > 0) {
    idx = (idx-1)/2;
    t[idx] += val;
  }
  
  return;
}

int sum_segt_rec (int *t, int a, int b, int k, int l, int r) {
  int ans = 0;
  
  if (r <= a || b <= l) {
    return 0;
  }
  
  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;
}

int sum_segt (int *t, int a, int b, int size) {
  return sum_segt_rec(t, a, b, 0, 0, size);
}

int main () {
  int n = 0;
  int p = 0;
  
  int res = 0;
  
  long long ans = 0LL;
  long long mod_num = 998244353LL;
  
  int t[1048575] = {};
  int size = 1;
  
  long long fact[300001] = {};
  long long invf[300001] = {};
  
  res = scanf("%d", &n);
  while (size <= n) {
    size <<= 1;
  }
  fact[0] = 1LL;
  for (int i = 0; i < n; i++) {
    fact[i+1] = (fact[i]*((long long)(i+1)))%mod_num;
  }
  invf[n] = power_mod(fact[n], mod_num-2LL, mod_num);
  for (int i = n; i > 0; i--) {
    invf[i-1] = (invf[i]*((long long)i))%mod_num;
  }
  for (int i = 0; i < n; i++) {
    int llc = 0;
    int lgc = 0;
    int rlc = 0;
    int rgc = 0;
    long long tmp1 = 0LL;
    long long tmp2 = 0LL;
    res = scanf("%d", &p);
    p--;
    llc = sum_segt(t, 0, p, size);
    lgc = i-llc;
    rlc = p-llc;
    rgc = n-p-1-lgc;
    tmp1 = (fact[llc+rgc]*invf[llc])%mod_num;
    tmp1 *= invf[rgc];
    tmp1 %= mod_num;
    tmp2 = (fact[lgc+rlc]*invf[lgc])%mod_num;
    tmp2 *= invf[rlc];
    tmp2 %= mod_num;
    ans += (tmp1*tmp2)%mod_num;
    add_segt(t, p, 1, size);
  }
  
  printf("%lld\n", ans%mod_num);
  return 0;
}
0