def rec_str(a):return"".join(["[",", ".join(rec_str(x)for x in a),"]"])if isinstance(a,list)else str(a) class BIT: def __init__(self,x): if isinstance(x,int): self.N=x self.F=[0]*(x+1) else: self.N=len(x) self.F=[0]*(self.N+1) for j in R(1,self.N+1): i=j-1 self.F[j]=x[i] k=j-(j&-j) while i>k: self.F[j]+=self.F[i] i-=i&-i def copy(self): a=__class__([]) a.N=self.N a.F=self.F[:] return a def Add(self,i,u): i+=1 while i<=self.N: self.F[i]+=u i+=i&-i def Set(self,i,u):self.Add(i,u-self.Get(i)) def Get(self,i):return self.IntervalSum(i,i) def InitialSegmentSum(self,r): assert(r>-2) a=0 i=min(r+1,self.N) while i: a+=self.F[i] i-=i&-i return a def IntervalSum(self,l,r):return self.InitialSegmentSum(r)-self.InitialSegmentSum(l-1) def list(self):return[self.Get(i)for i in R(self.N)] def __str__(self):return rec_str(self.list()) def Search(self,u):#Computing minimum of j such that InitialSegmentSum(j)>=u or j==N j=s=n=0 p=1<<17 #131072 while p: k=j|p p>>=1 if k<=self.N: n+=self.F[k] if n0 def InitialSegmentSize(self,i_final):return self.bit.InitialSegmentSum(i_final) def IntervalSize(self,i_start,i_final):returnself.bit.IntervalSum(i_start,i_final) def empty(self):return self.InitialSegmentSize(self.ubound)<1 def MaximumLeq(self,i):#Returning -1 if not exists num=self.InitialSegmentSize(i) def f(sum,j):return i<=j or num<=sum a=self.Search(f) return a if self.find(a)else -1 def MaximumLt(self,i):return self.MaximumLeq(i-1) def MinimumGeq(self,i):return self.MinimumGt(i-1) def MinimumGt(self,i):#Returning ubound+1 if not exists num=self.InitialSegmentSize(i) def f(sum,j):return i>=1 if k<=self.bit.N: n+=self.bit.F[k] if f(n,k-1):n=s else:s,j=n,k return j class CompressedLineSubset: def __init__(self,X): self.sorted_list=sorted(set(X)) N=len(self.sorted_list) self.compressed_coordinate={self.sorted_list[i]:i for i in R(N)} self.nnls=NonNegativeLineSubset(N-1) def copy(self): a=__class__([]) a.sorted_list=self.sorted_list[:] a.compressed_coordinate=self.copy() a.nnls=self.nnls.copy() return a def insert(self,i):self.nnls.insert(self.compressed_coordinate[i]) def erase(self,i):self.nnls.erase(self.compressed_coordinate[i]) def count(self,i):return self.nnls.count(self.compressed_coordinate[i])if i in self.compressed_coordinate else 0 def find(self,i):return self.count(i)>0 def InitialSegmentSize(self,i_final):return self.nnls.InitialSegmentSize(self.compressed_coordinate[i_final]) def IntervalSize(self,i_start,i_final):return self.nnls.IntervalSum(self.compressed_coordinate[i_start],self.compressed_coordinate[i_final]) def empty(self):return self.InitialSegmentSize(self.nnls.bit.N-1)<1 def MaximumLeq(self,i): t=self.nnls.MaximumLeq(self.compressed_coordinate[i]) return self.sorted_list[t]if t>=0 else None def MaximumLt(self,i): t=self.nnls.MaximumLt(self.compressed_coordinate[i]) return self.sorted_list[t]if t>=0 else None def MinimumGeq(self,i): t=self.nnls.MinimumGeq(self.compressed_coordinate[i]) return self.sorted_list[t]if t