結果

問題 No.2248 max(C)-min(C)
ユーザー tamatotamato
提出日時 2023-03-17 22:13:13
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,362 bytes
コンパイル時間 1,445 ms
コンパイル使用メモリ 81,600 KB
実行使用メモリ 225,308 KB
最終ジャッジ日時 2023-10-18 15:06:56
合計ジャッジ時間 43,133 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,564 KB
testcase_01 AC 38 ms
53,564 KB
testcase_02 AC 38 ms
53,564 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
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 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 1,712 ms
170,924 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 1,967 ms
170,944 KB
testcase_43 WA -
testcase_44 WA -
testcase_45 AC 1,561 ms
196,104 KB
testcase_46 WA -
testcase_47 WA -
testcase_48 AC 247 ms
183,352 KB
testcase_49 WA -
testcase_50 WA -
testcase_51 TLE -
testcase_52 WA -
testcase_53 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from bisect import bisect_left
mod = 998244353


class SegmentTree:
    def __init__(self, A, initialize=True, segfunc=min, ident=2000000000):
        self.N = len(A)
        self.LV = (self.N - 1).bit_length()
        self.N0 = 1 << self.LV
        self.segfunc = segfunc
        self.ident = ident
        if initialize:
            self.data = [self.ident] * self.N0 + A + [self.ident] * (self.N0 - self.N)
            for i in range(self.N0 - 1, 0, -1):
                self.data[i] = segfunc(self.data[i * 2], self.data[i * 2 + 1])
        else:
            self.data = [self.ident] * (self.N0 * 2)

    def update(self, i, x):
        i += self.N0 - 1
        self.data[i] = x
        for _ in range(self.LV):
            i >>= 1
            self.data[i] = self.segfunc(self.data[i * 2], self.data[i * 2 + 1])

    def get(self, i):
        return self.data[i + self.N0 - 1]

    # open interval [l, r)
    def query(self, l, r):
        l += self.N0 - 1
        r += self.N0 - 1
        ret_l = self.ident
        ret_r = self.ident
        while l < r:
            if l & 1:
                ret_l = self.segfunc(ret_l, self.data[l])
                l += 1
            if r & 1:
                ret_r = self.segfunc(self.data[r - 1], ret_r)
                r -= 1
            l >>= 1
            r >>= 1
        return self.segfunc(ret_l, ret_r)

    # return smallest i(l <= i < r) s.t. check(A[i]) == True
    def binsearch(self, l, r, check):
        if not check(self.query(l, r)):
            return r
        l += self.N0 - 1
        val = self.ident
        while True:
            if check(self.segfunc(val, self.data[l])):
                break
            if l & 1:
                val = self.segfunc(val, self.data[l])
                l += 1
            l >>= 1
        while l < self.N0:
            newval = self.segfunc(val, self.data[l * 2])
            if not check(newval):
                val = newval
                l = (l << 1) + 1
            else:
                l <<= 1
        return l - self.N0 + 1


N = int(input())
A_ori = list(map(int, input().split()))
B_ori = list(map(int, input().split()))

X = []
for a, b in zip(A_ori, B_ori):
    if a > b:
        a, b = b, a
    X.append((a, b, (a + b) // 2))
X.sort(key=lambda x: x[2])

A = []
B = []
AB = []
for a, b, ab in X:
    A.append(a)
    B.append(b)
    AB.append(ab)

ST_A_min = SegmentTree(A)
ST_A_max = SegmentTree(A, initialize=True, segfunc=max, ident=-2000000000)
ST_B_min = SegmentTree(B)
ST_B_max = SegmentTree(B, initialize=True, segfunc=max, ident=-2000000000)

prev = -1
ans = 10 ** 10
for i in range(N):
    if AB[i] == prev:
        continue

    if i != 0:
        mi_B = ST_B_min.query(1, i + 1)
        ma_B = ST_B_max.query(1, i + 1)
        if mi_B < AB[i]:
            prev = AB[i]
            continue
    else:
        ma_B = -1

    ok = max(ma_B, AB[-1])
    ng = AB[i] - 1
    mid = (ok + ng) // 2
    while ok - ng > 1:
        r = bisect_left(AB, mid + 1)
        flg = 1
        if r != N:
            mi_A = ST_A_min.query(r+1, N+1)
            ma_A = ST_A_max.query(r+1, N+1)
            if mi_A < AB[i] or ma_A > mid:
                flg = 0
        if flg:
            ok = mid
        else:
            ng = mid
        mid = (ok + ng) // 2
    ans = min(ans, ok - AB[i])

    prev = AB[i]
print(ans)
0