Hdu1828 calculate perimeter and line segment tree + discretization

Source: Internet
Author: User


Picture

Time Limit: 6000/2000 MS
(Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 415 accepted submission (s): 237


Problem description

A number
Rectangular posters, photographs and other pictures of the same shape are pasted
On a wall. Their sides are all vertical or horizontal. Each rectangle can be
Partially or totally covered by the others. The length of the boundary of
Union of all rectangles is called the perimeter.

Write a program to calculate the perimeter. An example with 7 rectangles is
Shown in Figure 1.

The corresponding boundary is the whole set of line segments drawn in Figure 2.

The vertices of all rectangles have integer coordinates.

 

 

Input

Your program is to read from standard input. The first
Line contains the number of rectangles pasted on the wall. In each of the subsequent
Lines, one can find the integer coordinates of the lower left vertex and
Upper right vertex of each rectangle. The values of those coordinates are given
As ordered pairs consisting of an X-coordinate followed by a Y-coordinate.

0 <= number of rectangles <5000
All coordinates are in the range [-keys, 10000] and any existing rectangle has
A positive area.

Please process to the end of file.

 

 

Output

Your program is to write to standard output. The output
Must contain a single line with a non-negative integer which corresponds to
Perimeter for the input rectangles.

 

 

Sample Input

7

-15 0 5 10

-5 8 20 25

15-4 24 14

0-6 16 4

2 15 10 22

30 10 36 20

34 0 40 16

 

 

Sample output

228

 

 

Discretization
+
Line Segment tree
.

When reading data, record the coordinates of the two sides of the vertical edge
X, Y1, Y2
And
Y
Coordinates on the Axis
Tempy [],
Pair
Tempy
Sort from small to large
,
Remove duplicate
Y
Axis coordinates are converted to an array
Y [],
Exploitation
Y []
Data pair
Y
Create a line segment tree
,
Then sort the vertical edges.
,
Scanning from left to right
,
Based on the scanned vertical side
Y
Maintain and update the line segment tree in the axis coverage Interval
,
Total length of statistical coverage
,
Record every update
Y
Total length of the Axis covered
,
After each update
Y
The variation of the total length of the Axis overwrite is the length of the circumference of the contour.
.
Add
X
Length added on the Axis
(
If
Y
If the axis is overwritten
,
This can be determined by the root node of the Line Segment tree.
),
Until the scan ends
.

 

The Code is as follows:
:

 

# Include <stdio. h> <br/> # include <string. h> <br/> # include <algorithm> <br/> Using STD: sort; <br/> # define maxn 5005 <br/> struct st <br/> {<br/> int LC, RC; <br/> int L, R; // left and right endpoints, corresponding to the subscript of array y [] <br/> int cov, covn; // determines whether the COV is completely overwritten, and covn determines the number of overwrites <br/> int covlen; // overwrite length <br/>} st [3 * maxn]; <br/> struct line <br/> {<br/> int X, Y1, Y2, flag; <br/> bool operator <(line const & A) const <br/>{< br/> If (x <. x) return 1; <br /> Else if (x =. x) return flag>. flag; <br/> return 0; <br/>}< br/>} line [3 * maxn]; <br/> int y [maxn]; <br/> void build (int l, int R, int N) <br/>{< br/> st [N]. L = Y [l]; <br/> st [N]. R = Y [R]; <br/> st [N]. cov = sT [N]. covlen = sT [N]. covn = sT [N]. lc = sT [N]. rc = 0; <br/> st [2 * n]. cov = sT [2 * n]. covlen = sT [2 * n]. covn = sT [2 * n]. lc = sT [2 * n]. rc = 0; <br/> st [2 * n + 1]. cov = sT [2 * n + 1]. covlen = sT [2 * n + 1]. covn = sT [2 * n + 1]. lc = sT [2 * n + 1]. rc = 0; <B R/> If (R-l = 1) return; <br/> int mid = (L + r)/2; <br/> build (L, mid, 2 * n); <br/> build (MID, R, 2 * n + 1); <br/>}< br/> void Update (int n, line) <br/>{< br/> If (ST [N]. L = line. y1) ST [N]. lc + = line. flag; <br/> If (ST [N]. R = line. y2) ST [N]. RC + = line. flag; <br/> If (ST [N]. L = line. y1 & St [N]. R = line. y2) ST [N]. cov + = line. flag; <br/> else <br/> {<br/> If (line. y2 <= sT [2 * n]. r) update (2 * n, line); <br/> else if (line. Y1> = sT [2 * n + 1]. l) update (2 * n + 1, line); <br/> else <br/>{< br/> line L1 = line, L2 = line; <br/> l1.y2 = sT [N * 2]. r; <br/> l2.y1 = sT [2 * n + 1]. l; <br/> Update (2 * n, L1); <br/> Update (2 * n + 1, L2 ); <br/>}< br/> If (ST [N]. cov = 1) <br/>{< br/> st [N]. covlen = sT [N]. r-ST [N]. l; <br/>}< br/> else <br/>{< br/> st [N]. covlen = sT [2 * n]. covlen + st [2 * n + 1]. covlen; <br/>}< br/> st [N]. covn + = line. flag; <br/>}< br/> int main () <Br/>{< br/> int N, I, xs, X1, Y1, X2, Y2, T, Tempy [maxn]; <br/> while (scanf ("% d", & N )! = EOF) <br/>{< br/> xs = 1; <br/> for (I = 1; I <= N; ++ I) <br/>{< br/> scanf ("% d", & X1, & Y1, & X2, & Y2 ); <br/> line [Xs]. X = x1; <br/> line [Xs]. y1 = Y1; <br/> line [Xs]. y2 = Y2; <br/> line [Xs]. flag = 1; <br/> line [Xs + 1]. X = x2; <br/> line [Xs + 1]. y1 = Y1; <br/> line [Xs + 1]. y2 = Y2; <br/> line [Xs + 1]. flag =-1; <br/> Tempy [Xs] = Y1; <br/> Tempy [Xs + 1] = Y2; <br/> Xs + = 2; <br/>}< br/> XS-= 2; <br/> sort (LINE + 1, line + Xs + 1); <br/> S ORT (Tempy + 1, Tempy + Xs + 1); <br/> T = 0; <br/> Y [++ T] = Tempy [1]; <br/> Y [0] =-2100000000; <br/> for (I = 2; I <= Xs + 1; ++ I) <br/>{< br/> If (Tempy [I]! = Y [T]) y [++ T] = Tempy [I]; // remove the same Y axis coordinates <br/> else continue; <br/>}< br/> build (1, t, 1); <br/> int lastlen = 0, Res = 0 ;; <br/> for (I = 1; I <= Xs + 1; ++ I) <br/> {<br/> If (ST [1]. covn) RES + = (line [I]. x-line [I-1]. x) * 2; // update the length added to the X axis <br/> Update (1, line [I]); // update the line segment tree <br/> res + = ABS (lastlen-ST [1]. covlen); // update the length of the Y axis. <br/> lastlen = sT [1]. covlen; <br/>}< br/> printf ("% d/N", Res); <br/>}< br/> return 0; <br/>}< br/>

 

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.