POJ 1151 Atlantis

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.


【题目分析】
离散化+线段树+扫描线 求矩形的面积并。
还看到一种只需要离散化之后就可以求的方法。


【代码】
一、线段树+扫描线(未AC,大数据跪了)

#include <cstdio>
#include <algorithm>
using namespace std;
double a[101][5];
struct line{
    int op;
    double h,l1,r1;
    int l,r;
}b[101];
struct node{
    double dis;
    int tmp,l,r;
}t[40001];
double nowh=0,ans=0;
double c[10001];
double l[401];
inline void built(int num,int left,int r)
{
    t[num].l=left;t[num].r=r;t[num].tmp=0;
    if (left==r) {t[num].dis=l[left];return;}
    int mid=(left+r)>>1;built(num*2,left,mid),built(num*2+1,mid+1,r);
    t[num].dis=t[num*2].dis+t[num*2+1].dis;
}
inline void push(int num)
{t[num*2].tmp+=t[num].tmp;t[num*2+1].tmp+=t[num].tmp;t[num].tmp=0;}
inline void add(int num,int l,int r,int x)
{
    if (t[num].l>r||t[num].r<l) return;
    if (t[num].l>=l&&t[num].r<=r){t[num].tmp+=x;return;}
    push(num); int mid=(l+r)>>1;
    add(num*2,l,r,x),add(num*2+1,l,r,x);
}
inline double gethi(int num)
{
    if (t[num].tmp<0) push(num);
    if (t[num].tmp>=1) return t[num].dis;
    if (t[num].l==t[num].r) return 0;
    else return gethi(num*2)+gethi(num*2+1);
}
inline bool cmp(line a,line b)
{return a.h<b.h;}
//void print(int num)
//{printf("%d %d %lf %d\n",t[num].l,t[num].r,t[num].dis,t[num].tmp);}
int main()
{

    int n,cnt=0,top=0,kase=0;
    while (~scanf("%d",&n)!=EOF&&n)
{
    cnt=0,top=0;nowh=0,ans=0;
    for (int i=1;i<=n;++i)
    {
        for (int j=1;j<=4;++j) scanf("%lf",&a[i][j]);
        b[++cnt].h=a[i][2];b[cnt].l1=a[i][1];b[cnt].r1=a[i][3];b[cnt].op=1;
        b[++cnt].h=a[i][4];b[cnt].l1=a[i][1];b[cnt].r1=a[i][3];b[cnt].op=-1;
        c[++top]=a[i][1];c[++top]=a[i][3];
    }
    sort(c+1,c+1+top);
    top=unique(c+1,c+1+top)-1-c;
    for (int i=1;i<top;++i) l[i]=c[i+1]-c[i];
    for (int i=1;i<=cnt;++i)
    {
        b[i].l=lower_bound(c+1,c+1+top,b[i].l1)-c;
        b[i].r=lower_bound(c+1,c+1+top,b[i].r1)-c;
    }
    built(1,1,top-1);
    sort(b+1,b+cnt+1,cmp);
    nowh=b[1].h;
    for (int i=1;i<=cnt;++i)
    {
        double hi=gethi(1);
        ans+=(b[i].h-nowh)*hi; nowh=b[i].h;
        add(1,b[i].l,b[i].r-1,b[i].op);
    }
    printf("Test case #%d\nTotal explored area: %.2lf\n",++kase,ans+1e-8);
}
}

二、离散化

#include<iostream>
#include<algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
double x[201],y[201],s[101][4];
int xy[201][201];
int n,cas=0;
double sum;
int main()
{
    int i,j,k;
    while(cin>>n)
    {   
        if(n==0)
            break;
        cas++;
        k=0;
        sum=0.0;
        memset(xy,0,sizeof(xy));
        for(i=1;i<=n;i++)
        {
            cin>>s[i][0]>>s[i][1]>>s[i][2]>>s[i][3];
            x[k]=s[i][0];
            y[k]=s[i][1];
            k++;
            x[k]=s[i][2];
            y[k]=s[i][3];
            k++;
        }
        sort(x,x+2*n);
        sort(y,y+2*n);
        for(k=1;k<=n;k++)
        {
            int i1,i2,j1,j2;
            for(i1=0;i1<2*n;i1++)
            {
                if(x[i1]==s[k][0])
                    break;
            }
            for(i2=0;i2<2*n;i2++)
            {
                if(x[i2]==s[k][2])
                    break;
            }
            for(j1=0;j1<2*n;j1++)
            {
                if(y[j1]==s[k][1])
                    break;
            }
            for(j2=0;j2<2*n;j2++)
            {
                if(y[j2]==s[k][3])
                    break;
            }
            for(i=i1;i<i2;i++)
            {
                for(j=j1;j<j2;j++)
                {
                    xy[i][j]=1;
                }
            }
        }
        for(i=0;i<2*n;i++)
        {
            for(j=0;j<2*n;j++)
            {
                sum+=xy[i][j]*(x[i+1]-x[i])*(y[j+1]-y[j]);
            }
        }
        printf("Test case #%d\n",cas);
        printf("Total explored area: %.2f\n",sum);
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值