結果

問題 No.2616 中央番目の中央値
ユーザー 👑 timitimi
提出日時 2024-02-06 16:40:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 899 ms / 2,000 ms
コード長 2,478 bytes
コンパイル時間 273 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 256,548 KB
最終ジャッジ日時 2024-02-06 16:41:15
合計ジャッジ時間 22,109 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 221 ms
252,084 KB
testcase_01 AC 236 ms
252,084 KB
testcase_02 AC 227 ms
252,084 KB
testcase_03 AC 233 ms
252,084 KB
testcase_04 AC 216 ms
252,084 KB
testcase_05 AC 219 ms
252,088 KB
testcase_06 AC 214 ms
252,092 KB
testcase_07 AC 220 ms
252,088 KB
testcase_08 AC 229 ms
252,088 KB
testcase_09 AC 221 ms
252,088 KB
testcase_10 AC 227 ms
252,092 KB
testcase_11 AC 233 ms
252,092 KB
testcase_12 AC 242 ms
252,100 KB
testcase_13 AC 279 ms
252,268 KB
testcase_14 AC 269 ms
256,548 KB
testcase_15 AC 273 ms
251,796 KB
testcase_16 AC 288 ms
255,324 KB
testcase_17 AC 322 ms
255,796 KB
testcase_18 AC 331 ms
251,288 KB
testcase_19 AC 425 ms
251,440 KB
testcase_20 AC 462 ms
251,436 KB
testcase_21 AC 626 ms
251,564 KB
testcase_22 AC 894 ms
250,236 KB
testcase_23 AC 875 ms
250,188 KB
testcase_24 AC 588 ms
250,184 KB
testcase_25 AC 596 ms
250,188 KB
testcase_26 AC 862 ms
250,188 KB
testcase_27 AC 899 ms
250,188 KB
testcase_28 AC 891 ms
250,188 KB
testcase_29 AC 861 ms
250,188 KB
testcase_30 AC 867 ms
250,184 KB
testcase_31 AC 871 ms
250,188 KB
testcase_32 AC 863 ms
250,184 KB
testcase_33 AC 881 ms
250,184 KB
testcase_34 AC 880 ms
250,188 KB
testcase_35 AC 861 ms
250,184 KB
testcase_36 AC 877 ms
250,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N=int(input())
A=list(map(int, input().split()))

#####segfunc#####
def segfunc(x, y):
    return x+y
#################

#####ide_ele#####
ide_ele =0
#################

class SegTree:
    """
    init(init_val, ide_ele): 配列init_valで初期化 O(N)
    update(k, x): k番目の値をxに更新 O(logN)
    query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
    """
    def __init__(self, init_val, segfunc, ide_ele):
        """
        init_val: 配列の初期値
        segfunc: 区間にしたい操作
        ide_ele: 単位元
        n: 要素数
        num: n以上の最小の2のべき乗
        tree: セグメント木(1-index)
        """
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num
        # 配列の値を葉にセット
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        """
        k番目の値をxに更新
        k: index(0-index)
        x: update value
        """
        k += self.num
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
            k >>= 1

    def query(self, l, r):
        """
        [l, r)のsegfuncしたものを得る
        l: index(0-index)
        r: index(0-index)
        """
        res = self.ide_ele

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.tree[r - 1])
            l >>= 1
            r >>= 1
        return res

mod=998244353;n=10**6+100
inv_t=[0]+[1]
for i in range(2,n):
  inv_t+=[inv_t[mod%i]*(mod-int(mod/i))%mod]

kai=[1,1]
rev_kai=[1,inv_t[1]]
for i in range(2,n):
  kai.append(kai[-1]*i%mod)
  rev_kai.append(rev_kai[-1]*inv_t[i]%mod)

def cmb(n,r):
  return kai[n]*rev_kai[r]*rev_kai[n-r]%mod

LL=[0]*N;RR=[1]*N
L=SegTree(LL, segfunc, ide_ele)
R=SegTree(RR, segfunc, ide_ele)
ll,rr=0,N;ans=0
for a in A:
  a-=1;rr-=1
  R.update(a,0)
  l,r=L.query(0,a),R.query(0,a)
  aa,bb,cc,dd=l,ll-l,r,rr-r
  ll+=1
  ans+=cmb(aa+dd,aa)*cmb(bb+cc,bb)
  ans%=mod
  L.update(a,1)
print(ans)
0