結果

問題 No.1965 Heavier
ユーザー AEnAEn
提出日時 2023-07-12 00:00:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,592 ms / 2,000 ms
コード長 2,571 bytes
コンパイル時間 1,008 ms
コンパイル使用メモリ 86,896 KB
実行使用メモリ 173,512 KB
最終ジャッジ日時 2023-10-11 14:54:21
合計ジャッジ時間 26,711 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,332 KB
testcase_01 AC 75 ms
71,244 KB
testcase_02 AC 75 ms
71,112 KB
testcase_03 AC 76 ms
71,364 KB
testcase_04 AC 77 ms
71,368 KB
testcase_05 AC 77 ms
71,220 KB
testcase_06 AC 1,134 ms
165,484 KB
testcase_07 AC 1,082 ms
166,344 KB
testcase_08 AC 1,040 ms
166,356 KB
testcase_09 AC 1,371 ms
173,512 KB
testcase_10 AC 1,400 ms
173,224 KB
testcase_11 AC 1,374 ms
173,476 KB
testcase_12 AC 76 ms
71,228 KB
testcase_13 AC 75 ms
71,260 KB
testcase_14 AC 76 ms
71,260 KB
testcase_15 AC 1,529 ms
164,296 KB
testcase_16 AC 1,475 ms
168,260 KB
testcase_17 AC 1,034 ms
137,544 KB
testcase_18 AC 1,592 ms
165,328 KB
testcase_19 AC 1,028 ms
164,184 KB
testcase_20 AC 1,056 ms
165,312 KB
testcase_21 AC 965 ms
157,012 KB
testcase_22 AC 923 ms
156,664 KB
testcase_23 AC 688 ms
123,340 KB
testcase_24 AC 831 ms
144,780 KB
testcase_25 AC 573 ms
168,760 KB
testcase_26 AC 585 ms
168,792 KB
testcase_27 AC 672 ms
164,032 KB
testcase_28 AC 666 ms
164,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegmentTree():
    """UnitXは単位元、fは区間で行いたい操作、initは自然数あるいは配列"""
    def __init__(self, init, unitX, f):
        self.f = f # (X, X) -> X
        self.unitX = unitX
        self.f = f
        if type(init) == int:
            self.n = init
            self.n = 1 << (self.n - 1).bit_length()
            self.X = [unitX] * (self.n * 2)
        else:
            self.n = len(init)
            self.n = 1 << (self.n - 1).bit_length()
            # len(init)が2の累乗ではない時UnitXで埋める
            self.X = [unitX] * self.n + init + [unitX] * (self.n - len(init))
            # 配列のindex1まで埋める
            for i in range(self.n-1, 0, -1):
                self.X[i] = self.f(self.X[i*2], self.X[i*2|1])
    
    def update(self, i, x):
        """0-indexedのi番目の値をxで置換"""
        # 最下段に移動
        i += self.n
        self.X[i] = x
        # 上向に更新
        i >>= 1
        while i:
            self.X[i] = self.f(self.X[i*2], self.X[i*2|1])
            i >>= 1
    
    def getvalue(self, i):
        """元の配列のindexの値を見る"""
        return self.X[i + self.n]
    
    def getrange(self, l, r):
        """区間[l, r)でのfを行った値"""
        l += self.n
        r += self.n
        al = self.unitX
        ar = self.unitX
        while l < r:
            # 左端が右子ノードであれば
            if l & 1:
                al = self.f(al, self.X[l])
                l += 1
            # 右端が右子ノードであれば
            if r & 1:
                r -= 1
                ar = self.f(self.X[r], ar)
            l >>= 1
            r >>= 1
        return self.f(al, ar)

import sys
input = sys.stdin.readline
from heapq import heappop, heappush
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

c = [[i,B[i]] for i in range(N)]
c.sort(key = lambda x:(x[1],-x[0]))
d = {c[i][0]:i for i in range(N)}
st1 = SegmentTree(N+3,-10**12,max)
st2 = SegmentTree(N+3,-10**12,max)

res,l,r = 0,0,0
seen = set()
while l<N:
    while r<N:
        id = d[r]
        n1 = st1.getrange(id,N+1)
        n2 = st2.getrange(0,id)
        if n1<A[r]+B[r] and n2<A[r]-B[r]:
            st1.update(id,A[r]+B[r])
            st2.update(id,A[r]-B[r])
            r += 1
            if r-l>1 and r not in seen:
                res += r-l-1
                seen.add(r)
        else:
            break
    id = d[l]
    st1.update(id,-10**12)
    st2.update(id,-10**12)
    l += 1
print(res)
0