結果

問題 No.808 Kaiten Sushi?
ユーザー vwxyzvwxyz
提出日時 2024-04-08 09:15:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 926 ms / 2,000 ms
コード長 5,990 bytes
コンパイル時間 196 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 175,200 KB
最終ジャッジ日時 2024-10-01 05:00:20
合計ジャッジ時間 23,942 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
55,680 KB
testcase_01 AC 53 ms
62,592 KB
testcase_02 AC 44 ms
55,552 KB
testcase_03 AC 44 ms
55,424 KB
testcase_04 AC 44 ms
55,680 KB
testcase_05 AC 42 ms
55,936 KB
testcase_06 AC 47 ms
60,416 KB
testcase_07 AC 148 ms
78,680 KB
testcase_08 AC 153 ms
78,748 KB
testcase_09 AC 192 ms
79,424 KB
testcase_10 AC 184 ms
79,268 KB
testcase_11 AC 175 ms
78,484 KB
testcase_12 AC 177 ms
78,884 KB
testcase_13 AC 171 ms
78,652 KB
testcase_14 AC 127 ms
77,384 KB
testcase_15 AC 199 ms
79,408 KB
testcase_16 AC 197 ms
79,268 KB
testcase_17 AC 127 ms
77,824 KB
testcase_18 AC 121 ms
77,056 KB
testcase_19 AC 97 ms
76,416 KB
testcase_20 AC 125 ms
77,216 KB
testcase_21 AC 101 ms
76,288 KB
testcase_22 AC 124 ms
77,564 KB
testcase_23 AC 321 ms
99,328 KB
testcase_24 AC 285 ms
104,904 KB
testcase_25 AC 294 ms
104,960 KB
testcase_26 AC 286 ms
103,492 KB
testcase_27 AC 704 ms
157,672 KB
testcase_28 AC 378 ms
99,256 KB
testcase_29 AC 683 ms
149,852 KB
testcase_30 AC 506 ms
108,288 KB
testcase_31 AC 717 ms
159,744 KB
testcase_32 AC 796 ms
174,932 KB
testcase_33 AC 515 ms
106,392 KB
testcase_34 AC 570 ms
123,524 KB
testcase_35 AC 655 ms
139,084 KB
testcase_36 AC 525 ms
106,412 KB
testcase_37 AC 44 ms
55,168 KB
testcase_38 AC 42 ms
55,424 KB
testcase_39 AC 838 ms
165,148 KB
testcase_40 AC 371 ms
95,232 KB
testcase_41 AC 369 ms
98,460 KB
testcase_42 AC 711 ms
148,168 KB
testcase_43 AC 432 ms
104,304 KB
testcase_44 AC 598 ms
123,496 KB
testcase_45 AC 450 ms
103,180 KB
testcase_46 AC 329 ms
90,368 KB
testcase_47 AC 926 ms
174,908 KB
testcase_48 AC 878 ms
175,200 KB
testcase_49 AC 900 ms
175,140 KB
testcase_50 AC 894 ms
175,044 KB
testcase_51 AC 882 ms
174,928 KB
testcase_52 AC 578 ms
140,236 KB
testcase_53 AC 756 ms
174,584 KB
testcase_54 AC 46 ms
55,168 KB
testcase_55 AC 42 ms
55,424 KB
testcase_56 AC 41 ms
55,424 KB
testcase_57 AC 350 ms
108,800 KB
testcase_58 AC 473 ms
118,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class Segment_Tree:
    def __init__(self,N,f,e,lst=None,dynamic=False):
        self.f=f
        self.e=e
        self.N=N
        if dynamic:
            self.segment_tree=defaultdict(lambda:self.e)
        else:
            if lst==None:
                self.segment_tree=[self.e]*2*self.N
            else:
                assert len(lst)<=self.N
                self.segment_tree=[self.e]*self.N+[x for x in lst]+[self.e]*(N-len(lst))
                for i in range(self.N-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.N<=i<0:
                return self.segment_tree[i+self.N*2]
            elif 0<=i<self.N:
                return self.segment_tree[i+self.N]
            else:
                raise IndexError("list index out of range")
        else:
            a,b,c=i.start,i.stop,i.step
            if a==None:
                a=self.N
            else:
                a+=self.N
            if b==None:
                b=self.N*2
            else:
                b+=self.N
            return self.segment_tree[slice(a,b,c)]

    def __setitem__(self,i,x):
        if -self.N<=i<0:
            i+=self.N*2
        elif 0<=i<self.N:
            i+=self.N
        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.N):
            self.segment_tree[i]=x
        for i in range(self.N-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.N
        else:
            L+=self.N
        if R==None:
            R=self.N*2
        else:
            R+=self.N
        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.N
        else:
            L+=self.N
        if R==None:
            R=self.N*2
        else:
            R+=self.N
        if L==R:
            return None
        x=self.Fold(L-self.N,R-self.N)
        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.N:
            if self.segment_tree[i]==self.segment_tree[i<<1]:
                i<<=1
            else:
                i<<=1
                i|=1
        i-=self.N
        return i

    def Bisect_Right(self,L=None,f=None):
        if L==self.N:
            return self.N
        if L==None:
            L=0
        L+=self.N
        vl=self.e
        vr=self.e
        l,r=L,self.N*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.N:
                    L<<=1
                    vv=self.f(v,self.segment_tree[L])
                    if f(vv):
                        v=vv
                        L+=1
                return L-self.N

    def Bisect_Left(self,R=None,f=None):
        if R==0:
            return 0
        if R==None:
            R=self.N
        R+=self.N
        vl=self.e
        vr=self.e
        l,r=self.N,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.N:
                    R=2*R+1
                    vv=self.f(self.segment_tree[R],v)
                    if f(vv):
                        v=vv
                        R-=1
                return R+1-self.N

    def __str__(self):
        return "["+", ".join(map(str,self.segment_tree[self.N:]))+"]"

def Compress(lst):
    decomp=sorted(list(set(lst)))
    comp={x:i for i,x in enumerate(decomp)}
    return comp,decomp

N,L=map(int,input().split())
X=list(map(int,input().split()))
Y=list(map(int,input().split()))
XY=[0]+X+Y
comp,decomp=Compress(XY)
le=len(comp)
STX=Segment_Tree(2*le,lambda x,y:x+y,0)
for x in X:
    STX[comp[x]]+=1
    STX[comp[x]+le]+=1
STY=Segment_Tree(2*le,lambda x,y:x+y,0)
for y in Y:
    STY[comp[y]]+=1
    STY[comp[y]+le]+=1
cur=0
ans=0
x=STX.Bisect_Right(0,lambda s:s==0)
ans=decomp[x]
STX[x]=0
STX[x+le]=0
for _ in range(N-1):
    prev=x
    y=STY.Bisect_Right((x+1)%le,lambda s:s==0)%le
    x=STX.Bisect_Right((y+1)%le,lambda s:s==0)%le
    y=(STY.Bisect_Left(x+le,lambda s:s==0)-1)%le
    ans+=(decomp[x]-decomp[y])%L+(decomp[y]-decomp[prev])%L
    STX[x]=0
    STX[x+le]=0
    STY[y]=0
    STY[y+le]=0
y=STY.Bisect_Right(0,lambda s:s==0)
ans+=(decomp[y]-decomp[x])%L
print(ans)
0