結果

問題 No.1712 Read and Pile
ユーザー chineristACchineristAC
提出日時 2021-05-06 22:33:50
言語 PyPy3
(7.3.15)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,690 bytes
コンパイル時間 175 ms
コンパイル使用メモリ 81,752 KB
実行使用メモリ 131,256 KB
最終ジャッジ日時 2023-10-17 19:42:26
合計ジャッジ時間 21,351 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
58,084 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
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 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 700 ms
107,900 KB
testcase_36 AC 748 ms
109,284 KB
testcase_37 AC 696 ms
107,996 KB
testcase_38 AC 46 ms
56,044 KB
testcase_39 AC 68 ms
86,872 KB
testcase_40 AC 72 ms
89,548 KB
testcase_41 WA -
testcase_42 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

class BIT():
    def __init__(self,n,mod=None):
        self.BIT=[0]*(n+1)
        self.num=n
        self.mod = mod

    def query(self,idx):
        res_sum = 0
        while idx > 0:
            res_sum += self.BIT[idx]
            if self.mod:
                res_sum %= self.mod
            idx -= idx&(-idx)
        return res_sum

    #Ai += x O(logN)
    def update(self,idx,x):
        while idx <= self.num:
            self.BIT[idx] += x
            if self.mod:
                self.BIT[idx] %= self.mod
            idx += idx&(-idx)
        return

import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import log,gcd

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

mod = 998244353
i2 = pow(2,mod-2,mod)

def solve(N,M,A):
    if N==1:
        return M
    if N==2:
        res = M
        pre = 1
        Z = A.count(0)
        for i in range(M):
            if A[i]==0:
                res += i2
                res %= mod
            else:
                if pre!=0 and A[i]!=pre:
                    res += 1
                    res %= mod
                elif pre==0:
                    res += i2
                    res %= mod
            pre = A[i]
        return res

    p = ((N-2)*pow(N,mod-2,mod)) % mod
    ip = pow(p,mod-2,mod)
    pow_p = [1 for i in range(N+M+1)]
    pow_ip = [1 for i in range(N+M+1)]
    for i in range(1,N+M+1):
        pow_p[i] = (pow_p[i-1] * p) % mod
        pow_ip[i] = (pow_ip[i-1] * ip) % mod

    A = [N-i for i in range(N)] + A
    A = [-1] + A

    zero = [A[i]==0 for i in range(N+M+1)]
    for i in range(1,N+M+1):
        zero[i] += zero[i-1]

    lastappear = [N-i+1 for i in range(N+1)]
    res = 0
    bit_p = BIT(N+M,mod=998244353)
    bit_cnt = BIT(N+M)
    for i in range(1,N+1):
        bit_cnt.update(i,1)
        bit_p.update(i,pow_ip[zero[i]])

    for i in range(N+1,N+M+1):
        if A[i]==0:
            res += (N-1)
            res %= mod
        else:
            pre = lastappear[A[i]]
            k = bit_cnt.query(pre-1)

            res = (res + N-1) % mod
            res += (bit_p.query(i)-bit_p.query(pre)) * pow_p[zero[i]] % mod
            res %= mod

            res -= k * pow_p[zero[i]-zero[pre]] % mod
            res %= mod

            bit_cnt.update(pre,-1)
            bit_p.update(pre,-pow_ip[zero[pre]])

            lastappear[A[i]] = i
            bit_cnt.update(i,1)
            bit_p.update(i,pow_ip[zero[i]])

    res *= i2
    res %= mod
    res +=  M
    res %= mod

    return res

N,M = mi()
A = li()

print(solve(N,M,A))
0