class Segment_Tree(): def __init__(self, L, calc, unit): """ calc を演算とするリスト L の Segment Tree を作成 calc: 演算 (2変数関数, Monoid) unit: Monoid calc の単位元 (xe=ex=xを満たすe) """ self.calc=calc self.unit=unit N=len(L); self.n=N d=max(1,(N-1).bit_length()) k=1<1: m>>=1 data[m]=calc(data[m<<1], data[m<<1|1]) def product(self, l, r, left_closed=True,right_closed=True): L=l+self.N+(not left_closed) R=r+self.N+(right_closed) vL=self.unit vR=self.unit data=self.data; calc=self.calc while L>=1 R>>=1 return calc(vL,vR) def all_product(self): return self.data[1] def max_right(self, left, cond): """ 以下の2つをともに満たす x の1つを返す.\n (1) r=left or cond(data[left]*data[left+1]*...*data[r-1]): True (2) r=N or cond(data[left]*data[left+1]*...*data[r]): False ※ cond が単調減少の時, cond(data[left]*...*data[r-1]) を満たす最大の r となる. cond:関数(引数が同じならば結果も同じ) cond(unit): True 0<=left<=N """ assert 0<=left<=self.N,"添字が範囲外" assert cond(self.unit),"単位元が条件を満たさない." if left==self.N: return self.N left+=self.N sm=self.unit calc=self.calc; data=self.data first=True while first or (left & (-left))!=left: first=False while left%2==0: left>>=1 if not cond(calc(sm, data[left])): while left1 and right&1: right>>=1 if not cond(calc(data[right], sm)): while rightlen(self.list)*self.REBUILD_RATIO: self.__build() def discard(self, x): if self.N==0: return False A=self.__find_bucket(x) i=bisect_left(A, x) if not(i!=len(A) and A[i]==x): return False # x が存在しないので... A.pop(i) self.N-=1 if len(A)==0: self.__build() return True def remove(self, x): if not self.discard(x): raise KeyError(x) #=== get, pop def __getitem__(self, index): if index<0: index+=self.N if index<0: raise IndexError("index out of range") for A in self.list: if index=value: return A[bisect_left(A,value)] else: for A in self.list: if A[-1]>value: return A[bisect_right(A,value)] #=== count def less_count(self, value, equal=False): """ a < value となる S の元 a の個数を求める. equal=True ならば, a < value が a <= value になる. """ count=0 if equal: for A in self.list: if A[-1]>value: return count+bisect_right(A, value) count+=len(A) else: for A in self.list: if A[-1]>=value: return count+bisect_left(A, value) count+=len(A) return count def more_count(self, value, equal=False): """ a > value となる S の元 a の個数を求める. equal=True ならば, a > value が a >= value になる. """ return self.N-self.less_count(value, not equal) #=== def is_upper_bound(self, x, equal=True): if self.N: a=self.list[-1][-1] return (avalue: i=bisect_left(A, value) if A[i]==value: return index+i else: raise ValueError("{} is not in Multiset".format(value)) index+=len(A) raise ValueError("{} is not in Multiset".format(value)) #================================================== def solve(): N=int(input()) A=list(map(int,input().split())) B=list(map(int,input().split())) D=[(A[i]+B[i])//2 for i in range(N)] for i in range(N): A[i],B[i]=min(A[i],B[i]), max(A[i],B[i]) S=Segment_Tree([(B[i],i) for i in range(N)], max, (-1, 0)) E=Sorted_Multiset() for i in range(N): E.add(B[i]) remain=[2]*N ans=E.get_max()-E.get_min() while True: x,i=S.all_product() if remain[i]==2: S.update(i,(D[i],i)) E.remove(B[i]) E.add(D[i]) elif remain[i]==1: S.update(i,(A[i],i)) E.remove(D[i]) E.add(A[i]) else: break remain[i]-=1 ans=min(ans, E.get_max()-E.get_min()) return ans #================================================== print(solve())