HDU 1542 Atlantis (segment Tree & scan line & area and)

Source: Internet
Author: User

AtlantisTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 6386 Accepted Submission (s): 2814


Problem Descriptionthere is several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to the write a program that calculates this quantity.

Inputthe input file consists of several test cases. Each test case is starts with a line containing a single integer n (1<=n<=100) of available maps. The n following lines describe one map each. Each of the these lines contains four numbers x1;y1;x2;y2 (0<=x1<x2<=100000;0<=y1<y2<=100000), not necessarily integers. The values (x1; y1) and (x2;y2) is the coordinates of the Top-left resp. Bottom-right corner of the mapped area.

The input file is terminated by a line containing a single 0. Don ' t process it.
Outputfor Each test case, the your program should the output one section. The first line of all sections must be ' test Case #k ', where k is the number of the the ' Test case ' (starting with 1). The second one must be ' total explored area:a ', where A is the ' total explored area ' i.e. the area of the the Union of all Rec Tangles in this test case), printed exact to both digits to the right of the decimal point.

Output a blank line after each test case.

Sample Input
210 10 20 2015 15 25 25.50

Sample Output

Sourcemid-central European Regional Contest 2000
Recommendlinle | We have carefully selected several similar problems for you:1828 1255 1823

pid=1543 "target=" _blank ">1543 3333 Test instructions: gives you some rectangles and axes that are parallel to the axis. Tell you their coordinates in the lower left and upper right corner. Ask you now. The area where the entire coordinate plane is covered by them. Train of thought: because the line is directly looking for the topic of tree scan lines. So I didn't think of any other ideas. Tell me why you can do it with a scan line. The first is the solution of the area.

area = Bottom length * high. This bottom length does not necessarily require continuous. So let's say we know the length and the bottom of a certain moment can be calculated as an area. The line tree can be finished to find out the length of the bottom of a certain time (the x-axis is covered).

So we just need to sort all the parallel and x-axis edges by height. Then insert into the segment tree in turn. The bottom of the rectangle is encountered and added to the line segment tree. When you encounter the top, remove the corresponding bottom from the segment tree. The text may not be very well understood. Look at the drawing and you'll know.

See the code for details:

#include <algorithm> #include <iostream> #include <string.h> #include <stdio.h>using namespace std;const int inf=0x3f3f3f3f;const int maxn=250; #define Lson l,mid,ls#define Rson mid+1,r,rsint n,m;int cov[maxn<<    2];d ouble len[maxn<<2],h[maxn];struct node{double x1,x2,h;    int V; Node (double a=0,double b=0,double c=0,int d=0): X1 (a), X2 (b), H (c), V (d) {}} Seg[maxn];bool cmp (node A,node b) {return A.H&L T;b.h;}    void Init () {sort (h,h+m); M=unique (h,h+m)-H;} int Hash (double x) {return lower_bound (h,h+m,x)-H;}    void pushup (int l,int r,int RT) {if (Cov[rt])//marked with a positive whole block covered with len[rt]=h[r+1]-h[l]; else if (l==r)//There is no son left or right.

len[rt]=0; else//not covered by the whole block but partially covered len[rt]=len[rt<<1]+len[rt<<1|1];} void update (int l,int r,int rt,int l,int r,int D) {if (l<=l&&r<=r) {cov[rt]+=d; Pushup (L,R,RT); Return }//Such a mark is not to be transmitted. Because you did not delete a top. There must be one below that corresponds to it.

int mid= (L+R) >>1,ls=rt<<1,rs=ls|1; if (l<=mid) update (LSON,L,R,D); if (r>mid) update (RSON,L,R,D); Pushup (L,R,RT);} int main () {int cas=1,i,ptr; Double X1,x2,y1,y2,ans; while (scanf ("%d", &n), N) {printf ("Test case #%d\n", cas++); for (i=ptr=0;i<n;i++) {scanf ("%lf%lf%lf%lf", &x1,&y1,&x2,&y2); h[ptr]=x1; Seg[ptr++]=node (x1,x2,y1,1); h[ptr]=x2; Seg[ptr++]=node (x1,x2,y2,-1); } m=ptr,ans=0; Init (); Sort (seg,seg+ptr,cmp); memset (len,0,sizeof len); memset (cov,0,sizeof CoV); for (i=0,ptr--;i<ptr;i++) {update (0,m-1,1,hash (seg[i].x1), Hash (seg[i].x2) -1,seg[i].v),//m node M-1 line segment ans+= (seg[i+1].h-seg[i].h) *len[1]; } printf ("Total explored area:%.2lf\n\n", ans); } return 0;}



Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

HDU 1542 Atlantis (segment Tree &amp; scan line &amp; area and)

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.