結果
問題 | No.1300 Sum of Inversions |
ユーザー |
![]() |
提出日時 | 2020-11-27 23:40:50 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,245 ms / 2,000 ms |
コード長 | 1,905 bytes |
コンパイル時間 | 410 ms |
コンパイル使用メモリ | 82,584 KB |
実行使用メモリ | 188,348 KB |
最終ジャッジ日時 | 2024-09-13 01:16:05 |
合計ジャッジ時間 | 30,857 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
ソースコード
import sys def I(): return int(sys.stdin.readline().rstrip()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) class SegTree: # モノイドに対して適用可能、Nが2冪でなくても良い def __init__(self,N,seg_func,unit): self.N = 1 << (N-1).bit_length() self.func = seg_func self.unit = unit self.tree = [self.unit]*(2*self.N) def set_val(self,i,x): # i番目(0-index)の値をxに変更 i += self.N self.tree[i] = x i >>= 1 while i: self.tree[i] = self.func(self.tree[i << 1],self.tree[i << 1 | 1]) i >>= 1 def fold(self,L,R): # [L,R)の区間取得 L += self.N R += self.N vL = self.unit vR = self.unit while L < R: if L & 1: vL = self.func(vL,self.tree[L]) L += 1 if R & 1: R -= 1 vR = self.func(self.tree[R],vR) L >>= 1 R >>= 1 return self.func(vL,vR) def seg_func(x,y): x0,x1 = x y0,y1 = y return ((x0+y0) % mod,(x1+y1) % mod) N = I() A = LI() mod = 998244353 AA = sorted(list(set(A))) d = {} M = len(AA) for i in range(M): d[AA[i]] = i ST_left = SegTree(N,seg_func,(0,0)) X_left = [] for i in range(N): a = d[A[i]] ST_left.set_val(a,seg_func((A[i],1),ST_left.tree[a+ST_left.N])) if 1 <= i <= N-2: X_left.append(ST_left.fold(a+1,N)) ST_right = SegTree(N,seg_func,(0,0)) X_right = [] for i in range(N-1,-1,-1): a = d[A[i]] ST_right.set_val(a,seg_func((A[i],1),ST_right.tree[a+ST_right.N])) if 1 <= i <= N-2: X_right.append(ST_right.fold(0,a)) X_right.reverse() ans = 0 for i in range(1,N-1): a = A[i] x,y = X_left[i-1] z,w = X_right[i-1] if y > 0 and w > 0: ans += a*(y*w)+w*x+y*z ans %= mod print(ans)