提出 #72206730


ソースコード 拡げる

import sys
import string
import math
import bisect
import os
import heapq
import operator
from io import BytesIO, IOBase
from heapq import heappop,heappush
from functools import lru_cache,cache
from copy import copy,deepcopy
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations
from array import array


INF = float('inf')
BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
 
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = sys.stdin.buffer.readline

ask = lambda *x:print('?',*x,flush=True)
reply = lambda *x:print('!',*x,flush=True)

RI = lambda: int(sys.stdin.readline())
RF = lambda: float(sys.stdin.readline())
RS = lambda: sys.stdin.readline().strip()
RFF = lambda: map(float, sys.stdin.readline().split())
RII = lambda: map(int, sys.stdin.readline().split())
RSS = lambda: map(str, sys.stdin.readline().strip().split())
RIL = lambda: list(RII())
RFL = lambda: list(RFF())
RSL = lambda: list(RSS())

from types import GeneratorType
def bootstrap(f, stack=[]):
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break 
                    to = stack[-1].send(to)
            return to

    return wrappedfunc

class Fenwick:
    def __init__(self,a):
        self.n = len(a)
        self.d = [0]*(self.n+1)
        for p in range(1,self.n+1):
            x = a[p-1]
            while p<=self.n:
                self.d[p] += x
                p += (p&-p)
    
    def update(self,p,x):
        p += 1
        while p<=self.n:
            self.d[p] += x
            p += (p&-p)
    
    def query(self,p):
        p += 1
        res = 0
        while p>=1:
            res += self.d[p]
            p -= (p&-p)
        return res

def main():
    n = RI()
    a = RIL()
    
    MOD = 998244353

    d = Fenwick([0]*(n+2))
    
    f = [0]*(n+1)
    
    for i in range(n-1,-1,-1):
        f[i] = d.query(a[i]-1)
        d.update(a[i],1)
    
    ans = 0
    
    dp = [0]*(n+1)
    d = Fenwick([0]*(n+2))
    for i in range(n):
        val = d.query(a[i]-1)
        dp[i] = (2*dp[i-1]+val)%MOD
        d.update(a[i],1)
        
        ans += (dp[i-1]+val)*f[i]%MOD
        ans %= MOD
    
    #print(dp,f)
    
    print(ans)
    
    
if __name__ == '__main__':
    main()

                

提出情報

提出日時
問題 F - Beautiful Kadomatsu
ユーザ x3x3
言語 Python (PyPy 3.11-v7.3.20)
得点 525
コード長 4277 Byte
結果 AC
実行時間 268 ms
メモリ 169692 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 525 / 525
結果
AC × 3
AC × 24
セット名 テストケース
Sample sample_01.txt, sample_02.txt, sample_03.txt
All sample_01.txt, sample_02.txt, sample_03.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt
ケース名 結果 実行時間 メモリ
sample_01.txt AC 127 ms 110360 KiB
sample_02.txt AC 128 ms 110344 KiB
sample_03.txt AC 128 ms 110600 KiB
test_01.txt AC 128 ms 110588 KiB
test_02.txt AC 202 ms 141328 KiB
test_03.txt AC 218 ms 150236 KiB
test_04.txt AC 169 ms 125316 KiB
test_05.txt AC 237 ms 154716 KiB
test_06.txt AC 208 ms 141472 KiB
test_07.txt AC 245 ms 159304 KiB
test_08.txt AC 187 ms 134580 KiB
test_09.txt AC 224 ms 149332 KiB
test_10.txt AC 200 ms 138052 KiB
test_11.txt AC 176 ms 129772 KiB
test_12.txt AC 243 ms 169488 KiB
test_13.txt AC 240 ms 169420 KiB
test_14.txt AC 264 ms 169692 KiB
test_15.txt AC 268 ms 169596 KiB
test_16.txt AC 268 ms 169316 KiB
test_17.txt AC 265 ms 169684 KiB
test_18.txt AC 265 ms 169388 KiB
test_19.txt AC 268 ms 169288 KiB
test_20.txt AC 265 ms 169356 KiB
test_21.txt AC 268 ms 169416 KiB