結果

問題 No.1300 Sum of Inversions
ユーザー marroncastle
提出日時 2020-11-29 20:48:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 829 ms / 2,000 ms
コード長 1,381 bytes
コンパイル時間 313 ms
コンパイル使用メモリ 82,476 KB
実行使用メモリ 189,492 KB
最終ジャッジ日時 2024-09-13 02:08:49
合計ジャッジ時間 21,468 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

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