結果

問題 No.1300 Sum of Inversions
ユーザー marroncastlemarroncastle
提出日時 2020-11-27 22:56:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 906 ms / 2,000 ms
コード長 1,324 bytes
コンパイル時間 405 ms
コンパイル使用メモリ 87,192 KB
実行使用メモリ 190,876 KB
最終ジャッジ日時 2023-10-09 20:56:44
合計ジャッジ時間 24,032 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,336 KB
testcase_01 AC 73 ms
71,528 KB
testcase_02 AC 73 ms
71,340 KB
testcase_03 AC 719 ms
148,936 KB
testcase_04 AC 701 ms
146,168 KB
testcase_05 AC 583 ms
128,860 KB
testcase_06 AC 801 ms
175,764 KB
testcase_07 AC 770 ms
155,636 KB
testcase_08 AC 852 ms
180,668 KB
testcase_09 AC 847 ms
181,048 KB
testcase_10 AC 489 ms
132,264 KB
testcase_11 AC 503 ms
131,120 KB
testcase_12 AC 708 ms
146,756 KB
testcase_13 AC 682 ms
139,768 KB
testcase_14 AC 906 ms
190,876 KB
testcase_15 AC 838 ms
180,672 KB
testcase_16 AC 713 ms
149,416 KB
testcase_17 AC 478 ms
129,700 KB
testcase_18 AC 540 ms
124,808 KB
testcase_19 AC 617 ms
134,092 KB
testcase_20 AC 629 ms
134,684 KB
testcase_21 AC 635 ms
134,916 KB
testcase_22 AC 575 ms
128,840 KB
testcase_23 AC 798 ms
175,648 KB
testcase_24 AC 580 ms
128,960 KB
testcase_25 AC 513 ms
126,120 KB
testcase_26 AC 500 ms
126,580 KB
testcase_27 AC 564 ms
128,276 KB
testcase_28 AC 853 ms
189,156 KB
testcase_29 AC 627 ms
134,576 KB
testcase_30 AC 835 ms
181,224 KB
testcase_31 AC 572 ms
128,656 KB
testcase_32 AC 591 ms
129,372 KB
testcase_33 AC 324 ms
125,040 KB
testcase_34 AC 340 ms
123,836 KB
testcase_35 AC 465 ms
159,872 KB
testcase_36 AC 496 ms
190,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# Binary Indexed Tree (Fenwick Tree)
class BIT:
  def __init__(self, n):
    self.n = n
    self.bit = [0]*(n+1)
    self.el = [0]*(n+1)
  def sum(self, 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+1)
total2 = BIT(N+1)
total3 = BIT(N+1)
cnt1 = BIT(N+1)
cnt2 = BIT(N+1)
cnt3 = BIT(N+1)
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
A = A[::-1]
for i in range(N):
  total1.add(compA[A[i]], A[i])
  cnt1.add(compA[A[i]], 1)
  p = total1.sum(compA[A[i]]-1)
  q = cnt1.sum(compA[A[i]]-1)
  total2.add(compA[A[i]], (p+q*A[i])%MOD)
  cnt2.add(compA[A[i]], q%MOD)
  p = total2.sum(compA[A[i]]-1)
  q = cnt2.sum(compA[A[i]]-1)
  total3.add(compA[A[i]], (p+q*A[i])%MOD)
  cnt3.add(compA[A[i]], q%MOD)
print(total3.sum(N+1)%MOD)
0