結果

問題 No.1965 Heavier
ユーザー lilictakalilictaka
提出日時 2022-06-06 00:59:36
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,794 bytes
コンパイル時間 247 ms
コンパイル使用メモリ 81,616 KB
実行使用メモリ 259,744 KB
最終ジャッジ日時 2023-10-21 03:23:36
合計ジャッジ時間 15,835 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
55,456 KB
testcase_01 AC 40 ms
55,456 KB
testcase_02 AC 39 ms
55,456 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 39 ms
55,456 KB
testcase_06 AC 898 ms
258,952 KB
testcase_07 AC 874 ms
259,744 KB
testcase_08 AC 595 ms
255,572 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 606 ms
256,832 KB
testcase_26 AC 602 ms
256,736 KB
testcase_27 AC 485 ms
209,784 KB
testcase_28 AC 488 ms
209,788 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
for i in range(N):
    l1 = seg1.query(zi1[i],N)
    l2 = seg2.query(zi2[i],N)
    l = max(l1,l2)
    if l == -1:
        ans += i 
    else:
        ans += i -l -1
    seg1.update(zi1[i],i)
    seg2.update(zi2[i],i)
print(ans)
0