Bzoj2957, bzoj2957 line segment tree
Consider the line segment tree. For a range, record its maximum slope and the answer that only considers the constraints within the range.
When merging, the answer to a range is the answer to the left subinterval plus the answer to the right subinterval considering the sum of the Left subinterval constraints.
When an answer with an interval constraint of h is obtained, the relationship between the left subinterval and h is determined. If the value is not greater than h, the answer is the answer whose right subinterval is restricted to h. Otherwise, the left subinterval is recursive.
Time complexity O (nlog2n)
Code:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 using namespace std; 7 #define N 100010 8 double c[N<<2]; 9 int i,j,k,n,m,a[N<<2],x,y,l,r,Mid,Ans=1;10 inline double Max(double x,double y){return x<y?y:x;}11 inline int Calc(int Node,int l,int r,double h){12 if(l==r)return c[Node]>h;13 int Mid=l+r>>1;14 if(h>=c[Node<<1])return Calc(Node<<1|1,Mid+1,r,h);15 return Calc(Node<<1,l,Mid,h)+a[Node]-a[Node<<1];16 }17 inline void Update(int Node,int l,int r,int x,double y){18 if(l==r){c[Node]=y;a[Node]=1;return;}19 int Mid=l+r>>1;20 if(Mid>=x)Update(Node<<1,l,Mid,x,y);else Update(Node<<1|1,Mid+1,r,x,y);21 c[Node]=Max(c[Node<<1],c[Node<<1|1]);22 a[Node]=a[Node<<1]+Calc(Node<<1|1,Mid+1,r,c[Node<<1]);23 }24 int main(){25 scanf("%d%d",&n,&m);26 for(i=1;i<=m;i++){27 scanf("%d%d",&x,&y);28 Update(1,1,n,x,(double)y/x);29 printf("%d\n",a[1]);30 }31 return 0;32 }Bzoj2957