結果

問題 No.1300 Sum of Inversions
ユーザー とりゐとりゐ
提出日時 2022-11-10 00:39:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,306 ms / 2,000 ms
コード長 1,742 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 86,932 KB
実行使用メモリ 137,220 KB
最終ジャッジ日時 2023-09-30 11:22:40
合計ジャッジ時間 38,015 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,528 KB
testcase_01 AC 75 ms
71,368 KB
testcase_02 AC 75 ms
71,512 KB
testcase_03 AC 1,154 ms
111,608 KB
testcase_04 AC 1,001 ms
111,604 KB
testcase_05 AC 856 ms
104,296 KB
testcase_06 AC 1,169 ms
123,044 KB
testcase_07 AC 1,148 ms
122,016 KB
testcase_08 AC 1,275 ms
123,872 KB
testcase_09 AC 1,258 ms
123,760 KB
testcase_10 AC 734 ms
101,152 KB
testcase_11 AC 717 ms
100,996 KB
testcase_12 AC 1,048 ms
111,012 KB
testcase_13 AC 1,046 ms
111,056 KB
testcase_14 AC 1,306 ms
127,204 KB
testcase_15 AC 1,218 ms
123,920 KB
testcase_16 AC 1,079 ms
112,888 KB
testcase_17 AC 729 ms
99,040 KB
testcase_18 AC 788 ms
101,780 KB
testcase_19 AC 931 ms
111,372 KB
testcase_20 AC 962 ms
111,428 KB
testcase_21 AC 955 ms
111,512 KB
testcase_22 AC 877 ms
104,300 KB
testcase_23 AC 1,154 ms
123,232 KB
testcase_24 AC 869 ms
105,008 KB
testcase_25 AC 801 ms
101,460 KB
testcase_26 AC 796 ms
101,608 KB
testcase_27 AC 833 ms
104,168 KB
testcase_28 AC 1,293 ms
125,920 KB
testcase_29 AC 938 ms
111,232 KB
testcase_30 AC 1,187 ms
124,072 KB
testcase_31 AC 860 ms
104,456 KB
testcase_32 AC 910 ms
104,908 KB
testcase_33 AC 821 ms
119,320 KB
testcase_34 AC 846 ms
137,220 KB
testcase_35 AC 814 ms
115,872 KB
testcase_36 AC 820 ms
125,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class segtree():
  def __init__(self,init,func,ide):
    self.n=len(init)
    self.func=func
    self.ide=ide
    self.size=1<<(self.n-1).bit_length()
    self.tree=[self.ide for i in range(2*self.size)]
    for i in range(self.n):
      self.tree[self.size+i]=init[i]
    for i in range(self.size-1,0,-1):
      self.tree[i]=self.func(self.tree[2*i], self.tree[2*i|1])
  
  def update(self,k,x):
    k+=self.size
    self.tree[k]=x
    k>>=1
    while k:
      self.tree[k]=self.func(self.tree[2*k],self.tree[k*2|1])
      k>>=1
  
  def get(self,i):
    return self.tree[i+self.size]
  
  def query(self,l,r):
    l+=self.size
    r+=self.size
    l_res=self.ide
    r_res=self.ide
    while l<r:
      if l&1:
        l_res=self.func(l_res,self.tree[l])
        l+=1
      if r&1:
        r-=1
        r_res=self.func(self.tree[r],r_res)
      l>>=1
      r>>=1
    return self.func(l_res,r_res)
  
  def debug(self,s=10):
    print([self.get(i) for i in range(min(self.n,s))])

from sys import stdin
input=lambda :stdin.readline()[:-1]

n=int(input())
a=list(map(int,input().split()))

mod=998244353

right1=[0]*n
right2=[0]*n
seg1=segtree([0]*n,lambda x,y:x+y,0)
seg2=segtree([0]*n,lambda x,y:(x+y)%mod,0)

c=[(a[i],i) for i in range(n)]
c.sort(key=lambda x:x[0])
for ai,i in c:
  right1[i]=seg1.query(i,n)
  right2[i]=seg2.query(i,n)
  seg1.update(i,1)
  seg2.update(i,ai)

left1=[0]*n
left2=[0]*n
seg1=segtree([0]*n,lambda x,y:x+y,0)
seg2=segtree([0]*n,lambda x,y:(x+y)%mod,0)

c=c[::-1]
for ai,i in c:
  left1[i]=seg1.query(0,i)
  left2[i]=seg2.query(0,i)
  seg1.update(i,1)
  seg2.update(i,ai)

ans=0
for i in range(n):
  ans+=left1[i]*right2[i]+left2[i]*right1[i]
  ans%=mod
  ans+=a[i]*left1[i]%mod*right1[i]
  ans%=mod

print(ans)
0