Poj1151 -- Atlanta (line segment tree + discretization + scanning line)

Source: Internet
Author: User
Atlanta Time limit:1000 ms Memory limit:10000kb 64bit Io format:% I64d & % i64usubmit status

 

Description

There are several except 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.

Input

The input 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.

Output

For 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 

I wrote the code to save trouble. I wrote the recursion in update without thinking about it. I found the error for one afternoon before I knew that the position was wrong...

Second bullet of scanning line

The bottom left and top right coordinates of N rectangles are given.

1. if the left subtree is (L, mid) and the right subtree is (MID, R), it cannot be Mid + 1, in this way, the segments between mid + 1 and mid are not counted.

2. X is scanned from left to right. Because the given number is a real number, in addition to discretization of X points, the left and right intervals of each segment in the line segment tree should be updated to the real numbers Y1, y2 ., through the comparison of the interval size, you can determine whether to directly obtain the value or continue to deepen. In the next layer, you need to change the interval to be judged so that the interval is within the control of the node.

3. the lazy array marks the number of occurrences of this line segment. If the value is 0, the node is equal to the sum of the left and right word nodes. Otherwise, the node is the maximum value.

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define maxn 300struct node1{    double l , r ;    double sum ;}cl[maxn<<3];int lazy[maxn<<3] ;struct node2{    double x , y1 , y2 ;    int flag ;}p[maxn<<3];double s[maxn<<3] ;bool cmp(node2 a,node2 b){    return a.x < b.x ;}void push_up(int rt){    if( lazy[rt] > 0 )        cl[rt].sum = cl[rt].r - cl[rt].l ;    else        cl[rt].sum = cl[rt*2].sum + cl[rt*2+1].sum ;}void creat(int rt,int l,int r){    if( r - l > 1 )    {        cl[rt].l = s[l] ;        cl[rt].r = s[r] ;        creat(rt*2,l,(l+r)/2);        creat(rt*2+1,(l+r)/2,r);        push_up(rt);    }    else    {        cl[rt].l = s[l] ;        cl[rt].r = s[r] ;        cl[rt].sum = 0 ;    }    return ;}void update(int rt,double y1,double y2,int flag){    if( cl[rt].l == y1 && cl[rt].r == y2 )    {        lazy[rt] += flag ;        push_up(rt);        return ;    }    else    {        if( cl[rt*2].r > y1 )            update(rt*2,y1,min(cl[rt*2].r,y2),flag);        if( cl[rt*2+1].l < y2 )            update(rt*2+1,max(cl[rt*2+1].l,y1),y2,flag);        push_up(rt);    }}int main(){    int temp = 1 , n , i , j ;    double x1 , y1 , x2 , y2 , ans ;    while(scanf("%d", &n) && n)    {        ans = 0 ;        for(i = 0 ; i < n ; i++)        {            scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);            p[i].x = x1 ;            p[i].y1 = y1 ;            p[i].y2 = y2 ;            p[i].flag = 1 ;            p[i+n].x = x2 ;            p[i+n].y1 = y1 ;            p[i+n].y2 = y2 ;            p[i+n].flag = -1 ;            s[i+1] = y1 ;            s[i+n+1] = y2 ;        }        sort(s+1,s+(2*n+1));        sort(p,p+2*n,cmp);        creat(1,1,2*n);        memset(lazy,0,sizeof(lazy));        update(1,p[0].y1,p[0].y2,p[0].flag);        for(i = 1 ; i < 2*n ; i++)        {            ans += ( p[i].x-p[i-1].x )*cl[1].sum ;            update(1,p[i].y1,p[i].y2,p[i].flag);        }        printf("Test case #%d\nTotal explored area: %.2lf\n\n", temp++, 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.