結果

問題 No.1014 competitive fighting
ユーザー vwxyzvwxyz
提出日時 2022-01-08 18:27:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,257 ms / 2,000 ms
コード長 17,600 bytes
コンパイル時間 455 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 320,936 KB
最終ジャッジ日時 2024-04-26 19:26:59
合計ジャッジ時間 35,367 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
58,328 KB
testcase_01 AC 44 ms
57,216 KB
testcase_02 AC 44 ms
58,972 KB
testcase_03 AC 44 ms
58,232 KB
testcase_04 AC 43 ms
57,824 KB
testcase_05 AC 1,223 ms
225,260 KB
testcase_06 AC 1,239 ms
227,068 KB
testcase_07 AC 1,219 ms
227,948 KB
testcase_08 AC 1,216 ms
229,072 KB
testcase_09 AC 1,227 ms
227,940 KB
testcase_10 AC 1,009 ms
320,936 KB
testcase_11 AC 552 ms
211,368 KB
testcase_12 AC 1,218 ms
227,396 KB
testcase_13 AC 676 ms
147,152 KB
testcase_14 AC 624 ms
142,852 KB
testcase_15 AC 675 ms
149,472 KB
testcase_16 AC 194 ms
81,736 KB
testcase_17 AC 367 ms
103,232 KB
testcase_18 AC 1,016 ms
193,100 KB
testcase_19 AC 1,167 ms
215,140 KB
testcase_20 AC 542 ms
127,560 KB
testcase_21 AC 950 ms
184,360 KB
testcase_22 AC 768 ms
161,156 KB
testcase_23 AC 431 ms
112,640 KB
testcase_24 AC 429 ms
112,640 KB
testcase_25 AC 240 ms
86,104 KB
testcase_26 AC 945 ms
186,568 KB
testcase_27 AC 349 ms
100,884 KB
testcase_28 AC 383 ms
106,560 KB
testcase_29 AC 140 ms
78,516 KB
testcase_30 AC 1,186 ms
222,444 KB
testcase_31 AC 987 ms
190,808 KB
testcase_32 AC 128 ms
78,688 KB
testcase_33 AC 156 ms
84,440 KB
testcase_34 AC 613 ms
178,664 KB
testcase_35 AC 801 ms
223,068 KB
testcase_36 AC 577 ms
171,068 KB
testcase_37 AC 141 ms
80,456 KB
testcase_38 AC 522 ms
163,204 KB
testcase_39 AC 327 ms
118,376 KB
testcase_40 AC 519 ms
164,692 KB
testcase_41 AC 416 ms
139,912 KB
testcase_42 AC 782 ms
209,836 KB
testcase_43 AC 156 ms
84,684 KB
testcase_44 AC 693 ms
205,580 KB
testcase_45 AC 447 ms
146,052 KB
testcase_46 AC 172 ms
90,472 KB
testcase_47 AC 298 ms
119,924 KB
testcase_48 AC 608 ms
180,268 KB
testcase_49 AC 147 ms
80,488 KB
testcase_50 AC 754 ms
210,268 KB
testcase_51 AC 461 ms
150,508 KB
testcase_52 AC 141 ms
80,720 KB
testcase_53 AC 1,257 ms
228,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
from collections import deque
import sys
readline=sys.stdin.readline

class Segment_Tree:
    def __init__(self,N,f,e,lst=None):
        self.f=f
        self.e=e
        self.N=N
        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 or a<-self.N:
                a=self.N
            elif self.N<=a:
                a=self.N*2
            elif a<0:
                a+=self.N*2
            else:
                a+=self.N
            if b==None or self.N<=b:
                b=self.N*2
            elif b<-self.N:
                b=self.N
            elif b<0:
                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 or L<-self.N:
            L=self.N
        elif self.N<=L:
            L=self.N*2
        elif L<0:
            L+=self.N*2
        else:
            L+=self.N
        if R==None or self.N<=R:
            R=self.N*2
        elif R<-self.N:
            R=self.N
        elif R<0:
            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 or L<-self.N:
            L=self.N
        elif self.N<=L:
            L=self.N*2
        elif L<0:
            L+=self.N*2
        else:
            L+=self.N
        if R==None or self.N<=R:
            R=self.N*2
        elif R<-self.N:
            R=self.N
        elif R<0:
            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 Interval_Decomp(self,L=None,R=None):
        if L==None or L<-self.N:
            L=self.N
        elif self.N<=L:
            L=self.N*2
        elif L<0:
            L+=self.N*2
        else:
            L+=self.N
        if R==None or self.N<=R:
            R=self.N*2
        elif R<-self.N:
            R=self.N
        elif R<0:
            R+=self.N*2
        else:
            R+=self.N
        interval_decomp=[]
        while L<R:
            if R&1:
                R-=1
                interval_decomp.append(R)
            if L&1:
                interval_decomp.append(L)
                L+=1
            L>>=1
            R>>=1
        interval_decomp.sort()
        return interval_decomp

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

class Graph:
    def __init__(self,V,edges=False,graph=False,directed=False,weighted=False,inf=float("inf")):
        self.V=V
        self.directed=directed
        self.weighted=weighted
        self.inf=inf
        if not graph:
            self.edges=edges
            self.graph=[[] for i in range(self.V)]
            if weighted:
                for i,j,d in self.edges:
                    self.graph[i].append((j,d))
                    if not self.directed:
                        self.graph[j].append((i,d))
            else:
                for i,j in self.edges:
                    self.graph[i].append(j)
                    if not self.directed:
                        self.graph[j].append(i)
        else:
            self.graph=graph

    def MIV_DFS(self,initial_vertices=None,bipartite_graph=False,cycle_detection=False,directed_acyclic=False,euler_tour=False,linked_components=False,lowlink=False,parents=False,postorder=False,preorder=False,subtree_size=False,topological_sort=False,unweighted_dist=False,weighted_dist=False):
        if initial_vertices==None:
            initial_vertices=[s for s in range(self.V)]
        seen=[False]*self.V
        finished=[False]*self.V
        if bipartite_graph:
            bg=[None]*self.V
            cnt=-1
        if directed_acyclic or cycle_detection or topological_sort:
            dag=True
        if euler_tour:
            et=[]
        if linked_components:
            lc=[]
        if lowlink:
            order=[None]*self.V
            ll=[None]*self.V
            idx=0
        if parents or cycle_detection or lowlink or subtree_size:
            ps=[None]*self.V
        if postorder or topological_sort:
            post=[]
        if preorder:
            pre=[]
        if subtree_size:
            ss=[1]*self.V
        if unweighted_dist:
            uwd=[self.inf]*self.V
        if weighted_dist:
            wd=[self.inf]*self.V
        for s in initial_vertices:
            if seen[s]:
                continue
            if bipartite_graph:
                cnt+=1
                bg[s]=(cnt,0)
            if linked_components:
                lc.append([])
            if unweighted_dist:
                uwd[s]=0
            if weighted_dist:
                wd[s]=0
            stack=[(s,0)] if self.weighted else [s]
            while stack:
                if self.weighted:
                    x,d=stack.pop()
                else:
                    x=stack.pop()
                if not seen[x]:
                    seen[x]=True
                    stack.append((x,d) if self.weighted else x)
                    if euler_tour:
                        et.append(x)
                    if linked_components:
                        lc[-1].append(x)
                    if lowlink:
                        order[x]=idx
                        ll[x]=idx
                        idx+=1
                    if preorder:
                        pre.append(x)
                    for y in self.graph[x]:
                        if self.weighted:
                            y,d=y
                        if not seen[y]:
                            stack.append((y,d) if self.weighted else y)
                            if bipartite_graph:
                                bg[y]=(cnt,bg[x][1]^1)
                            if parents or cycle_detection or lowlink or subtree_size:
                                ps[y]=x
                            if unweighted_dist or bipartite_graph:
                                uwd[y]=uwd[x]+1
                            if weighted_dist:
                                wd[y]=wd[x]+d
                        elif not finished[y]:
                            if directed_acyclic and dag:
                                dag=False
                                if cycle_detection:
                                    cd=(y,x)
                elif not finished[x]:
                    finished[x]=True
                    if euler_tour:
                        et.append(~x)
                    if lowlink:
                        for y in self.graph[x]:
                            if self.weighted:
                                y,d=y
                            if ps[x]==y:
                                continue
                            ll[x]=min(ll[x],order[y])
                        if x!=s:
                            ll[ps[x]]=min(ll[ps[x]],ll[x])
                    if postorder or topological_sort:
                        post.append(x)
                    if subtree_size:
                        for y in self.graph[x]:
                            if self.weighted:
                                y,d=y
                            if y==ps[x]:
                                continue
                            ss[x]+=ss[y]
        if bipartite_graph:
            bg_=bg
            bg=[[[],[]] for i in range(cnt+1)]
            for tpl in self.edges:
                i,j=tpl[:2] if self.weighted else tpl
                if not bg_[i][1]^bg_[j][1]:
                    bg[bg_[i][0]]=False
            for x in range(self.V):
                if bg[bg_[x][0]]:
                    bg[bg_[x][0]][bg_[x][1]].append(x)
        retu=()
        if bipartite_graph:
            retu+=(bg,)
        if cycle_detection:
            if dag:
                cd=[]
            else:
                y,x=cd
                cd=self.Route_Restoration(y,x,ps)
            retu+=(cd,)
        if directed_acyclic:
            retu+=(dag,)
        if euler_tour:
            retu+=(et,)
        if linked_components:
            retu+=(lc,)
        if lowlink:
            retu=(ll,)
        if parents:
            retu+=(ps,)
        if postorder:
            retu+=(post,)
        if preorder:
            retu+=(pre,)
        if subtree_size:
            retu+=(ss,)
        if topological_sort:
            if dag:
                tp_sort=post[::-1]
            else:
                tp_sort=[]
            retu+=(tp_sort,)
        if unweighted_dist:
            retu+=(uwd,)
        if weighted_dist:
            retu+=(wd,)
        if len(retu)==1:
            retu=retu[0]
        return retu

    def SIV_DFS(self,s,bipartite_graph=False,cycle_detection=False,directed_acyclic=False,euler_tour=False,linked_components=False,lowlink=False,parents=False,postorder=False,preorder=False,subtree_size=False,topological_sort=False,unweighted_dist=False,weighted_dist=False):
        seen=[False]*self.V
        finished=[False]*self.V
        if directed_acyclic or cycle_detection or topological_sort:
            dag=True
        if euler_tour:
            et=[]
        if linked_components:
            lc=[]
        if lowlink:
            order=[None]*self.V
            ll=[None]*self.V
            idx=0
        if parents or cycle_detection or lowlink or subtree_size:
            ps=[None]*self.V
        if postorder or topological_sort:
            post=[]
        if preorder:
            pre=[]
        if subtree_size:
            ss=[1]*self.V
        if unweighted_dist or bipartite_graph:
            uwd=[self.inf]*self.V
            uwd[s]=0
        if weighted_dist:
            wd=[self.inf]*self.V
            wd[s]=0
        stack=[(s,0)] if self.weighted else [s]
        while stack:
            if self.weighted:
                x,d=stack.pop()
            else:
                x=stack.pop()
            if not seen[x]:
                seen[x]=True
                stack.append((x,d) if self.weighted else x)
                if euler_tour:
                    et.append(x)
                if linked_components:
                    lc.append(x)
                if lowlink:
                    order[x]=idx
                    ll[x]=idx
                    idx+=1
                if preorder:
                    pre.append(x)
                for y in self.graph[x]:
                    if self.weighted:
                        y,d=y
                    if not seen[y]:
                        stack.append((y,d) if self.weighted else y)
                        if parents or cycle_detection or lowlink or subtree_size:
                            ps[y]=x
                        if unweighted_dist or bipartite_graph:
                            uwd[y]=uwd[x]+1
                        if weighted_dist:
                            wd[y]=wd[x]+d
                    elif not finished[y]:
                        if (directed_acyclic or cycle_detection or topological_sort) and dag:
                            dag=False
                            if cycle_detection:
                                cd=(y,x)
            elif not finished[x]:
                finished[x]=True
                if euler_tour:
                    et.append(~x)
                if lowlink:
                    for y in self.graph[x]:
                        if self.weighted:
                            y,d=y
                        if ps[x]==y:
                            continue
                        ll[x]=min(ll[x],order[y])
                    if x!=s:
                        ll[ps[x]]=min(ll[ps[x]],ll[x])
                if postorder or topological_sort:
                    post.append(x)
                if subtree_size:
                    for y in self.graph[x]:
                        if self.weighted:
                            y,d=y
                        if y==ps[x]:
                            continue
                        ss[x]+=ss[y]
        if bipartite_graph:
            bg=[[],[]]
            for tpl in self.edges:
                x,y=tpl[:2] if self.weighted else tpl
                if uwd[x]==self.inf or uwd[y]==self.inf:
                    continue
                if not uwd[x]%2^uwd[y]%2:
                    bg=False
                    break
            else:
                for x in range(self.V):
                    if uwd[x]==self.inf:
                        continue
                    bg[uwd[x]%2].append(x)
        retu=()
        if bipartite_graph:
            retu+=(bg,)
        if cycle_detection:
            if dag:
                cd=[]
            else:
                y,x=cd
                cd=self.Route_Restoration(y,x,ps)
            retu+=(cd,)
        if directed_acyclic:
            retu+=(dag,)
        if euler_tour:
            retu+=(et,)
        if linked_components:
            retu+=(lc,)
        if lowlink:
            retu=(ll,)
        if parents:
            retu+=(ps,)
        if postorder:
            retu+=(post,)
        if preorder:
            retu+=(pre,)
        if subtree_size:
            retu+=(ss,)
        if topological_sort:
            if dag:
                tp_sort=post[::-1]
            else:
                tp_sort=[]
            retu+=(tp_sort,)
        if unweighted_dist:
            retu+=(uwd,)
        if weighted_dist:
            retu+=(wd,)
        if len(retu)==1:
            retu=retu[0]
        return retu

    def SCC(self):
        reverse_graph=[[] for i in range(self.V)]
        for u in range(self.V):
            for v,d in self.graph[u]:
                reverse_graph[v].append(u)
        postorder=self.MIV_DFS(postorder=True)
        scc_points=[]
        seen=[False]*self.V
        for s in postorder[::-1]:
            if seen[s]:
                continue
            queue=deque([s])
            seen[s]=True
            lst=[]
            while queue:
                x=queue.popleft()
                lst.append(x)
                for y in reverse_graph[x]:
                    if not seen[y]:
                        seen[y]=True
                        queue.append(y)
            scc_points.append(lst)
        l=len(scc_points)
        idx=[None]*self.V
        for i in range(l):
            for x in scc_points[i]:
                idx[x]=i
        scc_edges=set()
        for u in range(self.V):
            for v,d in self.graph[u]:
                if idx[u]!=idx[v]:
                    scc_edges.add((idx[u],idx[v]))
        scc_edges=list(scc_edges)
        return scc_points,scc_edges

N=int(readline())
ABCI=[]
for i in range(N):
    a,b,c=map(int,readline().split())
    ABCI.append((a,b,c,i))
ABCI.sort()
edges=[]
A,B,C,I=[],[],[],[]
for a,b,c,i in ABCI:
    A.append(a)
    B.append(b)
    C.append(c)
    I.append(i)
graph=[[] for i in range(2*N)]
for i in range(1,N):
    graph[i].append((2*i,0))
    graph[i].append((2*i+1,0))
inf=float("inf")
ST=Segment_Tree(N,min,inf)
for i in range(N):
    r=bisect.bisect_right(A,B[i]-C[i])
    for j in ST.Interval_Decomp(0,min(r,i)):
        graph[i+N].append((j,B[i]))
    for j in ST.Interval_Decomp(min(r,i+1),r):
        graph[i+N].append((j,B[i]))
G=Graph(2*N,graph=graph,weighted=True,directed=True)
scc,scc_edges=G.SCC()
l=len(scc)
dp=[0]*l
for i in range(l):
    if len(scc[i])>=2:
        dp[i]=inf
SCC=Graph(l,edges=scc_edges,directed=True)
for i in range(l-1,-1,-1):
    for j in SCC.graph[i]:
        dp[i]=max(dp[i],dp[j])
    if scc[i][0]>=N:
        dp[i]+=B[scc[i][0]-N]
ans_lst=[None]*N
for i in range(l):
    if dp[i]==float("inf"):
        dp[i]="BAN"
    for x in scc[i]:
        if x>=N:
            ans_lst[I[x-N]]=dp[i]
print(*ans_lst,sep="\n")
0