結果

問題 No.1496 不思議な数え上げ
ユーザー tktk_snsntktk_snsn
提出日時 2022-02-16 12:26:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,055 ms / 3,000 ms
コード長 1,839 bytes
コンパイル時間 1,752 ms
コンパイル使用メモリ 85,804 KB
実行使用メモリ 152,128 KB
最終ジャッジ日時 2023-09-11 17:09:55
合計ジャッジ時間 36,530 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,180 KB
testcase_01 AC 72 ms
70,688 KB
testcase_02 AC 72 ms
70,984 KB
testcase_03 AC 818 ms
149,644 KB
testcase_04 AC 1,022 ms
151,504 KB
testcase_05 AC 995 ms
151,736 KB
testcase_06 AC 473 ms
151,712 KB
testcase_07 AC 476 ms
151,708 KB
testcase_08 AC 470 ms
151,520 KB
testcase_09 AC 477 ms
151,760 KB
testcase_10 AC 519 ms
151,908 KB
testcase_11 AC 472 ms
151,672 KB
testcase_12 AC 753 ms
151,836 KB
testcase_13 AC 589 ms
151,852 KB
testcase_14 AC 558 ms
151,680 KB
testcase_15 AC 600 ms
151,916 KB
testcase_16 AC 618 ms
152,128 KB
testcase_17 AC 951 ms
151,484 KB
testcase_18 AC 959 ms
151,700 KB
testcase_19 AC 962 ms
151,644 KB
testcase_20 AC 900 ms
151,644 KB
testcase_21 AC 976 ms
151,452 KB
testcase_22 AC 912 ms
151,744 KB
testcase_23 AC 979 ms
151,580 KB
testcase_24 AC 1,043 ms
151,576 KB
testcase_25 AC 763 ms
151,836 KB
testcase_26 AC 761 ms
151,688 KB
testcase_27 AC 1,055 ms
148,328 KB
testcase_28 AC 1,050 ms
148,708 KB
testcase_29 AC 1,020 ms
148,616 KB
testcase_30 AC 1,035 ms
148,852 KB
testcase_31 AC 1,019 ms
148,736 KB
testcase_32 AC 841 ms
140,076 KB
testcase_33 AC 510 ms
110,564 KB
testcase_34 AC 518 ms
107,216 KB
testcase_35 AC 153 ms
78,664 KB
testcase_36 AC 848 ms
142,212 KB
testcase_37 AC 549 ms
115,700 KB
testcase_38 AC 512 ms
110,216 KB
testcase_39 AC 720 ms
125,284 KB
testcase_40 AC 447 ms
100,444 KB
testcase_41 AC 728 ms
125,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect


class FenwickTree(object):
    def __init__(self, n):
        self.n = n
        self.log = n.bit_length()
        self.data = [0] * n

    def __sum(self, r):
        s = 0
        while r > 0:
            s += self.data[r - 1]
            r -= r & -r
        return s

    def add(self, p, x):
        """ a[p] += xを行う"""
        p += 1
        while p <= self.n:
            self.data[p - 1] += x
            p += p & -p

    def sum(self, l, r):
        """a[l] + a[l+1] + .. + a[r-1]を返す"""
        return self.__sum(r) - self.__sum(l)

    def lower_bound(self, x):
        """a[0] + a[1] + .. a[i] >= x となる最小のiを返す"""
        if x <= 0:
            return -1
        i = 0
        k = 1 << self.log
        while k:
            if i + k <= self.n and self.data[i + k - 1] < x:
                x -= self.data[i + k - 1]
                i += k
            k >>= 1
        return i

    def __repr__(self):
        res = [self.sum(i, i+1) for i in range(self.n)]
        return " ".join(map(str, res))


N = int(input())
P = list(map(int, input().split()))
A = list(map(int, input().split()))

S = [0] * (N + 1)
for i, p in enumerate(P):
    S[i+1] += S[i] + p

pos = {p: i for i, p in enumerate(P, start=1)}
bit = FenwickTree(N+2)
bit.add(0, 1)
bit.add(N+1, 1)

for i, a in enumerate(A, start=1):
    p = pos[i]
    tot = bit.sum(0, p)
    Lcnt = p - bit.lower_bound(tot)
    Rcnt = bit.lower_bound(tot + 1) - p
    bit.add(p, 1)

    ans = 0
    if Lcnt < Rcnt:
        for L in range(p - Lcnt, p):
            R = bisect.bisect_right(S, S[L] + a)
            R = min(R, p + Rcnt)
            ans += max(0, R - p)
    else:
        for R in range(p, p + Rcnt):
            L = bisect.bisect_left(S, S[R] - a)
            L = max(L, p - Lcnt)
            ans += max(0, p - L)
    print(ans)
0