結果
問題 | No.1496 不思議な数え上げ |
ユーザー | shotoyoo |
提出日時 | 2021-04-30 23:10:46 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 526 ms / 3,000 ms |
コード長 | 3,360 bytes |
コンパイル時間 | 195 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 132,448 KB |
最終ジャッジ日時 | 2024-07-19 03:00:17 |
合計ジャッジ時間 | 20,248 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 41 ms
52,224 KB |
testcase_01 | AC | 41 ms
52,864 KB |
testcase_02 | AC | 41 ms
52,736 KB |
testcase_03 | AC | 383 ms
131,352 KB |
testcase_04 | AC | 384 ms
131,220 KB |
testcase_05 | AC | 376 ms
131,348 KB |
testcase_06 | AC | 372 ms
131,220 KB |
testcase_07 | AC | 382 ms
131,348 KB |
testcase_08 | AC | 373 ms
130,840 KB |
testcase_09 | AC | 399 ms
131,480 KB |
testcase_10 | AC | 407 ms
131,224 KB |
testcase_11 | AC | 405 ms
131,348 KB |
testcase_12 | AC | 392 ms
131,352 KB |
testcase_13 | AC | 398 ms
131,228 KB |
testcase_14 | AC | 401 ms
131,220 KB |
testcase_15 | AC | 395 ms
131,224 KB |
testcase_16 | AC | 395 ms
131,204 KB |
testcase_17 | AC | 469 ms
131,352 KB |
testcase_18 | AC | 468 ms
131,228 KB |
testcase_19 | AC | 482 ms
131,480 KB |
testcase_20 | AC | 494 ms
131,220 KB |
testcase_21 | AC | 486 ms
130,964 KB |
testcase_22 | AC | 489 ms
130,972 KB |
testcase_23 | AC | 476 ms
132,448 KB |
testcase_24 | AC | 500 ms
130,840 KB |
testcase_25 | AC | 455 ms
131,096 KB |
testcase_26 | AC | 451 ms
131,476 KB |
testcase_27 | AC | 526 ms
129,432 KB |
testcase_28 | AC | 496 ms
127,900 KB |
testcase_29 | AC | 514 ms
128,280 KB |
testcase_30 | AC | 516 ms
129,436 KB |
testcase_31 | AC | 517 ms
128,408 KB |
testcase_32 | AC | 454 ms
124,116 KB |
testcase_33 | AC | 281 ms
102,016 KB |
testcase_34 | AC | 277 ms
100,992 KB |
testcase_35 | AC | 119 ms
77,184 KB |
testcase_36 | AC | 457 ms
123,420 KB |
testcase_37 | AC | 295 ms
102,784 KB |
testcase_38 | AC | 290 ms
102,528 KB |
testcase_39 | AC | 353 ms
115,944 KB |
testcase_40 | AC | 245 ms
98,432 KB |
testcase_41 | AC | 394 ms
115,460 KB |
ソースコード
import sys input = lambda : sys.stdin.readline().rstrip() sys.setrecursionlimit(2*10**5+10) write = lambda x: sys.stdout.write(x+"\n") debug = lambda x: sys.stderr.write(x+"\n") class BIT: ### BIT binary def __init__(self, n, values=None): self.bit = [0]*(n+1) self.n = n self.total = 0 if values is not None: for i,v in enumerate(values): self.add(i,v) self.total += v def check(self): l = [] prv = 0 for i in range(1,self.n+1): val = self.query(i) l.append(val-prv) prv = val print(" ".join(map(str, l))) #a1 ~ aiまでの和 O(logn) def query(self,i): res = 0 while i > 0: res += self.bit[i] # res %= M i -= i&(-i) return res def get(self,i): return self.query(i+1) - self.query(i) #ai += x(logN) def add(self,i,x): i += 1 if i==0: raise RuntimeError self.total += x while i <= self.n: self.bit[i] += x # self.bit[i] %= M i += i&(-i) def index(self, v): """a0,...,aiの和がv以上になる最小のindexを求める 存在しないとき配列サイズを返す """ if v <= 0: return 0 if self.total<v: return self.n x = 0 r = 1 while r < self.n: r = r << 1; ll = r while ll>0: if x+ll<self.n and self.bit[x+ll]<v: v -= self.bit[x+ll] x += ll ll = ll>>1 return x n = int(input()) p = list(map(lambda i: int(i), input().split())) A = list(map(int, input().split())) cum = [0] for v in p: cum.append(cum[-1]+v) index = [-1]*(n+1) for i,v in enumerate(p): index[v] = i bit = BIT(n) ans = [] for i in range(1,n+1): a = A[i-1] ind = index[i] if a==0: bit.add(ind, 1) ans.append(0) continue v = bit.query(ind) l = bit.index(v) if v==0: l = -1 r = bit.index(v+1) val = 0 if ind-l < r-ind: # if 1: for ii in range(l+1,ind+1): ok = ind+1 bad = r if cum[bad]-cum[ii]<=a: val += (bad-ind) continue if cum[ok]-cum[ii]>a: continue while abs(ok-bad)>1: mm = (ok+bad)//2 if cum[mm]-cum[ii]<=a: ok = mm else: bad = mm val += (ok-ind) else: for ii in range(ind+1, r+1): ok = ind bad = l+1 if -cum[bad]+cum[ii]<=a: val += (-bad+ind+1) continue if -cum[ok]+cum[ii]>a: continue while abs(ok-bad)>1: mm = (ok+bad)//2 if -cum[mm]+cum[ii]<=a: ok = mm else: bad = mm val += (-ok+ind+1) # j = ind+1 # val = 0 # for ii in range(l+1, ind+1): # while j+1<=r and cum[j+1]-cum[ii]<=a: # j += 1 # if cum[j]-cum[ii]<=a: # val += (j-ind) bit.add(ind, 1) ans.append(val) write("\n".join(map(str, ans)))