結果
問題 | No.1975 Zigzag Sequence |
ユーザー | shobonvip |
提出日時 | 2022-06-11 19:05:59 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 335 ms / 2,000 ms |
コード長 | 2,002 bytes |
コンパイル時間 | 349 ms |
コンパイル使用メモリ | 81,632 KB |
実行使用メモリ | 126,432 KB |
最終ジャッジ日時 | 2023-10-22 04:38:36 |
合計ジャッジ時間 | 10,553 ms |
ジャッジサーバーID (参考情報) |
judge10 / judge14 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 38 ms
53,424 KB |
testcase_01 | AC | 38 ms
53,424 KB |
testcase_02 | AC | 38 ms
53,424 KB |
testcase_03 | AC | 118 ms
76,684 KB |
testcase_04 | AC | 85 ms
76,680 KB |
testcase_05 | AC | 239 ms
93,548 KB |
testcase_06 | AC | 204 ms
93,140 KB |
testcase_07 | AC | 97 ms
76,732 KB |
testcase_08 | AC | 174 ms
85,572 KB |
testcase_09 | AC | 281 ms
106,204 KB |
testcase_10 | AC | 199 ms
89,948 KB |
testcase_11 | AC | 215 ms
92,256 KB |
testcase_12 | AC | 103 ms
77,004 KB |
testcase_13 | AC | 39 ms
53,452 KB |
testcase_14 | AC | 39 ms
53,452 KB |
testcase_15 | AC | 38 ms
53,452 KB |
testcase_16 | AC | 39 ms
53,452 KB |
testcase_17 | AC | 39 ms
53,452 KB |
testcase_18 | AC | 171 ms
91,104 KB |
testcase_19 | AC | 170 ms
91,104 KB |
testcase_20 | AC | 227 ms
92,480 KB |
testcase_21 | AC | 228 ms
92,480 KB |
testcase_22 | AC | 259 ms
126,432 KB |
testcase_23 | AC | 260 ms
126,432 KB |
testcase_24 | AC | 304 ms
120,360 KB |
testcase_25 | AC | 305 ms
120,624 KB |
testcase_26 | AC | 242 ms
91,664 KB |
testcase_27 | AC | 335 ms
111,144 KB |
testcase_28 | AC | 293 ms
120,360 KB |
testcase_29 | AC | 290 ms
120,096 KB |
testcase_30 | AC | 309 ms
118,776 KB |
testcase_31 | AC | 306 ms
118,776 KB |
testcase_32 | AC | 201 ms
91,108 KB |
testcase_33 | AC | 206 ms
91,108 KB |
testcase_34 | AC | 240 ms
95,960 KB |
testcase_35 | AC | 241 ms
95,960 KB |
ソースコード
# 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.right output ... return the n-th element listout ... return the BIT list """ def query(self, m): res = 0 while m > 0: res += self.bit[m] m -= m&(-m) return res def sum(self, a, b): return self.query(b)-self.query(a) def sumall(self): bitlen = self.bitlen-1 return self.query(bitlen) def add(self, m, x): m += 1 bitlen = len(self.bit) while m <= bitlen-1: self.bit[m] += x m += m&(-m) return def search(self, a): tmpsum = 0 i = 0 k = (self.bitlen-1).bit_length() while k >= 0: tmpk = 2**k if i+tmpk <= self.bitlen-1: if tmpsum+self.bit[i+tmpk] < a: tmpsum += self.bit[i+tmpk] i += tmpk k = k-1 return i+1 def output(self, a): return self.query(a+1)-self.query(a) def listout(self): return self.bit def __init__(self, a): self.bitlen = a self.bit = [0]*a def 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 + 7 nibeki = [0] * (n+1) nibeki[0] = 1 for i in range(n): nibeki[i+1] = nibeki[i] * 2 % mod ans = 0 for i in range(n): targ = a_c[a[i]] ans += bitA.sum(0, targ) * nibeki[n-i-1] % mod ans %= mod ans += bitB.sum(targ+1, m+1) * nibeki[n-i-1] % mod ans %= mod bit.add(targ, nibeki[i]) bitA.add(targ, bit.sum(targ+1, m+1)) bitB.add(targ, bit.sum(0, targ)) print(ans)