【poj1151】【离散化】Atlantis

Atlantis

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 21743 Accepted: 8184

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 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

Source

Mid-Central European Regional Contest 2000

题目大意就是说给你n个矩形,然后让你求面积
然后只有100个矩阵,但是大小很大,而且是实数,所以要离散,然后扫面线求出面积
我比较懒。不想写那么多字,就推荐你们看这个:http://blog.csdn.net/youngyangyang04/article/details/7787693

然后差不多就这样,我的代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<stack>
#define INF 2100000000
#define ll long long
#define clr(x)  memset(x,0,sizeof(x))
#define clrmax(x)  memset(x,127,sizeof(x))

using namespace std;

inline int read()
{
    char c;
    int ret=0;
    while(!(c>='0'&&c<='9'))
        c=getchar();
    while(c>='0'&&c<='9')
    {
        ret=(c-'0')+(ret<<1)+(ret<<3);
        c=getchar();
    }
    return ret;
}

#define M 10005

struct node
{
    double x,y1,y2;
    int flag,no;
}a[M*2];
#define x(y) a[y].x
#define y1(x) a[x].y1
#define y2(x) a[x].y2
#define flag(x) a[x].flag
#define no(x) a[x].no
struct tree2
{
    tree2 *lson,*rson;
    int s,bo;
    double len,l,r;
}*root,dizhi[M];

int T;
const double eps=1E-8;
int n,t,vis[M],cnt;
double t1[M],t2[M],t3[M],t4[M];
double y[M],ans;

bool com(node a,node b)
{
    return a.x<b.x;
}

bool equal(double a,double b)
{
    return (a+eps>=b&&a-eps<=b);
}

void bulid(tree2 *tree,int l,int r)
{
    tree->l=y[l];
    tree->r=y[r];
    tree->s=tree->len=0;
    if(r-l==1)
    {
        tree->bo=1;
        return ;
    }
    int mid=(l+r)>>1;
    tree->lson=&dizhi[T++];
    tree->rson=&dizhi[T++];
    bulid(tree->lson,l,mid);
    bulid(tree->rson,mid,r);
}

void update(tree2 *&tree)
{
    if(tree->s>0)
    {
        tree->len=tree->r-tree->l;
        return ;
    }
    if(tree->bo)
    {
        tree->len=0;
        return ;
    }
    tree->len=tree->lson->len+tree->rson->len;
}

void change(tree2 *tree,int l,int r,node x)
{
    if(equal(x.y1,x.y2))return ;
    if(equal(tree->l,x.y1)&&equal(tree->r,x.y2))
    {
        tree->s+=x.flag;
        update(tree);
        return ;
    }
    int mid=(l+r)>>1;
    if(x.y2<tree->lson->r)change(tree->lson,l,mid,x);
    else if(x.y1>tree->rson->l)change(tree->rson,mid,r,x);
    else
    {
        node temp=x;
        temp.y2=tree->lson->r;
        change(tree->lson,l,mid,temp);
        temp=x;
        temp.y1=tree->rson->l;
        change(tree->rson,mid,r,temp);
    }
    update(tree);
}



int main()
{
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    while(cin>>n)
    {
        cnt++;
        if(n==0)return 0;
        clr(a);clr(vis);ans=0;t=0;
        clr(dizhi);T=0;
        root=&dizhi[T++];
        for(int i=1;i<=n;i++)
        {
            double x1,x2,y1,y2;
            scanf("%lf%lf%lf%lf",&t1[i],&t2[i],&t3[i],&t4[i]);
            x1=t1[i];x2=t3[i];
            y1=t2[i];y2=t4[i];
            y[++t]=y1;
            x(t)=x1;y1(t)=y1;y2(t)=y2;flag(t)=1;no(t)=i;
            y[++t]=y2;
            x(t)=x2;y1(t)=y1;y2(t)=y2;flag(t)=-1;no(t)=i;
        }
        sort(a+1,a+2*n+1,com);
        sort(y+1,y+2*n+1);
        bulid(root,1,2*n);
        double last=x(1);
        vis[no(1)]=flag(1);
        for(int i=1;i<=2*n;i++)
        {
            ans+=(x(i)-x(i-1))*root->len;
            change(root,1,2*n,a[i]);
        }
        printf("Test case #%d\nTotal explored area: %.2f\n\n",cnt,ans);
    }
    return 0;
}

大概就是这个样子,如果有什么问题,或错误,请在评论区提出,谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值