提出 #47799152


ソースコード 拡げる

#ARC155B Abs Abs Function

#Segment Tree: O(logN)
class SegmentTree:
    def __init__(self,n,identity_e,combine_f):
        self._n=n;self._size=2  #size=1だとbisectが不便
        while self._size<self._n:self._size<<=1
        self._identity_e=identity_e;self._combine_f=combine_f;self._node=[self._identity_e]*2*self._size
    def build(self,array):
        assert len(array)==self._n,'array too large'
        for i,v in enumerate(array,start=self._size):self._node[i]=v
        for i in range(self._size-1,0,-1):self._node[i]=self._combine_f(self._node[i<<1|0],self._node[i<<1|1])
    def update(self,index,value):  #一点更新
        i=self._size+index;self._node[i]=value
        while i-1:i>>=1;self._node[i]=self._combine_f(self._node[i<<1|0],self._node[i<<1|1])
    def fold(self,L,R):  #区間取得: [L,R)の区間値を得る
        L+=self._size;R+=self._size;vL,vR=[self._identity_e]*2
        while L<R:
            if L&1:vL=self._combine_f(vL,self._node[L]);L+=1
            if R&1:R-=1;vR=self._combine_f(self._node[R],vR)
            L>>=1;R>>=1
        return self._combine_f(vL,vR)
    #down: Falseなら単調増加、Trueなら単調減少を仮定する。
    #[0:Lt]の作用値がX以上/以下 となる、最小のLtを返す。閉区間なので扱い注意。
    def bisect(self,X,down=False):
        now=1; cnt=self._identity_e
        while now<self._size:
            Lt,Rt=self._node[now<<1|0],self._node[now<<1|1]
            if not down and self._combine_f(cnt,Lt)>=X: now=now<<1|0
            elif   down and self._combine_f(cnt,Lt)<=X: now=now<<1|0
            else: cnt=self._combine_f(cnt,Lt); now=now<<1|1
        return now-self._size


#入力受取
Q,A,B=map(int,input().split()); Query=[tuple(map(int,input().split())) for _ in range(Q)]

#事前に検索候補の値をすべて取得し、座標圧縮しておく
P=set([A-B,A+B])
for t,a,b in Query:
    if t==1: P.add(a-b); P.add(a+b)
    if t==2: P.add(a); P.add(b+1)
P=sorted(P); D={j:i for i,j in enumerate(P)}

#区間最大値を取得するSegTreeを建てる。
#ST[i]: P[i] if (P[i]を追加した後) else -INF
ST=SegmentTree(len(P),-10**18,max)
ST.update(D[A-B],A-B); ST.update(D[A+B],A+B)
#クエリを実行
for t,a,b in Query:
    if t==1:
        ST.update(D[a-b],a-b); ST.update(D[a+b],a+b)
    if t==2:
        #a <= x < b+1 を満たすxが存在するか調べる
        x=ST.fold(D[a],D[b+1])
        if a<=x<=b: print(0)
        else:  #かなり雑に処理する
            ans=10**18
            #1. x<a となる最大のxを取得。foldで可能。
            if D[a]>0: ans=min(ans, abs(a-ST.fold(0,D[a])))
            #2. b<x となる最小のxを取得。bisectで対応。
            Lt=ST.bisect(b+1)
            if Lt<len(P): ans=min(ans,abs(b-P[Lt]))
            #答えを出力
            print(ans)

提出情報

提出日時
問題 B - Abs Abs Function
ユーザ navel_tos
言語 Python (PyPy 3.10-v7.3.12)
得点 500
コード長 2915 Byte
結果 AC
実行時間 833 ms
メモリ 261228 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 500 / 500
結果
AC × 3
AC × 34
セット名 テストケース
Sample 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_rand1_01.txt, 01_rand1_02.txt, 01_rand1_03.txt, 01_rand1_04.txt, 01_rand1_05.txt, 01_rand1_06.txt, 01_rand1_07.txt, 01_rand1_08.txt, 01_rand1_09.txt, 01_rand1_10.txt, 01_rand1_11.txt, 01_rand1_12.txt, 01_rand1_13.txt, 01_rand1_14.txt, 02_rand2_01.txt, 02_rand2_02.txt, 02_rand2_03.txt, 02_rand2_04.txt, 02_rand2_05.txt, 02_rand2_06.txt, 02_rand2_07.txt, 02_rand2_08.txt, 02_rand2_09.txt, 02_rand2_10.txt, 02_rand2_11.txt, 02_rand2_12.txt, 02_rand2_13.txt, 02_rand2_14.txt, 02_rand2_15.txt, 03_test_01.txt, 03_test_02.txt
ケース名 結果 実行時間 メモリ
00_sample_01.txt AC 59 ms 76104 KiB
00_sample_02.txt AC 59 ms 76160 KiB
00_sample_03.txt AC 58 ms 76204 KiB
01_rand1_01.txt AC 768 ms 260572 KiB
01_rand1_02.txt AC 797 ms 260712 KiB
01_rand1_03.txt AC 829 ms 260432 KiB
01_rand1_04.txt AC 818 ms 260276 KiB
01_rand1_05.txt AC 833 ms 260916 KiB
01_rand1_06.txt AC 772 ms 261104 KiB
01_rand1_07.txt AC 734 ms 260316 KiB
01_rand1_08.txt AC 755 ms 259744 KiB
01_rand1_09.txt AC 762 ms 259772 KiB
01_rand1_10.txt AC 773 ms 260772 KiB
01_rand1_11.txt AC 736 ms 260656 KiB
01_rand1_12.txt AC 722 ms 260552 KiB
01_rand1_13.txt AC 742 ms 261040 KiB
01_rand1_14.txt AC 721 ms 260396 KiB
02_rand2_01.txt AC 787 ms 260036 KiB
02_rand2_02.txt AC 760 ms 259756 KiB
02_rand2_03.txt AC 765 ms 259860 KiB
02_rand2_04.txt AC 805 ms 260200 KiB
02_rand2_05.txt AC 777 ms 260856 KiB
02_rand2_06.txt AC 787 ms 260952 KiB
02_rand2_07.txt AC 806 ms 259696 KiB
02_rand2_08.txt AC 810 ms 260032 KiB
02_rand2_09.txt AC 820 ms 260272 KiB
02_rand2_10.txt AC 626 ms 260184 KiB
02_rand2_11.txt AC 626 ms 260860 KiB
02_rand2_12.txt AC 611 ms 260160 KiB
02_rand2_13.txt AC 693 ms 260408 KiB
02_rand2_14.txt AC 678 ms 260352 KiB
02_rand2_15.txt AC 648 ms 261228 KiB
03_test_01.txt AC 58 ms 76308 KiB
03_test_02.txt AC 502 ms 202064 KiB