結果

問題 No.1332 Range Nearest Query
ユーザー vwxyzvwxyz
提出日時 2024-08-15 05:36:34
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 6,217 bytes
コンパイル時間 284 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 223,692 KB
最終ジャッジ日時 2024-08-15 05:38:05
合計ジャッジ時間 35,131 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
55,552 KB
testcase_01 AC 45 ms
54,784 KB
testcase_02 AC 45 ms
54,912 KB
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 AC 1,240 ms
221,640 KB
testcase_07 AC 1,295 ms
221,644 KB
testcase_08 AC 1,298 ms
221,640 KB
testcase_09 AC 1,299 ms
221,640 KB
testcase_10 AC 1,253 ms
221,640 KB
testcase_11 AC 1,221 ms
221,644 KB
testcase_12 AC 1,247 ms
222,020 KB
testcase_13 AC 1,233 ms
221,768 KB
testcase_14 AC 1,243 ms
222,148 KB
testcase_15 AC 1,340 ms
222,156 KB
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 AC 695 ms
215,400 KB
testcase_27 AC 720 ms
218,604 KB
testcase_28 AC 753 ms
113,420 KB
testcase_29 AC 876 ms
114,244 KB
testcase_30 AC 934 ms
114,044 KB
testcase_31 AC 564 ms
109,412 KB
testcase_32 AC 843 ms
114,484 KB
testcase_33 AC 949 ms
114,096 KB
testcase_34 AC 697 ms
113,368 KB
testcase_35 AC 739 ms
113,880 KB
testcase_36 AC 838 ms
114,204 KB
testcase_37 AC 812 ms
113,604 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 TLE -
testcase_41 WA -
testcase_42 AC 1,970 ms
162,756 KB
testcase_43 WA -
testcase_44 TLE -
testcase_45 WA -
testcase_46 AC 1,846 ms
150,616 KB
testcase_47 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class Segment_Tree:
    def __init__(self,N,f,e,lst=None,dynamic=False,bisect_search=True):
        self.f=f
        self.e=e
        self.N=N
        self.bisect_search=bisect_search
        if self.bisect_search:
            self.le=1
            while self.le<self.N:
                self.le*=2
        else:
            self.le=self.N
        if dynamic:
            self.segment_tree=defaultdict(lambda:self.e)
        else:
            if lst==None:
                self.segment_tree=[self.e]*2*self.le
            else:
                assert len(lst)<=self.le
                self.segment_tree=[self.e]*self.le+[x for x in lst]+[self.e]*(self.le-len(lst))
                for i in range(self.le-1,0,-1):
                    self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1])

    def __getitem__(self,i):
        if type(i)==int:
            if -self.le<=i<0:
                return self.segment_tree[i+self.le*2]
            elif 0<=i<self.le:
                return self.segment_tree[i+self.le]
            else:
                raise IndexError("list index out of range")
        else:
            a,b,c=i.start,i.stop,i.step
            if a==None:
                a=self.le
            else:
                a+=self.le
            if b==None:
                b=self.le*2
            else:
                b+=self.le
            return self.segment_tree[slice(a,b,c)]

    def __setitem__(self,i,x):
        if -self.le<=i<0:
            i+=self.le*2
        elif 0<=i<self.le:
            i+=self.le
        else:
            raise IndexError("list index out of range")
        self.segment_tree[i]=x
        while i>1:
            i>>= 1
            self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1])

    def Build(self,lst):
        for i,x in enumerate(lst,self.le):
            self.segment_tree[i]=x
        for i in range(self.le-1,0,-1):
            self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1])

    def Fold(self,L=None,R=None):
        if L==None:
            L=self.le
        else:
            assert 0<=L<=self.le
            L+=self.le
        if R==None:
            R=self.le*2
        else:
            assert 0<=R<=self.le
            R+=self.le
        vL=self.e
        vR=self.e
        while L<R:
            if L&1:
                vL=self.f(vL,self.segment_tree[L])
                L+=1
            if R&1:
                R-=1
                vR=self.f(self.segment_tree[R],vR)
            L>>=1
            R>>=1
        return self.f(vL,vR)

    def Fold_Index(self,L=None,R=None):
        if L==None:
            L=self.le
        else:
            assert 0<=L<=self.le
            L+=self.le
        if R==None:
            R=self.le*2
        else:
            assert 0<=R<=self.le
            R+=self.le
        if L==R:
            return None
        x=self.Fold(L-self.le,R-self.le)
        while L<R:
            if L&1:
                if self.segment_tree[L]==x:
                    i=L
                    break
                L+=1
            if R&1:
                R-=1
                if self.segment_tree[R]==x:
                    i=R
                    break
            L>>=1
            R>>=1
        while i<self.le:
            if self.segment_tree[i]==self.segment_tree[i<<1]:
                i<<=1
            else:
                i<<=1
                i|=1
        i-=self.le
        return i

    def Bisect_Right(self,L=None,f=None):
        assert self.bisect_search
        if L==self.le:
            return self.le
        if L==None:
            L=0
        assert 0<=L<=self.le
        L+=self.le
        vl=self.e
        vr=self.e
        l,r=L,self.le*2
        while l<r:
            if l&1:
                vl=self.f(vl,self.segment_tree[l])
                l+=1
            if r&1:
                r-=1
                vr=self.f(self.segment_tree[r],vr)
            l>>=1
            r>>=1
        if f(self.f(vl,vr)):
            return self.N
        v=self.e
        while True:
            while L%2==0:
                L>>=1
            vv=self.f(v,self.segment_tree[L])
            if f(vv):
                v=vv
                L+=1
            else:
                while L<self.le:
                    L<<=1
                    vv=self.f(v,self.segment_tree[L])
                    if f(vv):
                        v=vv
                        L+=1
                return L-self.le

    def Bisect_Left(self,R=None,f=None):
        assert self.bisect_search
        if R==0:
            return 0
        if R==None:
            R=self.le
        assert 0<=R<=self.le
        R+=self.le
        vl=self.e
        vr=self.e
        l,r=self.le,R
        while l<r:
            if l&1:
                vl=self.f(vl,self.segment_tree[l])
                l+=1
            if r&1:
                r-=1
                vr=self.f(self.segment_tree[r],vr)
            l>>=1
            r>>=1
        if f(self.f(vl,vr)):
            return 0
        v=self.e
        while True:
            R-=1
            while R>1 and R%2:
                R>>=1
            vv=self.f(self.segment_tree[R],v)
            if f(vv):
                v=vv
            else:
                while R<self.le:
                    R=2*R+1
                    vv=self.f(self.segment_tree[R],v)
                    if f(vv):
                        v=vv
                        R-=1
                return R+1-self.le

    def __str__(self):
        return "["+", ".join(map(str,[self.segment_tree[i] for i in range(self.le,self.le+self.N)]))+"]"

N=int(input())
X=list(map(int,input().split()))
Q=int(input())
query0=[(X[i],-1,i) for i in range(N)]
query1=[(-X[i],-1,i) for i in range(N)]
for q in range(Q):
    l,r,x=map(int,input().split())
    l-=1
    query0.append((x,l,r,q))
    query1.append((-x,l,r,q))
inf=1<<30
ans_lst=[inf]*Q
query0.sort()
query1.sort()
for query in (query0,query1):
    ST=Segment_Tree(N,max,-inf)
    for tpl in query:
        if len(tpl)==3:
            x,_,i=tpl
            ST[i]=x
        else:
            x,l,r,q=tpl
            ans_lst[q]=min(ans_lst[q],x-ST.Fold(l,r))
print(*ans_lst,sep="\n")
0