結果

問題 No.1300 Sum of Inversions
ユーザー ayaoniayaoni
提出日時 2020-11-27 22:37:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,316 ms / 2,000 ms
コード長 2,494 bytes
コンパイル時間 938 ms
コンパイル使用メモリ 87,252 KB
実行使用メモリ 190,744 KB
最終ジャッジ日時 2023-10-09 20:41:11
合計ジャッジ時間 33,347 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,260 KB
testcase_01 AC 74 ms
71,344 KB
testcase_02 AC 74 ms
71,308 KB
testcase_03 AC 1,004 ms
159,516 KB
testcase_04 AC 959 ms
157,780 KB
testcase_05 AC 772 ms
136,264 KB
testcase_06 AC 1,126 ms
174,972 KB
testcase_07 AC 1,082 ms
171,336 KB
testcase_08 AC 1,165 ms
180,372 KB
testcase_09 AC 1,196 ms
180,928 KB
testcase_10 AC 667 ms
125,596 KB
testcase_11 AC 685 ms
126,348 KB
testcase_12 AC 1,013 ms
159,056 KB
testcase_13 AC 969 ms
157,200 KB
testcase_14 AC 1,316 ms
190,744 KB
testcase_15 AC 1,217 ms
179,504 KB
testcase_16 AC 1,031 ms
160,652 KB
testcase_17 AC 657 ms
124,172 KB
testcase_18 AC 735 ms
131,020 KB
testcase_19 AC 872 ms
144,616 KB
testcase_20 AC 907 ms
146,956 KB
testcase_21 AC 890 ms
147,016 KB
testcase_22 AC 807 ms
136,056 KB
testcase_23 AC 1,151 ms
175,676 KB
testcase_24 AC 800 ms
137,084 KB
testcase_25 AC 695 ms
129,172 KB
testcase_26 AC 699 ms
129,420 KB
testcase_27 AC 751 ms
133,396 KB
testcase_28 AC 1,211 ms
187,096 KB
testcase_29 AC 884 ms
147,604 KB
testcase_30 AC 1,147 ms
178,232 KB
testcase_31 AC 777 ms
136,996 KB
testcase_32 AC 794 ms
136,692 KB
testcase_33 AC 429 ms
118,212 KB
testcase_34 AC 449 ms
130,232 KB
testcase_35 AC 666 ms
157,020 KB
testcase_36 AC 695 ms
175,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())


class SegTree:  # モノイドに対して適用可能、Nが2冪でなくても良い
    def __init__(self,N,seg_func,unit):
        self.N = 1 << (N-1).bit_length()
        self.func = seg_func
        self.unit = unit
        self.tree = [self.unit]*(2*self.N)

    def build(self,init_value):  # 初期値を[N,2N)に格納
        for i in range(len(init_value)):
            self.tree[i+self.N] = init_value[i]
        for i in range(self.N-1,0,-1):
            self.tree[i] = self.func(self.tree[i << 1],self.tree[i << 1 | 1])

    def set_val(self,i,x):  # i番目(0-index)の値をxに変更
        i += self.N
        self.tree[i] = x
        i >>= 1
        while i:
            self.tree[i] = self.func(self.tree[i << 1],self.tree[i << 1 | 1])
            i >>= 1

    def fold(self,L,R):  # [L,R)の区間取得
        L += self.N
        R += self.N
        vL = self.unit
        vR = self.unit
        while L < R:
            if L & 1:
                vL = self.func(vL,self.tree[L])
                L += 1
            if R & 1:
                R -= 1
                vR = self.func(self.tree[R],vR)
            L >>= 1
            R >>= 1
        return self.func(vL,vR)


def seg_func(x,y):
    x0,x1 = x
    y0,y1 = y
    return ((x0+y0) % mod,(x1+y1) % mod)


N = I()
A = LI()
mod = 998244353

AA = sorted(list(set(A)))
d = {}
M = len(AA)
for i in range(M):
    d[AA[i]] = i


ST_left = SegTree(N,seg_func,(0,0))
X_left = []
for i in range(N):
    a = d[A[i]]
    ST_left.set_val(a,seg_func((A[i],1),ST_left.tree[a+ST_left.N]))
    if 1 <= i <= N-2:
        X_left.append(ST_left.fold(a+1,N))

ST_right = SegTree(N,seg_func,(0,0))
X_right = []
for i in range(N-1,-1,-1):
    a = d[A[i]]
    ST_right.set_val(a,seg_func((A[i],1),ST_right.tree[a+ST_right.N]))
    if 1 <= i <= N-2:
        X_right.append(ST_right.fold(0,a))

X_right.reverse()

ans = 0
for i in range(1,N-1):
    a = A[i]
    x,y = X_left[i-1]
    z,w = X_right[i-1]
    if y > 0 and w > 0:
        ans += a*(y*w)+w*x+y*z
        ans %= mod

print(ans)
0