結果

問題 No.1300 Sum of Inversions
ユーザー roarisroaris
提出日時 2020-11-27 23:02:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 691 ms / 2,000 ms
コード長 1,263 bytes
コンパイル時間 276 ms
コンパイル使用メモリ 87,316 KB
実行使用メモリ 182,756 KB
最終ジャッジ日時 2023-10-09 21:00:04
合計ジャッジ時間 19,665 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
71,792 KB
testcase_01 AC 83 ms
71,644 KB
testcase_02 AC 81 ms
71,868 KB
testcase_03 AC 545 ms
158,312 KB
testcase_04 AC 522 ms
158,072 KB
testcase_05 AC 452 ms
141,124 KB
testcase_06 AC 592 ms
168,516 KB
testcase_07 AC 567 ms
167,212 KB
testcase_08 AC 642 ms
173,764 KB
testcase_09 AC 623 ms
173,364 KB
testcase_10 AC 389 ms
134,820 KB
testcase_11 AC 390 ms
134,048 KB
testcase_12 AC 512 ms
157,968 KB
testcase_13 AC 516 ms
157,168 KB
testcase_14 AC 691 ms
169,208 KB
testcase_15 AC 636 ms
173,432 KB
testcase_16 AC 537 ms
158,812 KB
testcase_17 AC 391 ms
129,488 KB
testcase_18 AC 440 ms
134,324 KB
testcase_19 AC 527 ms
148,424 KB
testcase_20 AC 503 ms
148,804 KB
testcase_21 AC 474 ms
148,756 KB
testcase_22 AC 493 ms
141,016 KB
testcase_23 AC 669 ms
168,680 KB
testcase_24 AC 489 ms
141,196 KB
testcase_25 AC 408 ms
134,136 KB
testcase_26 AC 379 ms
133,612 KB
testcase_27 AC 406 ms
141,020 KB
testcase_28 AC 612 ms
182,756 KB
testcase_29 AC 487 ms
148,528 KB
testcase_30 AC 592 ms
173,640 KB
testcase_31 AC 419 ms
140,800 KB
testcase_32 AC 432 ms
141,644 KB
testcase_33 AC 245 ms
108,712 KB
testcase_34 AC 256 ms
111,864 KB
testcase_35 AC 372 ms
181,908 KB
testcase_36 AC 412 ms
173,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import *

def compress(l):
    l = list(set(l))
    l.sort()
    idx = defaultdict(int)

    for i in range(len(l)):
        idx[l[i]] = i
    
    return idx

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

    def add(self, i, x):
        i += 1
        
        while i<=self.n:
            self.bit[i] += x
            i += i&(-i)

    def acc(self, i):
        s = 0
        
        while i>0:
            s += self.bit[i]
            i -= i&(-i)
        
        return s

N = int(input())
A = list(map(int, input().split()))
idx = compress(A)
n = len(idx.keys())
upper1 = [0]*N
upper2 = [0]*N
bit1 = BIT(n)
bit2 = BIT(n)

for i in range(N):
    upper1[i] = bit1.acc(n)-bit1.acc(idx[A[i]]+1)
    bit1.add(idx[A[i]], 1)
    upper2[i] = bit2.acc(n)-bit2.acc(idx[A[i]]+1)
    bit2.add(idx[A[i]], A[i])

lower1 = [0]*N
lower2 = [0]*N
bit1 = BIT(n)
bit2 = BIT(n)

for i in range(N-1, -1, -1):
    lower1[i] = bit1.acc(idx[A[i]])
    bit1.add(idx[A[i]], 1)
    lower2[i] = bit2.acc(idx[A[i]])
    bit2.add(idx[A[i]], A[i])

ans = 0
MOD = 998244353

for i in range(N):
    ans = (ans+A[i]*upper1[i]*lower1[i]+upper1[i]*lower2[i]+upper2[i]*lower1[i])%MOD
    
print(ans)
0