結果

問題 No.1965 Heavier
ユーザー AEnAEn
提出日時 2022-12-23 22:51:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,912 ms / 2,000 ms
コード長 2,559 bytes
コンパイル時間 817 ms
コンパイル使用メモリ 82,332 KB
実行使用メモリ 171,916 KB
最終ジャッジ日時 2024-04-29 04:36:33
合計ジャッジ時間 27,825 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,480 KB
testcase_01 AC 38 ms
52,864 KB
testcase_02 AC 38 ms
52,352 KB
testcase_03 AC 38 ms
52,992 KB
testcase_04 AC 37 ms
52,992 KB
testcase_05 AC 37 ms
52,480 KB
testcase_06 AC 1,167 ms
170,316 KB
testcase_07 AC 1,157 ms
170,704 KB
testcase_08 AC 1,161 ms
163,636 KB
testcase_09 AC 1,605 ms
171,916 KB
testcase_10 AC 1,656 ms
171,660 KB
testcase_11 AC 1,607 ms
169,920 KB
testcase_12 AC 39 ms
52,864 KB
testcase_13 AC 37 ms
52,992 KB
testcase_14 AC 38 ms
52,992 KB
testcase_15 AC 1,594 ms
170,432 KB
testcase_16 AC 1,628 ms
170,048 KB
testcase_17 AC 1,170 ms
141,000 KB
testcase_18 AC 1,912 ms
170,108 KB
testcase_19 AC 1,117 ms
168,520 KB
testcase_20 AC 1,261 ms
169,692 KB
testcase_21 AC 1,119 ms
156,308 KB
testcase_22 AC 1,062 ms
155,596 KB
testcase_23 AC 846 ms
131,488 KB
testcase_24 AC 933 ms
147,764 KB
testcase_25 AC 694 ms
169,684 KB
testcase_26 AC 705 ms
170,556 KB
testcase_27 AC 731 ms
169,288 KB
testcase_28 AC 728 ms
168,904 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)

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,-float('inf'),max)
st2 = SegmentTree(N+3,-float('inf'),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,-float('inf'))
    st2.update(id,-float('inf'))
    l += 1
print(res)

0