結果
問題 | No.235 めぐるはめぐる (5) |
ユーザー | vwxyz |
提出日時 | 2022-03-25 02:30:18 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 7,421 ms / 10,000 ms |
コード長 | 17,714 bytes |
コンパイル時間 | 310 ms |
コンパイル使用メモリ | 82,328 KB |
実行使用メモリ | 266,268 KB |
最終ジャッジ日時 | 2024-10-13 09:46:46 |
合計ジャッジ時間 | 27,872 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 7,421 ms
266,024 KB |
testcase_01 | AC | 4,674 ms
257,780 KB |
testcase_02 | AC | 7,040 ms
266,268 KB |
ソースコード
import sys readline=sys.stdin.readline 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 graph: self.graph=graph self.edges=[] for i in range(self.V): if self.weighted: for j,d in self.graph[i]: if self.directed or not self.directed and i<=j: self.edges.append((i,j,d)) else: for j in self.graph[i]: if self.directed or not self.directed and i<=j: self.edges.append((i,j)) else: 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) 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: bl=True for y in self.graph[x]: if self.weighted: y,d=y if ps[x]==y and bl: bl=False 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 Build_HLD(self,s): self.hld_parents,size,self.hld_depth=self.SIV_DFS(s,parents=True,subtree_size=True,unweighted_dist=True) stack=[s] self.hld_tour=[] self.hld_path_parents=[None]*self.V self.hld_path_parents[s]=s while stack: x=stack.pop() self.hld_tour.append(x) max_size=0 max_size_y=None for y in self.graph[x]: if self.weighted: y,d=y if y==self.hld_parents[x]: continue if max_size<size[y]: max_size=size[y] max_size_y=y for y in self.graph[x]: if self.weighted: y,d=y if y==self.hld_parents[x]: continue if y!=max_size_y: stack.append(y) self.hld_path_parents[y]=y if max_size_y!=None: stack.append(max_size_y) self.hld_path_parents[max_size_y]=self.hld_path_parents[x] self.hld_tour_idx=[None]*self.V for i in range(self.V): self.hld_tour_idx[self.hld_tour[i]]=i def HLD(self,a,b,edge=False): L,R=[],[] while self.hld_path_parents[a]!=self.hld_path_parents[b]: if self.hld_depth[self.hld_path_parents[a]]<self.hld_depth[self.hld_path_parents[b]]: R.append((self.hld_tour_idx[self.hld_path_parents[b]],self.hld_tour_idx[b]+1)) b=self.hld_parents[self.hld_path_parents[b]] else: L.append((self.hld_tour_idx[a]+1,self.hld_tour_idx[self.hld_path_parents[a]])) a=self.hld_parents[self.hld_path_parents[a]] if edge: if self.hld_depth[a]!=self.hld_depth[b]: retu=L+[(self.hld_tour_idx[a]+1,self.hld_tour_idx[b]+1)]+R[::-1] else: retu=L+R[::-1] else: if self.hld_depth[a]<self.hld_depth[b]: retu=L+[(self.hld_tour_idx[a],self.hld_tour_idx[b]+1)]+R[::-1] else: retu=L+[(self.hld_tour_idx[a]+1,self.hld_tour_idx[b])]+R[::-1] return retu class Lazy_Segment_Tree: def __init__(self,N,f,e,f_act,e_act,operate,lst=None): self.N=N self.f=f self.e=e self.f_act=f_act self.e_act=e_act self.operate=operate self.segment_tree=[self.e]*(self.N+self.N) self.segment_tree_act=[self.e_act]*(self.N+self.N) if lst!=None: for i,x in enumerate(lst): self.segment_tree[i+self.N]=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]) self.segment_tree_act=[self.e_act]*(self.N+self.N) def __getitem__(self,i): if type(i) is int: 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.Propagate_Above(i) self.Recalculate_Above(i) return self.Operate_At(i) 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.Propagate_Above(i) self.segment_tree[i]=x self.segment_tree_act[i]=self.e_act self.Recalculate_Above(i) def Operate_At(self,i): return self.operate(self.segment_tree[i],self.segment_tree_act[i]) def Propagate_At(self,i): self.segment_tree[i]=self.Operate_At(i) self.segment_tree_act[i<<1]=self.f_act(self.segment_tree_act[i<<1],self.segment_tree_act[i]) self.segment_tree_act[i<<1|1]=self.f_act(self.segment_tree_act[i<<1|1],self.segment_tree_act[i]) self.segment_tree_act[i]=self.e_act def Propagate_Above(self,i): H=i.bit_length()-1 for h in range(H,0,-1): self.Propagate_At(i>>h) def Recalculate_Above(self,i): while i>1: i>>=1 self.segment_tree[i]=self.f(self.Operate_At(i<<1),self.Operate_At(i<<1|1)) def Build(self,lst): for i,x in enumerate(lst): self.segment_tree[i+self.N]=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]) self.segment_tree_act=[self.e_act]*(self.N+self.N) 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 self.Propagate_Above(L//(L&-L)) self.Propagate_Above(R//(R&-R)-1) vL=self.e vR=self.e while L<R: if L&1: vL=self.f(vL,self.Operate_At(L)) L+=1 if R&1: R-=1 vR=self.f(self.Operate_At(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 Operate_Range(self,a,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 L0=L//(L&-L) R0=R//(R&-R)-1 self.Propagate_Above(L0) self.Propagate_Above(R0) while L<R: if L&1: self.segment_tree_act[L]=self.f_act(self.segment_tree_act[L],a) L+=1 if R&1: R-=1 self.segment_tree_act[R]=self.f_act(self.segment_tree_act[R],a) L>>=1 R>>=1 self.Recalculate_Above(L0) self.Recalculate_Above(R0) def Update(self): for i in range(1,self.N): self.Propagate_At(i) for i in range(self.N,self.N*2): self.segment_tree[i]=self.Operate_At(i) self.segment_tree_act[i]=self.e_act 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 Bisect_Right(self,L=None,f=None): if L==self.N: return self.N if L==None: L=0 L+=self.N self.Propagate_Above(L//(L&-L)) self.Propagate_Above(self.N//(self.N&-self.N)-1) l,r=L,self.N*2 vl=self.e vr=self.e while l<r: if l&1: vl=self.f(vl,self.Operate_At(l)) l+=1 if r&1: r-=1 vr=self.f(self.Operate_At(r),vr) l>>=1 r>>=1 if f(self.f(vl,vr)): return self.N v=self.e self.Propagate_Above(L) while True: while L%2==0: L>>=1 vv=self.f(v,self.Operate_At(L)) if f(vv): v=vv L+=1 else: while L<self.N: self.Propagate_At(L) L<<=1 vv=self.f(v,self.Operate_At(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 self.Propagate_Above(self.N//(self.N&-self.N)) self.Propagate_Above(R//(R&-R)-1) vl=self.e vr=self.e l,r=self.N,R while l<r: if l&1: vl=self.f(vl,self.Operate_At(l)) l+=1 if r&1: r-=1 vr=self.f(self.Operate_At(r),vr) l>>=1 r>>=1 if f(self.f(vl,vr)): return 0 v=self.e self.Propagate_Above(R-1) while True: R-=1 while R>1 and R%2: R>>=1 vv=self.f(self.Operate_At(R),v) if f(vv): v=vv else: while R<self.N: self.Propagate_At(R) R=(R<<1)|1 vv=self.f(self.Operate_At(R),v) if f(vv): v=vv R-=1 return R+1-self.N def __str__(self): import copy segment_tree=copy.deepcopy(self.segment_tree) segment_tree_act=copy.deepcopy(self.segment_tree_act) for i in range(1,self.N): segment_tree[i]=self.operate(segment_tree[i],segment_tree_act[i]) segment_tree_act[i<<1]=self.f_act(segment_tree_act[i<<1],segment_tree_act[i]) segment_tree_act[i<<1|1]=self.f_act(segment_tree_act[i<<1|1],segment_tree_act[i]) segment_tree_act[i]=self.e_act for i in range(self.N,self.N*2): segment_tree[i]=self.operate(segment_tree[i],segment_tree_act[i]) segment_tree_act[i]=self.e_act for i in range(self.N-1,0,-1): segment_tree[i]=self.f(segment_tree[i<<1],segment_tree[i<<1|1]) return '['+', '.join(map(str,[self.operate(x,a) for x,a in zip(segment_tree[self.N:],segment_tree_act[self.N:])]))+']' N=int(readline()) mod=10**9+7 S=list(map(int,readline().split())) C=list(map(int,readline().split())) edges=[] for _ in range(N-1): A,B=map(int,readline().split()) A-=1;B-=1 edges.append((A,B)) G=Graph(N,edges=edges) G.Build_HLD(0) def f(tpl0,tpl1): s0,c0=tpl0 s1,c1=tpl1 s=(s0+s1)%mod c=(c0+c1)%mod return (s,c) e=(0,0) def f_act(a,b): return (a+b)%mod e_act=0 def operate(tpl,a): s,c=tpl return ((s+c*a)%mod,c) LST=Lazy_Segment_Tree(N,f,e,f_act,e_act,operate,[(S[i],C[i]) for i in G.hld_tour]) Q=int(readline()) for _ in range(Q): data=map(int,readline().split()) if next(data)==0: X,Y,Z=data X-=1 Y-=1 for a,b in G.HLD(X,Y): if a>b: a,b=b,a LST.Operate_Range(Z,a,b) else: X,Y=data ans=0 X-=1 Y-=1 for a,b in G.HLD(X,Y): if a>b: a,b=b,a ans+=LST.Fold(a,b)[0] ans%=mod print(ans)