結果

問題 No.1300 Sum of Inversions
ユーザー terasaterasa
提出日時 2022-06-02 23:42:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,917 ms / 2,000 ms
コード長 2,516 bytes
コンパイル時間 242 ms
コンパイル使用メモリ 81,804 KB
実行使用メモリ 164,800 KB
最終ジャッジ日時 2023-10-21 01:22:12
合計ジャッジ時間 51,330 ms
ジャッジサーバーID
(参考情報)
judge9 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,636 KB
testcase_01 AC 41 ms
55,636 KB
testcase_02 AC 41 ms
55,636 KB
testcase_03 AC 1,532 ms
143,376 KB
testcase_04 AC 1,620 ms
133,868 KB
testcase_05 AC 1,201 ms
124,072 KB
testcase_06 AC 1,780 ms
151,984 KB
testcase_07 AC 1,782 ms
148,544 KB
testcase_08 AC 1,877 ms
156,516 KB
testcase_09 AC 1,893 ms
156,672 KB
testcase_10 AC 1,015 ms
115,356 KB
testcase_11 AC 974 ms
116,028 KB
testcase_12 AC 1,428 ms
134,204 KB
testcase_13 AC 1,471 ms
133,000 KB
testcase_14 AC 1,917 ms
164,800 KB
testcase_15 AC 1,811 ms
155,988 KB
testcase_16 AC 1,623 ms
144,836 KB
testcase_17 AC 933 ms
115,340 KB
testcase_18 AC 1,054 ms
120,160 KB
testcase_19 AC 1,295 ms
127,484 KB
testcase_20 AC 1,337 ms
128,596 KB
testcase_21 AC 1,244 ms
128,112 KB
testcase_22 AC 1,172 ms
124,200 KB
testcase_23 AC 1,772 ms
151,876 KB
testcase_24 AC 1,151 ms
125,388 KB
testcase_25 AC 1,010 ms
118,840 KB
testcase_26 AC 989 ms
117,492 KB
testcase_27 AC 1,190 ms
123,476 KB
testcase_28 AC 1,710 ms
160,480 KB
testcase_29 AC 1,213 ms
127,680 KB
testcase_30 AC 1,767 ms
155,692 KB
testcase_31 AC 1,200 ms
124,516 KB
testcase_32 AC 1,233 ms
126,260 KB
testcase_33 AC 929 ms
121,904 KB
testcase_34 AC 962 ms
121,436 KB
testcase_35 AC 1,041 ms
135,688 KB
testcase_36 AC 1,020 ms
152,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import pypyjit
import itertools
import heapq
import math
from collections import deque, defaultdict
import bisect

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
pypyjit.set_param('max_unroll_recursion=-1')


def index_lt(a, x):
    'return largest index s.t. A[i] < x or -1 if it does not exist'
    return bisect.bisect_left(a, x) - 1


def index_le(a, x):
    'return largest index s.t. A[i] <= x or -1 if it does not exist'
    return bisect.bisect_right(a, x) - 1


def index_gt(a, x):
    'return smallest index s.t. A[i] > x or len(a) if it does not exist'
    return bisect.bisect_right(a, x)


def index_ge(a, x):
    'return smallest index s.t. A[i] >= x or len(a) if it does not exist'
    return bisect.bisect_left(a, x)


class SegTree:
    def __init__(self, N, func, e):
        self.N = N
        self.func = func
        self.X = [e] * (N << 1)
        self.e = e

    def build(self, seq):
        for i in range(self.N):
            self.X[self.N + i] = seq[i]
        for i in range(self.N)[::-1]:
            self.X[i] = self.func(self.X[i << 1], self.X[i << 1 | 1])

    def add(self, i, x):
        i += self.N
        self.X[i] += x
        while i > 1:
            i >>= 1
            self.X[i] = self.func(self.X[i << 1], self.X[i << 1 | 1])

    def update(self, i, x):
        i += self.N
        self.X[i] = x
        while i > 1:
            i >>= 1
            self.X[i] = self.func(self.X[i << 1], self.X[i << 1 | 1])

    def query(self, L, R):
        L += self.N
        R += self.N
        vL = self.e
        vR = self.e
        while L < R:
            if L & 1:
                vL = self.func(vL, self.X[L])
                L += 1
            if R & 1:
                R -= 1
                vR = self.func(self.X[R], vR)
            L >>= 1
            R >>= 1
        return self.func(vL, vR)


N = int(input())
A = list(map(int, input().split()))
mod = 998_244_353

D = defaultdict(list)
for i, a in enumerate(A):
    D[a].append(i)

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


def f(a, b):
    return ((a[0] + b[0], a[1] + b[1]))


st = SegTree(N, f, (0, 0))
ans = 0
for k in sorted(D.keys()):
    for j, i in enumerate(D[k]):
        r, sr = st.query(i, N)
        _l, _sl = st.query(0, i)
        l, sl = i - _l, S[i] - _sl
        l -= j
        sl -= k * (j + 1)
        ans += sl * r
        ans += sr * l
        ans += k * r * l
        ans %= mod
    for i in D[k]:
        st.update(i, (1, k))
print(ans)
0