結果

問題 No.1300 Sum of Inversions
ユーザー terasaterasa
提出日時 2022-06-02 23:43:55
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,540 bytes
コンパイル時間 356 ms
コンパイル使用メモリ 81,612 KB
実行使用メモリ 164,632 KB
最終ジャッジ日時 2023-10-21 01:23:06
合計ジャッジ時間 53,043 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
55,636 KB
testcase_01 AC 45 ms
55,636 KB
testcase_02 AC 45 ms
55,636 KB
testcase_03 AC 1,637 ms
143,408 KB
testcase_04 AC 1,617 ms
133,876 KB
testcase_05 AC 1,305 ms
124,072 KB
testcase_06 AC 1,891 ms
151,996 KB
testcase_07 AC 1,856 ms
148,548 KB
testcase_08 TLE -
testcase_09 AC 1,998 ms
156,732 KB
testcase_10 AC 1,062 ms
115,360 KB
testcase_11 AC 1,059 ms
116,032 KB
testcase_12 AC 1,588 ms
134,208 KB
testcase_13 AC 1,557 ms
133,008 KB
testcase_14 TLE -
testcase_15 AC 1,981 ms
156,276 KB
testcase_16 AC 1,685 ms
144,840 KB
testcase_17 AC 1,038 ms
115,356 KB
testcase_18 AC 1,149 ms
120,164 KB
testcase_19 AC 1,386 ms
127,500 KB
testcase_20 AC 1,470 ms
128,612 KB
testcase_21 AC 1,432 ms
128,116 KB
testcase_22 AC 1,295 ms
124,196 KB
testcase_23 AC 1,912 ms
151,884 KB
testcase_24 AC 1,332 ms
125,396 KB
testcase_25 AC 1,168 ms
118,848 KB
testcase_26 AC 1,134 ms
118,040 KB
testcase_27 AC 1,283 ms
123,496 KB
testcase_28 TLE -
testcase_29 AC 1,416 ms
127,696 KB
testcase_30 TLE -
testcase_31 AC 1,295 ms
124,428 KB
testcase_32 AC 1,377 ms
126,276 KB
testcase_33 AC 1,097 ms
121,904 KB
testcase_34 AC 1,106 ms
121,436 KB
testcase_35 AC 1,146 ms
135,684 KB
testcase_36 AC 1,142 ms
152,708 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 % mod
        ans += sr * l % mod
        ans += k * r % mod * l % mod
        ans %= mod
    for i in D[k]:
        st.update(i, (1, k))
print(ans)
0