Bzoj2957, bzoj2957 line segment tree

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.