hdu1542 of the Matrix (segment tree + scan line)

Source: Internet
Author: User

To seek the matrix and, that is, to require all the area. That's OK. The total graph is cut according to the Matrix. make it a piece of a piece.

The input is expressed in coordinates, where the scan line is scanned from bottom to top. Initially let the following side is 1, the top is-1;

Use one first to start with the scan below. The update segment tree is encountered, and the change is removed by adding the edge to-1 o'clock.

This allows the length of the segment to be scanned from the bottom up to the previous scan. From the bottom to the top of the process, once encountered an edge, then calculate his height.

Height * length is the area.

/*The length of the leaf node [l,l] does not become 0, obviously this is the problem of each node of the segment tree represents an interval, [L,r] The interval represents the length of lx[r+1]-lx[l] 1___2___3___4___5 discrete condition 1 2 3 4 Each node in a segment tree*/#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>using namespacestd;#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1#defineMAXN 1005Doublex[maxn<<2];structseg{Doublel,r,h; intF;} S[MAXN<<1];structnode{intCnt//CNT Indicates whether the interval is fully covered if cnt==1 indicates that it is completely overwritten once//Cnt=0 is represented as being completely overwritten but does not mean not covered, cnt>1 means that it is completely overwritten by multiple times    DoubleLen;} TREE[MAXN*8];BOOLcmp (seg a,seg b) {returna.h<b.h;}intFindDoubleValintLintR) {    intleft=l,right=R; intmid;  while(left<=Right ) {Mid= (left+right)/2; if(x[mid]==val)returnmid; Else if(x[mid]>val) right=mid-1; ElseLeft=mid+1; }    return-1;}voidBuildintLintRintRT) {    if(l==r) {tree[rt].cnt=0; Tree[rt].len=0; return ; }    intM= (L+R)/2;    Build (Lson); Build (Rson);}voidGetlen (intRtintLintR) {    if(tree[rt].cnt)//if the whole paragraph is covered, the length is calculated directly{Tree[rt].len=x[r+1]-X[l]; }    Else if(L==R)//leaf nodetree[rt].len=0; Else //not the leaf, but not the whole paragraph, obtained from the son nodetree[rt].len=tree[rt<<1].len+tree[rt<<1|1].len;}voidUpdata (intLintRintCintLintRintRT) {    if(l>=l&&r>=r) {tree[rt].cnt+=C;        Getlen (RT,L,R); return ; }    intM= (L+R)/2; if(m>=L) Updata (L,r,c,lson); if(r>m) updata (L,r,c,rson); Getlen (rt,l,r);}intMain () {intn,m,t,i,j,ff=0; DoubleX1,x2,y1,y2;  while(SCANF ("%d", &n)! =EOF) {        if(!N) Break; M=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; //Lower Borders[m+1].l=x1;s[m+1].r=x2;s[m+1].h=y2;s[m+1].f=-1; //Upper Borderx[m]=X1; X[m+1]=x2; M=m+2; } sort (S,s+m,cmp); Sort (x,x+m); intk=1;  for(i=1; i<m;i++)//to repeat        {            if(x[i]!=x[i-1]) X[k++]=X[i]; } Build (0, K-1,1); Doubleans=0;  for(i=0; i<m;i++)        {            intLl=find (S[I].L,0, K-1);//two points to find the location            intRr=find (S[I].R,0, K-1)-1;//because this represents a line segment that is not a point. Updata (LL,RR,S[I].F,0, K-1,1); Ans+ = (s[i+1].H-S[I].H) *tree[1].len; } printf ("Test Case #%d\n",++ff); printf ("Total explored area:%.2lf\n\n", ans); }}

hdu1542 of the Matrix (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.