結果
問題 | No.1300 Sum of Inversions |
ユーザー | Coki628 |
提出日時 | 2020-11-27 23:37:55 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,025 ms / 2,000 ms |
コード長 | 2,528 bytes |
コンパイル時間 | 335 ms |
コンパイル使用メモリ | 82,892 KB |
実行使用メモリ | 211,776 KB |
最終ジャッジ日時 | 2024-09-13 01:12:39 |
合計ジャッジ時間 | 25,554 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 40 ms
53,500 KB |
testcase_01 | AC | 41 ms
54,208 KB |
testcase_02 | AC | 40 ms
53,828 KB |
testcase_03 | AC | 778 ms
142,256 KB |
testcase_04 | AC | 746 ms
142,252 KB |
testcase_05 | AC | 634 ms
127,208 KB |
testcase_06 | AC | 877 ms
173,564 KB |
testcase_07 | AC | 796 ms
149,996 KB |
testcase_08 | AC | 906 ms
179,904 KB |
testcase_09 | AC | 916 ms
180,040 KB |
testcase_10 | AC | 555 ms
120,936 KB |
testcase_11 | AC | 526 ms
119,872 KB |
testcase_12 | AC | 743 ms
142,076 KB |
testcase_13 | AC | 742 ms
143,032 KB |
testcase_14 | AC | 1,025 ms
211,776 KB |
testcase_15 | AC | 934 ms
180,056 KB |
testcase_16 | AC | 795 ms
142,944 KB |
testcase_17 | AC | 511 ms
119,076 KB |
testcase_18 | AC | 576 ms
122,228 KB |
testcase_19 | AC | 685 ms
136,144 KB |
testcase_20 | AC | 698 ms
136,724 KB |
testcase_21 | AC | 687 ms
136,768 KB |
testcase_22 | AC | 615 ms
127,260 KB |
testcase_23 | AC | 831 ms
173,536 KB |
testcase_24 | AC | 613 ms
127,140 KB |
testcase_25 | AC | 567 ms
120,860 KB |
testcase_26 | AC | 545 ms
119,244 KB |
testcase_27 | AC | 592 ms
126,792 KB |
testcase_28 | AC | 964 ms
210,612 KB |
testcase_29 | AC | 680 ms
136,480 KB |
testcase_30 | AC | 950 ms
180,312 KB |
testcase_31 | AC | 624 ms
127,264 KB |
testcase_32 | AC | 645 ms
127,636 KB |
testcase_33 | AC | 312 ms
104,308 KB |
testcase_34 | AC | 318 ms
100,304 KB |
testcase_35 | AC | 523 ms
162,600 KB |
testcase_36 | AC | 628 ms
211,032 KB |
ソースコード
import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for k in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for k in range(c)] for k in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10**9) INF = 10**19 MOD = 998244353 EPS = 10**-10 class BIT: """ Binary Indexed Tree (MOD付) """ def __init__(self, n): self.n = n # 0-indexed n += 1 nv = 1 while nv < n: nv *= 2 self.size = nv self.tree = [0] * nv def sum(self, i): """ [0, i]を合計する """ s = 0 i += 1 while i > 0: s += self.tree[i-1] s %= MOD i -= i & -i return s def add(self, i, x): """ 値の追加:添字i, 値x """ i += 1 while i <= self.size: self.tree[i-1] += x self.tree[i-1] %= MOD i += i & -i def query(self, l, r): """ 区間和の取得 [l, r) """ return (self.sum(r-1) - self.sum(l-1)) % MOD def get(self, i): """ 一点取得 """ return self.query(i, i+1) def update(self, i, x): """ 値の更新:添字i, 値x """ self.add(i, x - self.get(i)) def print(self): for i in range(self.n): print(self.get(i), end=' ') print() def compress(S): """ 座標圧縮 """ zipped, unzipped = {}, {} for i, a in enumerate(sorted(S)): zipped[a] = i unzipped[i] = a return zipped, unzipped N = INT() A = LIST() zp, unzp = compress(set(A)) MX = len(zp) bitcntl = BIT(MX) bitcntr = BIT(MX) bitsml = BIT(MX) bitsmr = BIT(MX) for i in range(N): bitcntr.add(zp[A[i]], 1) bitsmr.add(zp[A[i]], A[i]) ans = 0 for i in range(N): cntl = bitcntl.query(zp[A[i]]+1, MX) cntr = bitcntr.query(0, zp[A[i]]) sml = bitsml.query(zp[A[i]]+1, MX) smr = bitsmr.query(0, zp[A[i]]) ans += sml*cntr + cntl*smr + A[i]*(cntl*cntr) ans %= MOD bitcntl.add(zp[A[i]], 1) bitcntr.add(zp[A[i]], -1) bitsml.add(zp[A[i]], A[i]) bitsmr.add(zp[A[i]], -A[i]) print(ans)