結果
問題 | No.1975 Zigzag Sequence |
ユーザー |
![]() |
提出日時 | 2022-06-11 19:05:59 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 309 ms / 2,000 ms |
コード長 | 2,002 bytes |
コンパイル時間 | 262 ms |
コンパイル使用メモリ | 82,500 KB |
実行使用メモリ | 127,024 KB |
最終ジャッジ日時 | 2024-09-22 05:57:54 |
合計ジャッジ時間 | 8,976 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 33 |
ソースコード
# a[i] a[j] a[k]# which a[i] > a[j] < a[k]# について, 2^(i+k-2)# 2^iを入れる a[i]にclass BIT:"""<Attention> 0-indexed.query ... return the sum [0 to m]sum ... return the sum [a to b]sumall ... return the sum [all]add ... 'add' number to element (be careful that it doesn't set a value.)search ... the sum version of bisect.rightoutput ... return the n-th elementlistout ... return the BIT list"""def query(self, m):res = 0while m > 0:res += self.bit[m]m -= m&(-m)return resdef sum(self, a, b):return self.query(b)-self.query(a)def sumall(self):bitlen = self.bitlen-1return self.query(bitlen)def add(self, m, x):m += 1bitlen = len(self.bit)while m <= bitlen-1:self.bit[m] += xm += m&(-m)returndef search(self, a):tmpsum = 0i = 0k = (self.bitlen-1).bit_length()while k >= 0:tmpk = 2**kif i+tmpk <= self.bitlen-1:if tmpsum+self.bit[i+tmpk] < a:tmpsum += self.bit[i+tmpk]i += tmpkk = k-1return i+1def output(self, a):return self.query(a+1)-self.query(a)def listout(self):return self.bitdef __init__(self, a):self.bitlen = aself.bit = [0]*adef compress(arr):*XS, = set(arr)XS.sort()return {cmp_e: cmp_i for cmp_i, cmp_e in enumerate(XS)}n = int(input())a = list(map(int,input().split()))a_c = compress(a)m = len(a_c)bit = BIT(m+2)#i時点で, a[i] についての 2^i の和bitA = BIT(m+2)#bitA ... a[i] > a[j] となる a[j] についての, 2^i の和bitB = BIT(m+2)#bitB ... a[i] < a[j] となる a[j] についての, 2^i の和mod = 10**9 + 7nibeki = [0] * (n+1)nibeki[0] = 1for i in range(n):nibeki[i+1] = nibeki[i] * 2 % modans = 0for i in range(n):targ = a_c[a[i]]ans += bitA.sum(0, targ) * nibeki[n-i-1] % modans %= modans += bitB.sum(targ+1, m+1) * nibeki[n-i-1] % modans %= modbit.add(targ, nibeki[i])bitA.add(targ, bit.sum(targ+1, m+1))bitB.add(targ, bit.sum(0, targ))print(ans)