結果
問題 | No.1300 Sum of Inversions |
ユーザー | Shirotsume |
提出日時 | 2023-02-17 20:32:51 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,974 ms / 2,000 ms |
コード長 | 1,641 bytes |
コンパイル時間 | 197 ms |
コンパイル使用メモリ | 82,236 KB |
実行使用メモリ | 141,824 KB |
最終ジャッジ日時 | 2024-07-19 11:38:40 |
合計ジャッジ時間 | 51,246 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 40 ms
53,760 KB |
testcase_01 | AC | 42 ms
54,016 KB |
testcase_02 | AC | 44 ms
54,144 KB |
testcase_03 | AC | 1,478 ms
120,408 KB |
testcase_04 | AC | 1,544 ms
120,072 KB |
testcase_05 | AC | 1,238 ms
117,248 KB |
testcase_06 | AC | 1,757 ms
130,428 KB |
testcase_07 | AC | 1,693 ms
129,292 KB |
testcase_08 | AC | 1,805 ms
132,800 KB |
testcase_09 | AC | 1,805 ms
133,020 KB |
testcase_10 | AC | 1,074 ms
112,896 KB |
testcase_11 | AC | 1,195 ms
113,536 KB |
testcase_12 | AC | 1,918 ms
119,876 KB |
testcase_13 | AC | 1,512 ms
126,592 KB |
testcase_14 | AC | 1,974 ms
138,900 KB |
testcase_15 | AC | 1,865 ms
133,204 KB |
testcase_16 | AC | 1,597 ms
115,860 KB |
testcase_17 | AC | 1,039 ms
109,604 KB |
testcase_18 | AC | 1,182 ms
113,408 KB |
testcase_19 | AC | 1,362 ms
121,344 KB |
testcase_20 | AC | 1,422 ms
121,216 KB |
testcase_21 | AC | 1,354 ms
121,216 KB |
testcase_22 | AC | 1,275 ms
117,120 KB |
testcase_23 | AC | 1,821 ms
131,144 KB |
testcase_24 | AC | 1,346 ms
117,316 KB |
testcase_25 | AC | 1,149 ms
113,536 KB |
testcase_26 | AC | 1,081 ms
113,408 KB |
testcase_27 | AC | 1,214 ms
117,120 KB |
testcase_28 | AC | 1,865 ms
138,004 KB |
testcase_29 | AC | 1,396 ms
121,216 KB |
testcase_30 | AC | 1,830 ms
133,628 KB |
testcase_31 | AC | 1,241 ms
117,120 KB |
testcase_32 | AC | 1,256 ms
117,376 KB |
testcase_33 | AC | 646 ms
103,808 KB |
testcase_34 | AC | 652 ms
102,436 KB |
testcase_35 | AC | 1,155 ms
141,824 KB |
testcase_36 | AC | 1,171 ms
139,216 KB |
ソースコード
import sys from collections import deque, Counter input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) inf = 2 ** 63 - 1 mod = 998244353 class fenwick_tree(): n=1 data=[0 for i in range(n)] def __init__(self,N): self.n=N self.data=[0 for i in range(N)] def add(self,p,x): assert 0<=p<self.n,"0<=p<n,p={0},n={1}".format(p,self.n) p+=1 while(p<=self.n): self.data[p-1]+=x p += p&(-p) def sum(self,l,r): assert (0<=l and l<=r and r<=self.n),"0<=l<=r<=n,l={0},r={1},n={2}".format(l,r,self.n) return self.sum0(r)-self.sum0(l) def sum0(self,r): s=0 while(r>0): s+=self.data[r-1] r-=r&-r return s def __getitem__(self, p): if isinstance(p, int): return self.sum(p, p + 1) else: return self.sum(p.start, p.stop) def __setitem__(self, p, x): return self.add(p, x - self[p]) n = ii() a = li() sa = list(set(a)) A = [(sa[i], i) for i in range(len(sa))] A.sort() d = {} sn = len(A) for i in range(sn): d[A[i][0]] = i f1 = fenwick_tree(sn) c1 = fenwick_tree(sn) f2 = fenwick_tree(sn) c2 = fenwick_tree(sn) ans = 0 for i in range(n): f2[d[a[i]]] += a[i] c2[d[a[i]]] += 1 for i in range(n): f2[d[a[i]]] -= a[i] c2[d[a[i]]] -= 1 ans += f1[d[a[i]] + 1: sn] * c2[0:d[a[i]]] + f2[0:d[a[i]]] * c1[d[a[i]] + 1: sn] + c1[d[a[i]] + 1: sn] * c2[0:d[a[i]]] * a[i] ans %= mod f1[d[a[i]]] += a[i] c1[d[a[i]]] += 1 print(ans)