N-PICTURE-POJ 1177 (scan line for perimeter)

Source: Internet
Author: User

Test instructions: For the perimeter, the rectangle is first fused to the perimeter, including the inner perimeter analysis: Just look at the time feeling will be tricky, let people start, but after learning the scan line believe it is very simple bar (scan line template-), or do not say, the following is an accurate picture, you can take to debug the data

*************************************************************************************************************** **

#include <stdio.h>
#include <math.h>
#include <algorithm>
usingnamespaceStd

#defineLson r<<1
#defineRson r<<1|1

Const intMAXN = 2e5+5;
ConstintOO = 1e9+7;

structStgmenttree
{///Len saves the length of the edge contained in the interval, the number of times the save interval is cover, and how many unconnected interval segments the sum holds
intL, R, Len, cover, sum;
BOOLLB, RB;///whether the left and right are covered
intMid () {return(r+l) >>1;}
}a[maxn<<2];
///dir equals 1 to the left, 1 to the right and the right to the left .
structpoint{intX, y1, y2, dir;} EGE[MAXN];
intHASH[MAXN], NH;///Save the discretized data, NH represents the number of elements

BOOLCMP (point N1, point N2)
{///sort the edges by the value of x from left to right
returnn1.x < n2.x;
}
///find the length of an edge, X1< X2
int Findseglen (int x1, int x2)
{///use subscript to retrieve values directly
returnHASH[X2]-hash[x1];
}
voidBuildtree (intRintLintR
{
A[R]. L = L, A[r]. R = r, a[r].cover=0;

if(L = = R1)return;

Buildtree (Lson, L, A[r].mid ());
Buildtree (Rson, A[r].mid (), R);
}
voidPushup (intR///merge Operations
{///First notice whether this interval is overwritten
if(A[r].cover! =0)
{
A[r].len= Findseglen (A[r]. L, A[r]. R);
A[r].sum = a[r].lb = A[R].RB =1;
}
Else if(A[r]. L = = A[r]. R1)
{
A[r].len =0;
A[r].sum = a[r].lb = A[R].RB =0;
}
Else
{///If this interval is not covered and is not a leaf node, then it is equal to the sum of the sub-interval
A[r].len = A[lson].len + A[rson].len;

a[r].lb = a[lson].lb, a[r].rb = a[rson].rb;
A[r].sum = A[lson].sum + A[rson].sum-(a[lson].rb & a[rson].lb);
}

}
voidUpdata (intRintLintRintDir
{
if(A[r]. L = = L && a[r]. R = = r)
{
A[r].cover + = dir;
Pushup (R);

return;
}

if(R <= a[r].mid ())
Updata (Lson, L, R, dir);
Elseif(L >= a[r].mid ())
Updata (Rson, L, R, dir);
Else
{
Updata (Lson, L, A[r].mid (), dir);
Updata (Rson, A[r].mid (), R, dir);
}

Pushup (R);
}

intMain ()
{
intN

while(SCANF ("%d", &n)! = EOF)
{
intI, X1, x2, y1, y2, k=0, c=0, pre=0; NH =0;

for(i=0; i<n; i++)
{
scanf"%d%d%d%d", &x1, &y1, &x2, &y2);
Ege[k].x=x1, Ege[k].y1=y1, Ege[k].y2=y2, ege[k++].dir=1;///Left y
EGE[K].X=X2, Ege[k].y1=y1, Ege[k].y2=y2, ege[k++].dir=-1;///Right y
hash[nh++] = y1, hash[nh++] = y2;
}

Sort (Hash, HASH+NH);///sort to repeat
NH = unique (hash, HASH+NH)-hash;
///The discretized data is started from 0 subscript.
Buildtree (1,0, nh-1);

///Sort by x on edge
Sort (Ege, ege+k, CMP);

for(i=0; i<k-1; i++)
{
intL = Lower_bound (hash, HASH+NH, ege[i].y1)-hash;
intR = Lower_bound (hash, HASH+NH, ege[i].y2)-hash;

Updata (1, L, R, Ege[i].dir);

C + = Fabs (a[1].len-pre) + (ege[i+1].x-ege[i].x) *a[1].sum *2;
Pre = a[1].len;
}

printf"%d\n", C+pre);
}

return0; 

}

N-PICTURE-POJ 1177 (scan line for perimeter)

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.