結果

問題 No.1965 Heavier
ユーザー lilictakalilictaka
提出日時 2022-06-06 01:06:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 977 ms / 2,000 ms
コード長 2,825 bytes
コンパイル時間 308 ms
コンパイル使用メモリ 81,672 KB
実行使用メモリ 259,956 KB
最終ジャッジ日時 2023-10-21 03:23:55
合計ジャッジ時間 16,833 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,516 KB
testcase_01 AC 43 ms
55,516 KB
testcase_02 AC 44 ms
55,516 KB
testcase_03 AC 44 ms
55,516 KB
testcase_04 AC 44 ms
55,516 KB
testcase_05 AC 45 ms
55,516 KB
testcase_06 AC 977 ms
259,956 KB
testcase_07 AC 947 ms
259,864 KB
testcase_08 AC 660 ms
256,088 KB
testcase_09 AC 738 ms
259,604 KB
testcase_10 AC 735 ms
259,600 KB
testcase_11 AC 730 ms
259,600 KB
testcase_12 AC 43 ms
55,544 KB
testcase_13 AC 44 ms
55,544 KB
testcase_14 AC 44 ms
55,544 KB
testcase_15 AC 745 ms
256,844 KB
testcase_16 AC 751 ms
256,840 KB
testcase_17 AC 576 ms
210,960 KB
testcase_18 AC 776 ms
256,928 KB
testcase_19 AC 758 ms
256,228 KB
testcase_20 AC 791 ms
256,824 KB
testcase_21 AC 753 ms
238,080 KB
testcase_22 AC 737 ms
237,552 KB
testcase_23 AC 569 ms
193,020 KB
testcase_24 AC 667 ms
194,880 KB
testcase_25 AC 664 ms
256,940 KB
testcase_26 AC 659 ms
256,844 KB
testcase_27 AC 532 ms
209,912 KB
testcase_28 AC 530 ms
209,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegTree:
    """
    init(init_val, ide_ele): 配列init_valで初期化 O(N)
    update(k, x): k番目の値をxに更新 O(logN)
    query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
    """
    def __init__(self, init_val, segfunc, ide_ele):
        """
        init_val: 配列の初期値
        segfunc: 区間にしたい操作
        ide_ele: 単位元
        n: 要素数
        num: n以上の最小の2のべき乗
        tree: セグメント木(1-index)
        """
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num
        # 配列の値を葉にセット
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        """
        k番目の値をxに更新
        k: index(0-index)
        x: update value
        """
        k += self.num
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
            k >>= 1

    def query(self, l, r):
        """
        [l, r)のsegfuncしたものを得る
        l: index(0-index)
        r: index(0-index)
        """
        res = self.ide_ele

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.tree[r - 1])
            l >>= 1
            r >>= 1
        return res
    def value(self,index):
        return self.query(index,index+1)
from collections import defaultdict
def compress(A):
    tmp = defaultdict(int)
    B = list(set(A))
    B.sort()
    zipped = [] #i番目のAの要素は何番目に大きいか
    unzipped = [] # i番目に大きいAの要素は何か
    for index,b in enumerate(B):
        tmp[b] = index
        unzipped.append(b)
    for a in A:
        zipped.append(tmp[a])
    return zipped,unzipped
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
memo1 = []
memo2 = []
for i in range(N):
    memo1.append(A[i] + B[i])
for i in range(N):
    memo2.append(A[i]-B[i])
seg1 = SegTree([-1] * N,lambda x,y:max(x,y),-1)
seg2 = SegTree([-1] * N,lambda x,y:max(x,y),-1)
zi1,unzi1 = compress(memo1)
zi2,unzi2 = compress(memo2)
ans = 0
lastl = -1
for i in range(N):
    l1 = seg1.query(zi1[i],N)
    l2 = seg2.query(zi2[i],N)
    l = max(l1,l2,lastl)
    if l == -1:
        ans += i 
    else:
        ans += i -l -1
    lastl = l
    seg1.update(zi1[i],i)
    seg2.update(zi2[i],i)
print(ans)
0