結果

問題 No.1300 Sum of Inversions
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2020-11-27 22:07:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 704 ms / 2,000 ms
コード長 1,342 bytes
コンパイル時間 427 ms
コンパイル使用メモリ 86,864 KB
実行使用メモリ 157,220 KB
最終ジャッジ日時 2023-10-01 06:09:07
合計ジャッジ時間 19,947 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,092 KB
testcase_01 AC 73 ms
71,124 KB
testcase_02 AC 75 ms
71,144 KB
testcase_03 AC 588 ms
121,168 KB
testcase_04 AC 546 ms
121,172 KB
testcase_05 AC 470 ms
121,292 KB
testcase_06 AC 618 ms
145,492 KB
testcase_07 AC 567 ms
125,956 KB
testcase_08 AC 629 ms
148,096 KB
testcase_09 AC 623 ms
148,056 KB
testcase_10 AC 355 ms
116,860 KB
testcase_11 AC 356 ms
116,728 KB
testcase_12 AC 513 ms
121,152 KB
testcase_13 AC 495 ms
120,704 KB
testcase_14 AC 704 ms
157,220 KB
testcase_15 AC 605 ms
148,136 KB
testcase_16 AC 529 ms
121,856 KB
testcase_17 AC 357 ms
112,524 KB
testcase_18 AC 428 ms
116,852 KB
testcase_19 AC 485 ms
115,792 KB
testcase_20 AC 471 ms
116,216 KB
testcase_21 AC 463 ms
116,428 KB
testcase_22 AC 434 ms
121,416 KB
testcase_23 AC 584 ms
145,424 KB
testcase_24 AC 443 ms
111,152 KB
testcase_25 AC 379 ms
116,760 KB
testcase_26 AC 383 ms
116,584 KB
testcase_27 AC 425 ms
121,128 KB
testcase_28 AC 651 ms
155,744 KB
testcase_29 AC 476 ms
115,804 KB
testcase_30 AC 624 ms
148,316 KB
testcase_31 AC 444 ms
121,216 KB
testcase_32 AC 482 ms
111,844 KB
testcase_33 AC 315 ms
104,348 KB
testcase_34 AC 332 ms
102,864 KB
testcase_35 AC 405 ms
133,580 KB
testcase_36 AC 460 ms
156,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""

組の個数なら割と簡単だが…
1個,2個の場合で別にBITをもつ

座圧する

BITでAの位置にAを足しておく、みたいなことをする
2本目のBITで、2つのAの和を入れておく


"""

import sys
from sys import stdin

def bitadd(a,w,bit): #aにwを加える(1-origin)
    a += 1
    x = a
    while x <= (len(bit)-1):
        bit[x] += w
        x += x & (-1 * x)
 
def bitsum(a,bit): #ind 1~aまでの和を求める
    a += 1
    ret = 0
    x = a
    while x > 0:
        ret += bit[x]
        x -= x & (-1 * x)
    return ret

N = int(stdin.readline())
A = list(map(int,stdin.readline().split()))

dic = {}
lis = []
for i in A:
    if i not in dic:
        dic[i] = 0
        lis.append(i)
lis.sort()
for i in range(len(lis)):
    dic[lis[i]] = i+1

AS = [0] * (len(lis) + 2)
AK = [0] * (len(lis) + 2)

BS = [0] * (len(lis) + 2)
BK = [0] * (len(lis) + 2)

mod = 998244353
A.reverse()
ans = 0

for i in range(N):

    da = dic[A[i]]
    #答えの更新
    ans += bitsum(da-1,BS) + bitsum(da-1,BK) * A[i]
    ans %= mod

    #BSの更新
    tmp = bitsum(da-1 ,AS) + bitsum(da-1, AK) * A[i]
    bitadd(da,tmp % mod,BS)

    #BKの更新
    tmp = bitsum(da-1, AK)
    bitadd(da,tmp % mod,BK)

    #ASの更新
    bitadd(da,A[i],AS)

    #AKの更新
    bitadd(da,1,AK)

print (ans % mod)
0