HDU 1542 Atlantis (line segment tree)

Source: Internet
Author: User
Atlanta tistime limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others) Total submission (s): 6899 accepted submission (s): 3022

Problem descriptionthere are several generated ent Greek texts that contain descriptions of the fabled island Atlanta. some of these texts even include maps of parts of the island. but unfortunately, these maps describe different regions of Atlanta. your friend Bill has to know the total area for which maps exist. you (unwisely) volunteered to write a program that calculates this quantity.
 
Inputthe input file consists of several test cases. each test case 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 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) are 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, your program shocould output one section. the first line of each section must be "Test Case # K", where k is the number of the test case (starting with 1 ). the second one must be "total occupied ed area: a", where A is the total occupied ed area (I. e. the area of the Union of all rectangles in this test case), printed exact to two 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
Test case #1Total explored area: 180.00 
Given the coordinates in the lower left corner of a rectangle and the coordinates in the upper right corner are: (x1, Y1), (X2, Y2). For such a rectangle, we construct Two Line SegmentsOne line is located at X1, and its range in Y coordinate is [Y1, y2], and the value of a cover domain is given to 1; the other line segment is located at X2, the interval is the same as [Y1, y2], and a cover value is-1. Based on this method, two line segments are constructed for each rectangle, and all line segments are sorted from left to right based on the located X. At the beginning, the cover value on the Line Segment tree was 0, but after the first line segment (x = 0) was inserted into the line segment tree, we add the cover of the line segment to the cover of the Line Segment tree. In this case, the cover value on the Line Segment tree is 1, insert the second line segment (x = 1) next time. At this time, we can find that the cover of some line segments is 0 and the cover of some line segments is 1. Observe carefully, but when inserting the second line segment, if the cover on the Line Segment tree is already 1, does it constitute a parallel area between the second line segment to be inserted? That is to say, when we insert a line segment, we only need to check whether the cover between the region of the line segment is greater than or equal to 1. If yes, then you can add the area value (the X position of the current line segment-The X position of the previous line segment) * (the size of the range)
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;const int maxn=110;int n,index=0;double x1,x2,y1,y2,y[2*maxn];struct LINE{    double x,y_down,y_up;    int flag;    bool operator<(const LINE &a)const    {        return x<a.x;    }}line[2*maxn];struct TREE{    double y_down,y_up,x;    int cover;    bool flag;}tree[1000*maxn];void build(int i,int l,int r){    tree[i].x=-1;    tree[i].cover=0;    tree[i].y_down=y[l];    tree[i].y_up=y[r];    tree[i].flag=false;    if(l+1==r)    {        tree[i].flag=true;        return ;    }    int mid=(l+r)>>1;    build(2*i,l,mid);    build(2*i+1,mid,r);}double insert(int i,double x,double l,double r,int flag){    if(r<=tree[i].y_down||l>=tree[i].y_up)        return 0;    if(tree[i].flag)    {        if(tree[i].cover>0)        {            double temp_x=tree[i].x;            double ans=(x-temp_x)*(tree[i].y_up-tree[i].y_down);            tree[i].x=x;            tree[i].cover+=flag;            return ans;        }        else        {            tree[i].cover+=flag;            tree[i].x=x;            return 0;        }    }    double ans1,ans2;    ans1=insert(2*i,x,l,r,flag);    ans2=insert(2*i+1,x,l,r,flag);    return ans1+ans2;}int main(){    int count=0;    while(scanf("%d",&n)!=EOF&&n)    {        index=1;        for(int i=1;i<=n;i++)        {            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);            y[index]=y1;            line[index].x=x1;            line[index].y_down=y1;            line[index].y_up=y2;            line[index].flag=1;            index++;            y[index]=y2;            line[index].x=x2;            line[index].y_down=y1;            line[index].y_up=y2;            line[index].flag=-1;            index++;        }        sort(&y[1],&y[index]);        sort(&line[1],&line[index]);        build(1,1,index-1);        double ans=0;        for(int i=1;i<index;i++)            ans+=insert(1,line[i].x,line[i].y_down,line[i].y_up,line[i].flag);        printf("Test case #%d\nTotal explored area: %.2f\n\n",++count,ans);    }    return 0;}


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.