結果

問題 No.1300 Sum of Inversions
ユーザー marroncastlemarroncastle
提出日時 2020-11-29 20:48:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 880 ms / 2,000 ms
コード長 1,381 bytes
コンパイル時間 262 ms
コンパイル使用メモリ 87,280 KB
実行使用メモリ 190,904 KB
最終ジャッジ日時 2023-10-11 02:36:55
合計ジャッジ時間 23,745 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,172 KB
testcase_01 AC 70 ms
71,448 KB
testcase_02 AC 72 ms
71,176 KB
testcase_03 AC 691 ms
146,916 KB
testcase_04 AC 675 ms
150,168 KB
testcase_05 AC 570 ms
124,772 KB
testcase_06 AC 756 ms
174,456 KB
testcase_07 AC 741 ms
161,684 KB
testcase_08 AC 817 ms
180,208 KB
testcase_09 AC 816 ms
179,664 KB
testcase_10 AC 485 ms
129,132 KB
testcase_11 AC 489 ms
127,852 KB
testcase_12 AC 677 ms
146,124 KB
testcase_13 AC 671 ms
135,432 KB
testcase_14 AC 880 ms
190,904 KB
testcase_15 AC 800 ms
179,376 KB
testcase_16 AC 695 ms
147,464 KB
testcase_17 AC 467 ms
126,532 KB
testcase_18 AC 527 ms
120,360 KB
testcase_19 AC 612 ms
129,972 KB
testcase_20 AC 629 ms
130,372 KB
testcase_21 AC 628 ms
130,468 KB
testcase_22 AC 571 ms
124,740 KB
testcase_23 AC 773 ms
173,660 KB
testcase_24 AC 590 ms
125,136 KB
testcase_25 AC 522 ms
122,300 KB
testcase_26 AC 517 ms
123,136 KB
testcase_27 AC 573 ms
124,208 KB
testcase_28 AC 836 ms
189,148 KB
testcase_29 AC 615 ms
130,000 KB
testcase_30 AC 814 ms
179,812 KB
testcase_31 AC 583 ms
124,580 KB
testcase_32 AC 592 ms
125,316 KB
testcase_33 AC 361 ms
125,104 KB
testcase_34 AC 380 ms
123,380 KB
testcase_35 AC 456 ms
153,496 KB
testcase_36 AC 489 ms
190,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# Binary Indexed Tree (Fenwick Tree) 1-indexed
class BIT:
  def __init__(self, n):
    self.n = n
    self.bit = [0]*(n+1)
    self.el = [0]*(n+1)
  def sum(self, i): #1~iまで足す
    s = 0
    while i > 0:
      s += self.bit[i]
      i -= i & -i
    return s
  def add(self, i, x):
    # assert i > 0
    self.el[i] += x
    while i <= self.n:
      self.bit[i] += x
      i += i & -i
  def get(self, i, j=None):
    if j is None:
      return self.el[i]
    return self.sum(j) - self.sum(i-1)
  def lower_bound(self,x):
    w = i = 0
    k = 1<<((self.n).bit_length())
    while k:
      if i+k <= self.n and w + self.bit[i+k] < x:
        w += self.bit[i+k]
        i += k
      k >>= 1
    return i+1
N = int(input())
total1 = BIT(N)
total2 = BIT(N)
cnt1 = BIT(N)
cnt2 = BIT(N)
A = list(map(int, input().split()))
comp = lambda arr: {e: i+1 for i, e in enumerate(sorted(set(arr)))}
compA = comp(A)
MOD = 998244353
B = A[::-1]
ans = 0
S,T = [0]*N,[0]*N
n,m = [0]*N,[0]*N
for i in range(N):
  total1.add(compA[A[i]], A[i])
  cnt1.add(compA[A[i]], 1)
  S[i] = total1.get(compA[A[i]]+1,N)%MOD
  n[i] = cnt1.get(compA[A[i]]+1,N)
for i in range(N):
  total2.add(compA[B[i]], B[i])
  cnt2.add(compA[B[i]], 1)
  T[N-1-i] = total2.sum(compA[B[i]]-1)%MOD
  m[N-1-i] = cnt2.sum(compA[B[i]]-1)
for i in range(N):
  ans += S[i]*m[i] + T[i]*n[i] + A[i]*m[i]*n[i]
  ans %= MOD
print(ans)
0