結果
問題 | No.230 Splarraay スプラレェーイ |
ユーザー | vwxyz |
提出日時 | 2022-03-23 18:30:45 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,329 ms / 5,000 ms |
コード長 | 9,171 bytes |
コンパイル時間 | 843 ms |
コンパイル使用メモリ | 82,348 KB |
実行使用メモリ | 140,732 KB |
最終ジャッジ日時 | 2024-10-11 21:39:03 |
合計ジャッジ時間 | 10,387 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 40 ms
53,888 KB |
testcase_01 | AC | 41 ms
54,272 KB |
testcase_02 | AC | 42 ms
53,888 KB |
testcase_03 | AC | 42 ms
53,888 KB |
testcase_04 | AC | 41 ms
53,888 KB |
testcase_05 | AC | 134 ms
76,956 KB |
testcase_06 | AC | 325 ms
81,532 KB |
testcase_07 | AC | 136 ms
76,640 KB |
testcase_08 | AC | 205 ms
78,172 KB |
testcase_09 | AC | 756 ms
105,464 KB |
testcase_10 | AC | 663 ms
83,288 KB |
testcase_11 | AC | 597 ms
95,044 KB |
testcase_12 | AC | 779 ms
104,352 KB |
testcase_13 | AC | 368 ms
82,168 KB |
testcase_14 | AC | 297 ms
92,108 KB |
testcase_15 | AC | 721 ms
95,332 KB |
testcase_16 | AC | 905 ms
119,620 KB |
testcase_17 | AC | 1,329 ms
140,732 KB |
testcase_18 | AC | 516 ms
104,676 KB |
testcase_19 | AC | 967 ms
125,788 KB |
ソースコード
import sys readline=sys.stdin.readline 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 while True: while L%2==0: L>>=1 self.Propagate_Above(L) vv=self.f(v,self.Operate_At(L)) if f(vv): v=vv L+=1 else: while L<self.N: L<<=1 self.Propagate_Above(L) 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 while True: R-=1 while R>1 and R%2: R>>=1 self.Propagate_Above(R) vv=self.f(self.Operate_At(R),v) if f(vv): v=vv else: while R<self.N: R=2*R+1 self.Propagate_Above(R) 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()) Q=int(readline()) def f(tpl0,tpl1): x0,y0,c0=tpl0 x1,y1,c1=tpl1 return (x0+x1,y0+y1,c0+c1) e=(0,0,0) def f_act(a,b): if b==None: return a return b e_act=None def operate(tpl,a): if a==None: return tpl x,y,c=tpl if a==0: return (c,0,c) elif a==1: return (0,c,c) LST=Lazy_Segment_Tree(N,f,e,f_act,e_act,operate,[(0,0,1)]*N) A,B=0,0 for _ in range(Q): x,l,r=map(int,readline().split()) r+=1 if x==0: a,b,c=LST.Fold(l,r) if a>b: A+=a elif a<b: B+=b elif x==1: LST.Operate_Range(0,l,r) else: LST.Operate_Range(1,l,r) a,b,c=LST.Fold() A+=a B+=b print(A,B)