import sys readline=sys.stdin.readline class Li_Chao_Tree: def __init__(self,N,X=None,inf=float("inf")): self.N=N if X==None: self.X=[x for x in range(self.N)] else: self.X=X self.idx={x:i for i,x in enumerate(self.X)} self.inf=inf self.li_chao_tree=[(0,self.inf)]*(2*self.N) self.left,self.right=[None]*2*N,[None]*2*N for i in range(self.N,2*self.N): self.left[i]=self.X[i-self.N] self.right[i]=self.X[i-self.N] for i in range(self.N-1,0,-1): self.left[i]=self.left[i<<1] self.right[i]=self.right[i<<1|1] def add_line_one_segment(self,a,b,i): queue=[i] while queue: i=queue.pop() aa,bb=self.li_chao_tree[i] l=a*self.X[self.left[i]]+b r=a*self.X[self.right[i]]+b ll=aa*self.X[self.left[i]]+bb rr=aa*self.X[self.right[i]]+bb if ll<=l and rr<=r: continue if l<=ll and r<=rr: self.li_chao_tree[i]=(a,b) continue queue.append(i<<1) queue.append(i<<1|1) def add_line(self,a,b,L=None,R=None): if L==None: L=self.N else: L+=self.N if R==None: R=2*self.N else: R+=self.N while L>=1 R>>=1 def __call__(self,x): i=self.idx[x]+self.N retu=self.inf while i: a,b=self.li_chao_tree[i] retu=min(retu,a*x+b) i>>=1 return retu def __getitem__(self,i): x=self.X[i] i+=self.N retu=self.inf while i: a,b=self.li_chao_tree[i] retu=min(retu,a*x+b) i>>=1 return retu def __str__(self): li_chao_tree=[(0,inf)]*self.N for i,x in enumerate(self.X): ii=i+self.N while ii: aa,bb=self.li_chao_tree[ii] a,b=li_chao_tree[i] if aa*x+bb>=1 return "["+", ".join(map(str,li_chao_tree))+"]" def Z_algorithm(lst): le=len(lst) LCP=[None]*le LCP[0]=le L,R=0,0 for i in range(1,le): if i