結果

問題 No.1496 不思議な数え上げ
ユーザー penguinmanpenguinman
提出日時 2021-04-25 07:33:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 977 ms / 3,000 ms
コード長 3,482 bytes
コンパイル時間 258 ms
コンパイル使用メモリ 82,792 KB
実行使用メモリ 122,944 KB
最終ジャッジ日時 2024-07-16 03:48:21
合計ジャッジ時間 29,944 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,664 KB
testcase_01 AC 37 ms
53,828 KB
testcase_02 AC 37 ms
54,296 KB
testcase_03 AC 751 ms
122,524 KB
testcase_04 AC 975 ms
122,804 KB
testcase_05 AC 932 ms
122,876 KB
testcase_06 AC 428 ms
122,612 KB
testcase_07 AC 443 ms
122,596 KB
testcase_08 AC 428 ms
122,592 KB
testcase_09 AC 393 ms
122,356 KB
testcase_10 AC 399 ms
122,392 KB
testcase_11 AC 392 ms
122,448 KB
testcase_12 AC 695 ms
122,540 KB
testcase_13 AC 550 ms
122,392 KB
testcase_14 AC 494 ms
122,568 KB
testcase_15 AC 582 ms
122,604 KB
testcase_16 AC 585 ms
122,932 KB
testcase_17 AC 788 ms
122,668 KB
testcase_18 AC 852 ms
122,944 KB
testcase_19 AC 806 ms
122,808 KB
testcase_20 AC 824 ms
122,656 KB
testcase_21 AC 902 ms
122,508 KB
testcase_22 AC 856 ms
122,588 KB
testcase_23 AC 894 ms
122,776 KB
testcase_24 AC 897 ms
122,888 KB
testcase_25 AC 659 ms
122,756 KB
testcase_26 AC 680 ms
122,664 KB
testcase_27 AC 977 ms
119,984 KB
testcase_28 AC 919 ms
119,732 KB
testcase_29 AC 926 ms
120,032 KB
testcase_30 AC 918 ms
119,972 KB
testcase_31 AC 931 ms
120,136 KB
testcase_32 AC 738 ms
117,000 KB
testcase_33 AC 481 ms
97,628 KB
testcase_34 AC 491 ms
97,108 KB
testcase_35 AC 154 ms
77,996 KB
testcase_36 AC 770 ms
116,488 KB
testcase_37 AC 517 ms
99,468 KB
testcase_38 AC 499 ms
98,804 KB
testcase_39 AC 692 ms
111,844 KB
testcase_40 AC 429 ms
99,112 KB
testcase_41 AC 714 ms
112,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# Refernce: https://atcoder.jp/contests/practice2/submissions/16814531
class SegTree():
    def __init__(self, a, func, idem):
        # the number of nodes is 2n - 1
        self.n = 1 << len(a).bit_length()
        self.size = len(a)
        self.func = func
        self.idem = idem # Identity element
        self.node = [idem] * (2 * self.n - 1)
        for i in range(len(a) + self.n - 2, -1, -1):
            if i >= self.n - 1:
                self.node[i] = a[i - self.n + 1]
            else:
                self.node[i] = self.func(self.node[2 * i + 1], self.node[2 * i + 2])
            
    def get(self, x):
        return self.node[x + self.n - 1]

    def set(self, x, val):
        x += self.n - 1
        self.node[x] = val
        while x > 0:
            x = (x - 1) >> 1
            self.node[x] = self.func(self.node[2*x+1], self.node[2*x+2])
        return
    
    def prod(self, l, r):
        L, R = l + self.n, r + self.n
        s_l, s_r = self.idem, self.idem
        while L < R:
            if R & 1:
                R -= 1
                s_r = self.func(self.node[R-1], s_r)
            if L & 1:
                s_l = s_l = self.func(s_l, self.node[L-1])
                L += 1
            L >>= 1
            R >>= 1
        return self.func(s_l, s_r)

    def max_right(self, l, val):
        if l == self.size:
            return self.size
        L = l + self.n
        s = self.idem
        while 1:
            while L % 2 == 0:
                L >>= 1
            if not val(self.func(s, self.node[L - 1])):
                while L < self.n:
                    L <<= 1
                    if val(self.func(s, self.node[L-1])):
                        s = self.func(s, self.node[L-1])
                        L += 1
                return L - self.n
            s = self.func(s, self.node[L - 1])
            L += 1
            if (L & -L) == L:
                return self.size

    def min_left(self, r, val):
        if r == 0:
            return 0
        R = r + self.n
        s = self.idem
        while True:
            R -= 1
            while R > 1 and R % 2:
                R >>= 1
            if not val(self.func(self.node[R - 1], s)):
                while R < self.n:
                    R = (R << 1) + 1
                    if val(self.func(self.node[R - 1], s)):
                        s = self.func(self.node[R - 1], s)
                        R -= 1
                return R + 1 - self.n
            s = self.func(self.node[R - 1], s)
            if (R & -R) == R:
                return 0

import sys
input=sys.stdin.readline
import bisect

inf = int(1e20)
N = int(input())
P = list(map(int, input().split()))
A = list(map(int, input().split()))
rev = [0]*N
dp = [0]*(N+1)

for i in range(N):
    dp[i+1] = dp[i]+P[i]
    P[i] -= 1
    rev[P[i]] = i

seg = SegTree(P, min, inf)
for i in range(N):
    ans = 0
    left = seg.min_left(rev[i], lambda z: i <= z)
    right = seg.max_right(rev[i], lambda z: i <= z)
    left = max(left-1, -1)
    right = min(right, N)
    if rev[i]-left < right-rev[i]:
        for l in range(left+1, rev[i]+1):
            r = bisect.bisect_right(dp, dp[l]+A[i])
            r -= 2
            r = max(r, rev[i]-1)
            r = min(r, right-1)
            ans += r-rev[i]+1
    else:
        for r in range(rev[i], right):
            l = bisect.bisect_left(dp, dp[r+1]-A[i])
            l = max(l, left+1)
            l = min(l, rev[i]+1)
            ans += rev[i]-l+1
    print(ans)
0