Scanning line practice notes

 

Before scanning line and can not understand this kind of thing, really thought it was the line, still I think this thing in the end how to achieve?

Results understood, seems to really line.

 

Ok. In fact, the point is to determine a linear axis + length, straight and will determine the length of the line, but also have value.

Scan lines should be said that the axis point by the query sequence to achieve.

 

I saw a big brother blog , benefit. I read this blog came to understand the practice of scanning lines of this thing.

 

Then based on that blog, I changed the code inside, the first question I did scan line, P1904 Los Valley :

 

#include <bits / STDC ++ H.>
 the using  namespace STD;
 const  int MAXN 5E4 + = 50 ;
 int Mark [MAXN << 2 ]; // record the number of a lower base section 
int SUM [MAXN << 2 ]; // record the total length of a lower base section 
int a [MAXN]; // for discretizing h
 // to as the ordinate line (section), on the ordinate line scan 
struct P {
     int X, h, Ju; // to h scanning lines parallel sides, x is the scanning direction, ju = -1 ju = 1 to the left as the right 
    BOOL  operator <( const P & P) const { return X <  PX;}
    P () {}
    P(int xx,int hh,int juu){x=xx;h=hh;ju=juu;}
}s[maxn];
void upfather(int k,int l,int r){
    if(mark[k])sum[k]=a[r+1]-a[l];//
    else if(l==r)sum[k]=0;
    else sum[k]=sum[k<<1]+sum[k<<1|1];
}//n k
void update(int L,int R,int ju,int k,int l,int r){
    if(L<=l&&r<=R){
        mark[k]+=ju;
        upfather(k,l,r);
        return;
    }
    int mid=(l+r)>>1;
    if(L<=mid)update(L,R,ju,k<<1,l,mid);
    if(R>mid)update(L,R,ju,k<<1|1,mid+1,r);
    upfather(k,l,r);
}
int search(int key,int *x,int k){
    int l=0,r=k-1;
    while(l<=r){
        int mid=(l+r)>>1;
        if(x[mid]==key)return mid;
        if(x[mid]>key)r=mid-1;
        else l=mid+1; 
    }
    return -1;
}
struct PP{
    int x,y;
    PP(){}
    PP(int xx,int yy){x=xx;y=yy;}
}ans[maxn<<2];
int solve(int tot,int up)//tot是边数 up是点数 
{
    int upp=0,tans=0;
    for(int i=0;i<tot;i++){
        int R=search(s[i].h,a,up)-1;
        update(0,R,s[i].ju,1,0,up-1);//printf("!");
        if(sum[1]==tans)continue;
        if(ans[upp-1].x==s[i].x&&ans[upp-1].y==tans)upp--;
        else ans[upp++]=PP(s[i].x,tans);
        if(ans[upp-1].x==s[i].x&&ans[upp-1].y==sum[1])upp--;
        else ans[upp++]=PP(s[i].x,sum[1]);
        tans=sum[1];
    }
    return upp;
}
int main()
{
    int n,tot=0,l,h,r,up=0;
    a[up++]=0;
//    scanf("%d",&n);
    while(~scanf("%d%d%d",&l,&h,&r))
//    while(tot<n*2)
    {
//        scanf("%d%d%d",&l,&h,&r);
        a[up++]=h;
        s[tot++]=P(l,h,-1);
        s[tot++]=P(r,h,1);
    }
    sort(a,a+up);
    sort(s,s+tot);
    up=unique(a,a+up)-a;
    int upp=solve(tot,up);
    for(int i=0;i<upp;i++)
    {
        if(i&1)printf("%d ",ans[i].y);
        else printf("%d ",ans[i].x);
//         printf("%d% d\n",ans[i].x,ans[i].y);
    }
}

 

2019-09-04

Guess you like

Origin www.cnblogs.com/kkkek/p/11462426.html