結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
59,452 KB
testcase_01 AC 46 ms
59,216 KB
testcase_02 AC 45 ms
58,080 KB
testcase_03 AC 45 ms
58,448 KB
testcase_04 AC 45 ms
58,852 KB
testcase_05 AC 1,272 ms
225,776 KB
testcase_06 AC 1,252 ms
227,448 KB
testcase_07 AC 1,251 ms
228,080 KB
testcase_08 AC 1,266 ms
229,340 KB
testcase_09 AC 1,254 ms
228,084 KB
testcase_10 AC 1,035 ms
320,952 KB
testcase_11 AC 561 ms
211,696 KB
testcase_12 AC 1,219 ms
226,404 KB
testcase_13 AC 644 ms
147,412 KB
testcase_14 AC 639 ms
142,852 KB
testcase_15 AC 688 ms
149,476 KB
testcase_16 AC 191 ms
81,860 KB
testcase_17 AC 357 ms
103,716 KB
testcase_18 AC 1,047 ms
193,240 KB
testcase_19 AC 1,236 ms
215,268 KB
testcase_20 AC 559 ms
127,828 KB
testcase_21 AC 988 ms
184,748 KB
testcase_22 AC 824 ms
161,416 KB
testcase_23 AC 433 ms
113,056 KB
testcase_24 AC 438 ms
112,892 KB
testcase_25 AC 242 ms
86,236 KB
testcase_26 AC 984 ms
186,832 KB
testcase_27 AC 364 ms
101,140 KB
testcase_28 AC 401 ms
106,560 KB
testcase_29 AC 141 ms
78,812 KB
testcase_30 AC 1,208 ms
222,344 KB
testcase_31 AC 1,012 ms
191,320 KB
testcase_32 AC 131 ms
78,828 KB
testcase_33 AC 160 ms
84,312 KB
testcase_34 AC 624 ms
178,536 KB
testcase_35 AC 813 ms
222,960 KB
testcase_36 AC 585 ms
171,152 KB
testcase_37 AC 149 ms
80,568 KB
testcase_38 AC 539 ms
163,328 KB
testcase_39 AC 331 ms
118,504 KB
testcase_40 AC 528 ms
164,824 KB
testcase_41 AC 413 ms
139,908 KB
testcase_42 AC 818 ms
210,196 KB
testcase_43 AC 164 ms
84,688 KB
testcase_44 AC 729 ms
205,732 KB
testcase_45 AC 462 ms
146,312 KB
testcase_46 AC 178 ms
90,612 KB
testcase_47 AC 310 ms
120,160 KB
testcase_48 AC 601 ms
180,652 KB
testcase_49 AC 142 ms
80,812 KB
testcase_50 AC 762 ms
210,524 KB
testcase_51 AC 480 ms
150,396 KB
testcase_52 AC 146 ms
80,848 KB
testcase_53 AC 1,361 ms
227,976 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