結果

問題 No.2250 Split Permutation
ユーザー lam6er
提出日時 2025-03-31 17:58:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 540 ms / 3,000 ms
コード長 1,705 bytes
コンパイル時間 253 ms
コンパイル使用メモリ 82,720 KB
実行使用メモリ 119,616 KB
最終ジャッジ日時 2025-03-31 17:58:55
合計ジャッジ時間 8,296 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 998244353

class FenwickTree:
    def __init__(self, size):
        self.size = size
        self.tree = [0] * (size + 2)  # 1-based indexing
    
    def update(self, index, value):
        while index <= self.size:
            self.tree[index] = (self.tree[index] + value) % MOD
            index += index & -index
    
    def query(self, index):
        res = 0
        while index > 0:
            res = (res + self.tree[index]) % MOD
            index -= index & -index
        return res
    
    def range_query(self, l, r):
        if l > r:
            return 0
        return (self.query(r) - self.query(l - 1)) % MOD

def main():
    import sys
    input = sys.stdin.read().split()
    ptr = 0
    N = int(input[ptr])
    ptr += 1
    P = list(map(int, input[ptr:ptr + N]))
    ptr += N
    
    ft_count = FenwickTree(N)
    ft_sum = FenwickTree(N)
    total_inversion = 0
    sum_part = 0
    pow2_N_minus_1 = pow(2, N - 1, MOD)
    
    for j in range(N):
        current_p = P[j]
        current_j = j + 1  # 1-based index
        
        a_j = ft_count.range_query(current_p + 1, N)
        total_inversion = (total_inversion + a_j) % MOD
        
        sum_2i = ft_sum.range_query(current_p + 1, N)
        exponent = (N - 1) - current_j
        pow_term = pow(2, exponent, MOD)
        contribution = (sum_2i * pow_term) % MOD
        sum_part = (sum_part + contribution) % MOD
        
        ft_count.update(current_p, 1)
        pow2_j = pow(2, current_j, MOD)
        ft_sum.update(current_p, pow2_j)
    
    ans = (total_inversion * pow2_N_minus_1 - sum_part) % MOD
    ans = (ans + MOD) % MOD  # Ensure non-negative
    print(ans)

if __name__ == "__main__":
    main()
0