結果
問題 | No.1300 Sum of Inversions |
ユーザー | terasa |
提出日時 | 2022-06-02 23:43:55 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,654 ms / 2,000 ms |
コード長 | 2,540 bytes |
コンパイル時間 | 236 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 164,700 KB |
最終ジャッジ日時 | 2024-09-21 02:12:57 |
合計ジャッジ時間 | 41,560 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 52 ms
55,040 KB |
testcase_01 | AC | 44 ms
54,784 KB |
testcase_02 | AC | 44 ms
54,784 KB |
testcase_03 | AC | 1,321 ms
144,072 KB |
testcase_04 | AC | 1,264 ms
134,420 KB |
testcase_05 | AC | 985 ms
124,496 KB |
testcase_06 | AC | 1,433 ms
152,676 KB |
testcase_07 | AC | 1,387 ms
149,292 KB |
testcase_08 | AC | 1,522 ms
157,000 KB |
testcase_09 | AC | 1,520 ms
157,052 KB |
testcase_10 | AC | 854 ms
116,032 KB |
testcase_11 | AC | 885 ms
116,484 KB |
testcase_12 | AC | 1,289 ms
134,428 KB |
testcase_13 | AC | 1,244 ms
133,616 KB |
testcase_14 | AC | 1,654 ms
164,700 KB |
testcase_15 | AC | 1,534 ms
156,368 KB |
testcase_16 | AC | 1,329 ms
145,648 KB |
testcase_17 | AC | 785 ms
116,096 KB |
testcase_18 | AC | 923 ms
121,072 KB |
testcase_19 | AC | 1,136 ms
128,116 KB |
testcase_20 | AC | 1,121 ms
129,060 KB |
testcase_21 | AC | 1,099 ms
128,616 KB |
testcase_22 | AC | 1,033 ms
124,288 KB |
testcase_23 | AC | 1,495 ms
152,212 KB |
testcase_24 | AC | 1,035 ms
125,568 KB |
testcase_25 | AC | 909 ms
119,404 KB |
testcase_26 | AC | 902 ms
118,144 KB |
testcase_27 | AC | 1,013 ms
124,080 KB |
testcase_28 | AC | 1,631 ms
160,688 KB |
testcase_29 | AC | 1,122 ms
128,312 KB |
testcase_30 | AC | 1,545 ms
156,632 KB |
testcase_31 | AC | 1,001 ms
124,884 KB |
testcase_32 | AC | 1,053 ms
126,720 KB |
testcase_33 | AC | 793 ms
122,700 KB |
testcase_34 | AC | 818 ms
121,684 KB |
testcase_35 | AC | 886 ms
136,504 KB |
testcase_36 | AC | 871 ms
153,416 KB |
ソースコード
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)