码迷,mamicode.com
首页 > 其他好文 > 详细

矩阵重叠面积计算 线段树hdu1542

时间:2017-09-26 01:02:59      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:one   scanf   www.   计算   lun   pac   bottom   miss   should   

Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14378    Accepted Submission(s): 5931


Problem Description
There are 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 write a program that calculates this quantity.
 

 

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

 

Output
For each test case, your program should 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 explored area: a”, where a is the total explored 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
2
10 10 20 20
15 15 25 25.5 0
 

 

Sample Output
Test case #1
Total explored area: 180.00
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define lz 2*u,l,mid
#define rz 2*u+1,mid+1,r
const int maxn=4222;
double sum[maxn];
int flag[maxn];
double X[maxn];

struct Node
{
    double lx, rx, y;
    int s;
    Node(){};
    Node(double lx_, double rx_, double y_, int s_)
    {
        lx=lx_, rx=rx_, y=y_, s=s_;
    }
    bool operator <(const Node &S) const
    {
        return y<S.y;
    }
}line[maxn];

int find(double tmp, int n)
{
    int l=1, r=n, mid;
    while(l<=r)
    {
        mid=(l+r)>>1;
        if(X[mid]==tmp) return mid;
        else if(X[mid]<tmp) l=mid+1;
        else r=mid-1;
    }
}

void push_up(int u, int l, int r)
{
    if(flag[u]) sum[u]=X[r+1]-X[l];
    else if(l==r) sum[u]=0;
    else sum[u]=sum[2*u]+sum[2*u+1];
}

void Update(int u, int l, int r, int tl, int tr, int c)
{
    if(tl<=l&&r<=tr)
    {
        flag[u]+=c;
        push_up(u,l,r);
        return ;
    }
    int mid=(l+r)>>1;
    if(tr<=mid) Update(lz,tl,tr,c);
    else if(tl>mid) Update(rz,tl,tr,c);
    else
    {
        Update(lz,tl,mid,c);
        Update(rz,mid+1,tr,c);
    }
    push_up(u,l,r);
}

int main()
{
    int n,tcase=0;
    while(cin >> n,n)
    {
        int num=0;
        memset(flag,0,sizeof(flag));
        memset(sum,0,sizeof(sum));
        for(int i=0; i<n; i++)
        {
            double x1,x2,y1,y2;
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            line[++num]=Node(x1,x2,y1,1);
            X[num]=x1;
            line[++num]=Node(x1,x2,y2,-1);
            X[num]=x2;
        }
        sort(X+1,X+num+1);
        sort(line+1,line+num+1);
        int k=2*n;
        double ans=0;
        for(int i=1; i<num; i++)
        {
            int l=find(line[i].lx,k);
            int r=find(line[i].rx,k)-1;//r之所以要减一是因为防止搜索分叉的时候出现其余的节点都到左节点,而只有一个节点到右节点,如果减一后再配合查询时的+1那么就可以解决这个问题了,因为如果要有分叉的话,他实际上是右子树的第一个减去左子树开始的地方,如果没有分叉,那么直接+1再减也是满足的
            Update(1,1,k,l,r,line[i].s);
            ans+=sum[1]*(line[i+1].y-line[i].y);
        }
        printf("Test case #%d\n",++tcase);
        printf("Total explored area: %.2lf\n\n",ans);
    }
    return 0;
}

 

 

 

矩阵重叠面积计算 线段树hdu1542

标签:one   scanf   www.   计算   lun   pac   bottom   miss   should   

原文地址:http://www.cnblogs.com/mfys/p/7594645.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!