結果

問題 No.1965 Heavier
ユーザー lilictakalilictaka
提出日時 2022-06-03 23:27:19
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,667 bytes
コンパイル時間 1,139 ms
コンパイル使用メモリ 81,320 KB
実行使用メモリ 236,608 KB
最終ジャッジ日時 2023-10-21 02:28:54
合計ジャッジ時間 17,822 ms
ジャッジサーバーID
(参考情報)
judge11 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
55,412 KB
testcase_01 AC 44 ms
55,412 KB
testcase_02 AC 43 ms
55,412 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 44 ms
55,412 KB
testcase_06 AC 1,078 ms
234,140 KB
testcase_07 AC 1,067 ms
234,156 KB
testcase_08 AC 713 ms
236,576 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 730 ms
236,608 KB
testcase_26 AC 720 ms
236,084 KB
testcase_27 AC 433 ms
167,280 KB
testcase_28 AC 434 ms
167,256 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
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
dis = []
plus = []
for i in range(N):
    dis.append(A[i] -B[i])
    plus.append(B[i] + A[i])
dis = list(set(dis))
plus = list(set(plus))
dis.sort()
plus.sort()
disindex= defaultdict(int)
plusindex = defaultdict(int)
segdis = SegTree([-1] * len(dis),lambda x,y:max(x,y),-1)
segplus = SegTree([-1] * len(plus),lambda x,y:max(x,y),-1)
for i,v in enumerate(dis):
    disindex[v] = i
for i,v in enumerate(plus):
    plusindex[v] = i
ans = 0
for i in range(N):
    a = A[i]
    b = B[i]
    nowindex = disindex[a -b]
    l1 = segdis.query(nowindex,len(dis))
    segdis.update(nowindex,i)
    nowindex = plusindex[b+a]
    l2 = segplus.query(nowindex,len(plus))
    segplus.update(nowindex,i)
    l = max(l1,l2)
    ans += i - l -1
print(ans)
0