結果

問題 No.1496 不思議な数え上げ
ユーザー shotoyooshotoyoo
提出日時 2021-04-30 22:58:14
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,417 bytes
コンパイル時間 1,139 ms
コンパイル使用メモリ 86,736 KB
実行使用メモリ 136,236 KB
最終ジャッジ日時 2023-09-26 07:36:44
合計ジャッジ時間 8,176 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
75,548 KB
testcase_01 AC 75 ms
71,168 KB
testcase_02 AC 78 ms
71,108 KB
testcase_03 AC 423 ms
133,692 KB
testcase_04 AC 423 ms
133,988 KB
testcase_05 AC 416 ms
133,728 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
#     print(i,ind,l,r,a)
    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)))
0