結果

問題 No.2248 max(C)-min(C)
ユーザー tamatotamato
提出日時 2023-03-17 22:55:14
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 4,215 bytes
コンパイル時間 692 ms
コンパイル使用メモリ 81,728 KB
実行使用メモリ 262,876 KB
最終ジャッジ日時 2023-10-18 15:55:55
合計ジャッジ時間 66,587 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,524 KB
testcase_01 AC 39 ms
53,524 KB
testcase_02 AC 39 ms
53,524 KB
testcase_03 AC 117 ms
77,292 KB
testcase_04 AC 96 ms
76,360 KB
testcase_05 AC 84 ms
75,976 KB
testcase_06 AC 135 ms
78,016 KB
testcase_07 AC 123 ms
77,840 KB
testcase_08 AC 148 ms
78,436 KB
testcase_09 AC 133 ms
77,884 KB
testcase_10 AC 80 ms
75,688 KB
testcase_11 AC 133 ms
77,888 KB
testcase_12 AC 115 ms
77,340 KB
testcase_13 AC 140 ms
77,720 KB
testcase_14 AC 136 ms
78,028 KB
testcase_15 AC 145 ms
78,680 KB
testcase_16 AC 112 ms
77,260 KB
testcase_17 AC 125 ms
77,888 KB
testcase_18 AC 1,307 ms
250,268 KB
testcase_19 AC 231 ms
100,356 KB
testcase_20 AC 1,550 ms
250,152 KB
testcase_21 AC 1,436 ms
241,376 KB
testcase_22 AC 1,027 ms
227,468 KB
testcase_23 AC 691 ms
168,896 KB
testcase_24 AC 327 ms
107,472 KB
testcase_25 AC 1,327 ms
258,640 KB
testcase_26 AC 1,251 ms
262,484 KB
testcase_27 AC 1,361 ms
256,660 KB
testcase_28 AC 235 ms
96,568 KB
testcase_29 AC 1,237 ms
247,712 KB
testcase_30 AC 595 ms
149,692 KB
testcase_31 AC 1,521 ms
249,888 KB
testcase_32 AC 358 ms
112,892 KB
testcase_33 AC 546 ms
143,816 KB
testcase_34 AC 1,553 ms
249,888 KB
testcase_35 AC 1,513 ms
250,156 KB
testcase_36 AC 1,536 ms
249,892 KB
testcase_37 AC 796 ms
193,268 KB
testcase_38 AC 2,366 ms
252,392 KB
testcase_39 TLE -
testcase_40 AC 2,467 ms
252,716 KB
testcase_41 AC 2,863 ms
262,876 KB
testcase_42 TLE -
testcase_43 AC 2,159 ms
242,112 KB
testcase_44 AC 2,404 ms
252,660 KB
testcase_45 AC 1,896 ms
242,688 KB
testcase_46 AC 1,513 ms
164,228 KB
testcase_47 AC 2,413 ms
252,916 KB
testcase_48 AC 741 ms
227,488 KB
testcase_49 TLE -
testcase_50 TLE -
testcase_51 TLE -
testcase_52 TLE -
testcase_53 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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


class SparseTable():
    def __init__(self, A, STfunc):
        # A: 処理したい数列
        self.N = len(A)
        self.K = self.N.bit_length() - 1
        self.table = [0] * (self.N * (self.K + 1))
        self.STfunc = STfunc
        for i, a in enumerate(A):
            self.table[i] = a
        for k in range(1, self.K + 1):
            for i in range(self.N):
                j = i + (1 << (k-1))
                if j <= self.N-1:
                    self.table[i + k * self.N] = STfunc(self.table[i + (k-1) * self.N], self.table[j + (k-1) * self.N])

    def query(self, l, r):
        l -= 1
        r -= 1
        # [l, r)の最小値を求める
        k = (r-l).bit_length() - 1
        return self.STfunc(self.table[l + k * self.N], self.table[r - (1 << k) + k * self.N])


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 = []
val = set()
for a, b, ab in X:
    A.append(a)
    B.append(b)
    AB.append(ab)
    val.add(a)
    val.add(b)
    val.add(ab)

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

ans = min(max(A) - min(A), max(B) - min(B))
for v in sorted(list(val)):
    i = bisect_left(AB, v)

    if i != 0:
        mi_B = ST_B_min.query(1, i + 1)
        ma_B = ST_B_max.query(1, i + 1)
        if mi_B < v:
            continue
    else:
        ma_B = -1
        mi_B = 10 ** 10

    ok = max(ma_B, AB[-1])
    ng = min(v, ma_B) - 1
    if (ng + 1) - v >= ans:
        continue
    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 < v or ma_A > mid:
                flg = 0
        if flg:
            ok = mid
        else:
            ng = mid
        mid = (ok + ng) // 2
    ans = min(ans, ok - v)

print(ans)
0