結果

問題 No.2250 Split Permutation
ユーザー sotanishy
提出日時 2023-03-17 22:14:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 505 ms / 3,000 ms
コード長 950 bytes
コンパイル時間 191 ms
コンパイル使用メモリ 82,348 KB
実行使用メモリ 109,768 KB
最終ジャッジ日時 2024-09-18 11:20:35
合計ジャッジ時間 7,554 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class FenwickTree:
    def __init__(self, size):
        self.data = [0] * (size + 1)
        self.size = size

    # i is exclusive
    def prefix_sum(self, i):
        s = 0
        while i > 0:
            s = (s + self.data[i]) % mod
            i -= i & -i
        return s

    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.data[i] = (self.data[i] + x) % mod
            i += i & -i

mod = 998244353
N = int(input())
P = list(map(lambda x: int(x)-1, input().split()))
pow2 = [1] * (N+1)
for i in range(1, N+1):
    pow2[i] = pow2[i-1] * 2 % mod
ft_cnt = FenwickTree(N)
ft_val = FenwickTree(N)
ans = 0
for i, x in enumerate(P):
    cnt = ft_cnt.prefix_sum(N) - ft_cnt.prefix_sum(x)
    val = ft_val.prefix_sum(N) - ft_val.prefix_sum(x)
    ans = (ans + pow2[N-1] * (cnt - val * pow(pow2[i], mod-2, mod))) % mod
    ft_cnt.add(x, 1)
    ft_val.add(x, pow2[i])
print(ans)
0