Intersection of hdu1255 matrices (segment tree + scan line)

Source: Internet
Author: User

/*not a leaf node, and cnt=1. Note here, cnt=1 the exact meaning of what, should be, can be determined, this interval is completely covered 1 times, and has not been completely covered two times or more do not know not sure, then how to do, just add T[lch].s + T[rch].s that is, look at the left and right child interval is covered by one or more lengths, then superimposed on the parents is the parents are covered two times or more length*/#include<stdio.h>#include<string.h>#include<algorithm>using namespacestd;#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1#defineMAXN 1005structsegment{Doublel,r,h; intF;} S[MAXN*2];structnode{intCNT; DoubleLen1;//record one or more of the    DoubleLen2;//record two or more times}tree[maxn*8];Doublemark[maxn<<1];BOOLCMP (segment A,segment b) {returna.h<b.h;}voidBuildintLintRintRT) {tree[rt].cnt=0; Tree[rt].len1=tree[rt].len2=0; if(l==r) {return ; }    intM= (L+R)/2;    Build (Lson); Build (Rson);}/*Focus.     If the CNT >= of the node is 2, it indicates that the len1=len2= interval length is covered by at least two line segments.     If the node cnt = = 1, indicating that the interval is covered by a line segment, len1= interval length, as long as the left and right nodes len1 have values, then those lengths must be at least two times, so len2 for the len1 of the left and right nodes.     If the node cnt = 0, the description is not fully covered, directly with its left and right node update. Also pay attention to the special contract leaf node. */voidGetlen (intLintRintRT) {    if(tree[rt].cnt>=2)//>=2 is very good understanding, is covered 2 times or more, directly minus one minus{tree[rt].len1=mark[r+1]-Mark[l]; Tree[rt].len2=mark[r+1]-Mark[l]; }    Else if(tree[rt].cnt==1)//you need to note here//since CNT is greater than 0, it is directly obtained by covering one or more parts, and because it is completely covered once//may be covered in other parts, at this point can be based on the child, because the LEN1 record coverage once or//the above, as has been completely covered once, just add the left and right children of one or more of the value of the overlay//so that's the value of 2 or more overrides. {tree[rt].len1=mark[r+1]-MARK[L];//because the overlay once, LEN1 also to continue to add        if(L = = r)//because the overlay once, and only one l==r, so len2 is 0{tree[rt].len2=0; }        Else{tree[rt].len2=tree[rt<<1].len1+tree[rt<<1|1].len1; }    }    Else if(tree[rt].cnt==0)//for full coverage, according to the child. Similar to the above equals 1, it can be found that if it is completely overwritten//can be covered according to the right and left child, that CNT is equal to 1 o'clock, asked to cover 2 times can also//according to the child.     {        if(L = =r) Tree[rt].len1=tree[rt].len2=0; Else{tree[rt].len1=tree[rt<<1].len1+tree[rt<<1|1].len1; Tree[rt].len2=tree[rt<<1].len2+tree[rt<<1|1].len2; }    }}voidUpdata (intLintRintCintLintRintRT) {    if(l>=l&&r>=r) {tree[rt].cnt+=B;        Getlen (L,R,RT); return ; }    intM= (L+R)/2; if(m>=L) Updata (L,r,c,lson); if(r>m) updata (L,r,c,rson); Getlen (L,R,RT);}intFindDoubleValintXinty) {    intL=x,r=y,m;  while(l<=r) {m= (l+r)/2; if(mark[m]==val)returnm; Else if(mark[m]>val) r=m-1; Elsel=m+1; }    return-1;}intMain () {intT,n,i; DoubleX1,x2,y1,y2; scanf ("%d",&t);  while(t--) {scanf ("%d",&N); intm=0;  for(i=0; i<n;i++) {scanf ("%LF%LF%LF%LF",&x1,&y1,&x2,&y2); S[M].L=x1;s[m].r=x2;s[m].h=y1;s[m].f=1; mark[m++]=X1; S[M].L=x1;s[m].r=x2;s[m].h=y2;s[m].f=-1; mark[m++]=x2; } sort (S,s+m,cmp); Sort (Mark,mark+m); intk=1;  for(i=1; i<m;i++)        {            if(mark[i]!=mark[i-1]) Mark[k++]=Mark[i]; }        /*for (i=0;i<k;i++) printf ("%.2lf", Mark[i]);        printf ("\ n"); */Build (0, K-1,1); Doubleans=0;  for(i=0; i<m;i++)        {            intLl=find (S[I].L,0, K-1); intRr=find (S[I].R,0, K-1)-1; Updata (LL,RR,S[I].F,0, K-1,1); Ans+ = (s[i+1].H-S[I].H) *tree[1].len2; } printf ("%.2lf\n", ans); }}

Intersection of hdu1255 matrices (segment tree + scan line)

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.