結果

問題 No.855 ヘビの日光浴
ユーザー vwxyzvwxyz
提出日時 2024-04-13 12:47:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,023 ms / 3,153 ms
コード長 7,952 bytes
コンパイル時間 518 ms
コンパイル使用メモリ 82,516 KB
実行使用メモリ 137,968 KB
最終ジャッジ日時 2024-04-13 12:47:56
合計ジャッジ時間 26,143 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
56,244 KB
testcase_01 AC 47 ms
56,648 KB
testcase_02 AC 46 ms
56,252 KB
testcase_03 AC 48 ms
57,204 KB
testcase_04 AC 47 ms
56,860 KB
testcase_05 AC 47 ms
56,252 KB
testcase_06 AC 50 ms
57,120 KB
testcase_07 AC 48 ms
56,980 KB
testcase_08 AC 52 ms
57,860 KB
testcase_09 AC 54 ms
57,412 KB
testcase_10 AC 66 ms
56,636 KB
testcase_11 AC 50 ms
57,220 KB
testcase_12 AC 47 ms
57,196 KB
testcase_13 AC 53 ms
57,016 KB
testcase_14 AC 48 ms
57,108 KB
testcase_15 AC 50 ms
57,416 KB
testcase_16 AC 48 ms
57,740 KB
testcase_17 AC 51 ms
58,136 KB
testcase_18 AC 49 ms
56,892 KB
testcase_19 AC 49 ms
58,108 KB
testcase_20 AC 48 ms
56,552 KB
testcase_21 AC 50 ms
56,920 KB
testcase_22 AC 50 ms
57,224 KB
testcase_23 AC 50 ms
57,420 KB
testcase_24 AC 48 ms
57,452 KB
testcase_25 AC 47 ms
56,584 KB
testcase_26 AC 48 ms
57,072 KB
testcase_27 AC 46 ms
57,452 KB
testcase_28 AC 48 ms
56,696 KB
testcase_29 AC 63 ms
58,796 KB
testcase_30 AC 65 ms
56,328 KB
testcase_31 AC 72 ms
58,080 KB
testcase_32 AC 48 ms
56,444 KB
testcase_33 AC 138 ms
77,368 KB
testcase_34 AC 129 ms
76,852 KB
testcase_35 AC 75 ms
72,864 KB
testcase_36 AC 103 ms
76,712 KB
testcase_37 AC 113 ms
76,892 KB
testcase_38 AC 138 ms
77,084 KB
testcase_39 AC 96 ms
76,132 KB
testcase_40 AC 134 ms
77,348 KB
testcase_41 AC 109 ms
76,372 KB
testcase_42 AC 100 ms
73,288 KB
testcase_43 AC 96 ms
68,172 KB
testcase_44 AC 139 ms
77,324 KB
testcase_45 AC 114 ms
76,504 KB
testcase_46 AC 130 ms
77,960 KB
testcase_47 AC 94 ms
76,700 KB
testcase_48 AC 129 ms
76,608 KB
testcase_49 AC 109 ms
76,880 KB
testcase_50 AC 117 ms
76,976 KB
testcase_51 AC 94 ms
76,456 KB
testcase_52 AC 73 ms
70,528 KB
testcase_53 AC 47 ms
57,068 KB
testcase_54 AC 50 ms
57,768 KB
testcase_55 AC 55 ms
56,800 KB
testcase_56 AC 69 ms
57,368 KB
testcase_57 AC 70 ms
57,716 KB
testcase_58 AC 46 ms
57,324 KB
testcase_59 AC 48 ms
57,124 KB
testcase_60 AC 47 ms
57,988 KB
testcase_61 AC 46 ms
56,376 KB
testcase_62 AC 48 ms
58,148 KB
testcase_63 AC 45 ms
57,060 KB
testcase_64 AC 48 ms
58,360 KB
testcase_65 AC 51 ms
58,696 KB
testcase_66 AC 48 ms
57,336 KB
testcase_67 AC 45 ms
57,172 KB
testcase_68 AC 47 ms
56,808 KB
testcase_69 AC 48 ms
56,816 KB
testcase_70 AC 46 ms
57,120 KB
testcase_71 AC 49 ms
57,684 KB
testcase_72 AC 48 ms
57,896 KB
testcase_73 AC 674 ms
96,760 KB
testcase_74 AC 599 ms
95,332 KB
testcase_75 AC 524 ms
87,036 KB
testcase_76 AC 810 ms
113,400 KB
testcase_77 AC 729 ms
105,712 KB
testcase_78 AC 945 ms
125,312 KB
testcase_79 AC 270 ms
80,860 KB
testcase_80 AC 342 ms
81,072 KB
testcase_81 AC 998 ms
129,504 KB
testcase_82 AC 959 ms
137,968 KB
testcase_83 AC 1,023 ms
130,400 KB
testcase_84 AC 964 ms
131,396 KB
testcase_85 AC 1,009 ms
130,740 KB
testcase_86 AC 947 ms
131,080 KB
testcase_87 AC 969 ms
130,840 KB
testcase_88 AC 934 ms
130,832 KB
testcase_89 AC 1,001 ms
130,792 KB
testcase_90 AC 936 ms
131,272 KB
testcase_91 AC 1,005 ms
130,856 KB
testcase_92 AC 954 ms
131,260 KB
testcase_93 AC 45 ms
56,324 KB
testcase_94 AC 368 ms
123,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
import bisect

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

W,H,N=map(int,input().split())
XYL=[]
X,Y=[],[]
for i in range(N):
    x,y,l=map(int,input().split())
    if x in (0,H+1):
        Y.append(y)
        l=min(l,H+1)
    else:
        X.append(x)
        l=min(l,W+1)
    XYL.append((x,y,l))
compX,decompX=Compress(X)
compY,decompY=Compress(Y)
leX=len(compX)
leY=len(compY)
inf=1<<60
STX0=Segment_Tree(leX,max,-inf,[0]*leX)
STX1=Segment_Tree(leX,max,-inf,[0]*leX)
STY0=Segment_Tree(leY,max,-inf,[0]*leY)
STY1=Segment_Tree(leY,max,-inf,[0]*leY)
for x,y,L in XYL:
    if x==0:
        j=compY[y]
        L+=STY0[j]
        r0=STX0.Bisect_Right(0,lambda ma:ma<=y-1)
        r1=STX1.Bisect_Right(0,lambda ma:ma<=W-y)
        if r0<r1 and decompX[r0]<=L and STY1[j]<H+1-decompX[r0]:
            STX0[r0]=0
            STY0[j]=0
        elif r1<r0 and decompX[r1]<=L and STY1[j]<H+1-decompX[r1]:
            STX1[r1]=0
            STY0[j]=0
        elif L+STY1[j]>=H+1:
            STY0[j]=0
            STY1[j]=0
        elif L<H+1:
            STY0[j]=L
        else:
            STY0[j]=0
    elif x==H+1:
        j=compY[y]
        L+=STY1[j]
        l0=STX0.Bisect_Left(leX,lambda ma:ma<=y-1)
        l1=STX1.Bisect_Left(leX,lambda ma:ma<=W-y)
        if l0>l1 and H+1-decompX[l0-1]<=L and STY0[j]<decompX[l0-1]:
            STX0[l0-1]=0
            STY1[j]=0
        elif l1>l0 and H+1-decompX[l1-1]<=L and STY0[j]<decompX[l1-1]:
            STX1[l1-1]=0
            STY1[j]=0
        elif STY0[j]+L>=H+1:
            STY1[j]=0
            STY0[j]=0
        elif L<H+1:
            STY1[j]=L
        else:
            STY1[j]=0
    elif y==0:
        i=compX[x]
        L+=STX0[i]
        r0=STY0.Bisect_Right(0,lambda ma:ma<=x-1)
        r1=STY1.Bisect_Right(0,lambda ma:ma<=H-x)
        if r0<r1 and decompY[r0]<=L and STX1[i]<W+1-decompY[r0]:
            STY0[r0]=0
            STX0[i]=0
        elif r1<r0 and decompY[r1]<=L and STX1[i]<W+1-decompY[r1]:
            STY1[r1]=0
            STX0[i]=0
        elif L+STX1[i]>=W+1:
            STX0[i]=0
            STX1[i]=0
        elif L<W+1:
            STX0[i]=L
        else:
            STX0[i]=0
    elif y==W+1:
        i=compX[x]
        L+=STX1[i]
        l0=STY0.Bisect_Left(leY,lambda ma:ma<=x-1)
        l1=STY1.Bisect_Left(leY,lambda ma:ma<=H-x)
        if l0>l1 and W+1-decompY[l0-1]<=L and STX0[i]<decompY[l0-1]:
            STY0[l0-1]=0
            STX1[i]=0
        elif l1>l0 and W+1-decompY[l1-1]<=L and STX0[i]<decompY[l1-1]:
            STY1[l1-1]=0
            STX1[i]=0
        elif STX0[i]+L>=W+1:
            STX1[i]=0
            STX0[i]=0
        elif L<W+1:
            STX1[i]=L
        else:
            STX1[i]=0
ans=sum(STX0[x]+STX1[x] for x in range(leX))+sum(STY0[y]+STY1[y] for y in range(leY))
print(ans)
0