# https://github.com/shakayami/ACL-for-python/wiki/lazysegtree class lazy_segtree(): ''' T = lazy_segtree(V,OP,E,MAPPING,COMPOSITION,ID) V:初期リスト OP:要素同士の作用(G*G->G) E:OPにおける単位元 ※OP(data, E) = data MAPPING:要素にapplyさせる写像(F*G->G) COMPOSITION:写像の合成(F*F->F) ID:恒等写像 ※mapping(data, ID) = data F:写像fの集合 G:X=(x[l],...,x[r-1])の集合 例) x -> min(a,x) と x[l]+...+x[r-1] op,e:要素同士を足すので op=add,e=0 map:Fa(x) = min(a,x) とするので map=min comp:Fa・Fb = Fmin(a,b) より comp=min id:F_{INF}(x) = x より id=INF T.set(i,x):i番目の要素をxに変更 T.get(i):i番目の要素を取得 T.query(l,r):[l,r)に対するクエリの結果を取得 T.apply(l,r,f):[l,r)にfを作用 T.max_right(l,f):l<=iでf=Trueとなる最大のiを取得 T.min_left(r,f):i<=rでf=Trueとなる最小のiを取得 ※ 各要素はタプルで持つとTLEするので (a,b) -> (a<<32)+b など工夫 ''' def update(self,k):self.d[k]=self.op(self.d[2*k],self.d[2*k+1]) def all_apply(self,k,f): self.d[k]=self.mapping(f,self.d[k]) if (k>i) self.d[p]=x for i in range(1,self.log+1):self.update(p>>i) def get(self,p): assert 0<=p and p>i) return self.d[p] def prod(self,l,r): assert 0<=l and l<=r and r<=self.n if l==r:return self.e l+=self.size r+=self.size for i in range(self.log,0,-1): if (((l>>i)<>i) if (((r>>i)<>i) sml,smr=self.e,self.e while(l>=1 r>>=1 return self.op(sml,smr) def all_prod(self):return self.d[1] def apply_point(self,p,f): assert 0<=p and p>i) self.d[p]=self.mapping(f,self.d[p]) for i in range(1,self.log+1):self.update(p>>i) def apply(self,l,r,f): assert 0<=l and l<=r and r<=self.n if l==r:return l+=self.size r+=self.size for i in range(self.log,0,-1): if (((l>>i)<>i) if (((r>>i)<>i) l2,r2=l,r while(l>=1 r>>=1 l,r=l2,r2 for i in range(1,self.log+1): if (((l>>i)<>i) if (((r>>i)<>i) def max_right(self,l,g): assert 0<=l and l<=self.n assert g(self.e) if l==self.n:return self.n l+=self.size for i in range(self.log,0,-1):self.push(l>>i) sm=self.e while(1): while(l%2==0):l>>=1 if not(g(self.op(sm,self.d[l]))): while(l>i) sm=self.e while(1): r-=1 while(r>1 and (r%2)):r>>=1 if not(g(self.op(self.d[r],sm))): while(r