POJ 1151 Atlantis

来源:互联网 发布:sql中replace的用法 编辑:程序博客网 时间:2024/05/17 02:56

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.


【题目分析】
扫描线,大概就是平行于x轴的一条直线。矩形的平行于x轴把整个图形分成了许多部分,每次只需要维护一层就可以了。用线段树把线段插入或删除,然后才乘以高,就可以做了。(离散化很特殊double—>int).


【代码】

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;struct node{    double dis,real;    int l,r,cnt;}t[1001];double data[201];int n,kase;double ys[201],ls[201];double a[201][4];double ans=0;struct line{    int l,r;    double h;    int opt;}b[201];inline bool cmp(line a,line b){return a.h<b.h;}inline void build(int k,int L,int R){    t[k].l=L;t[k].r=R;    if (L==R) {t[k].dis=data[L]; return ;}    int mid=(L+R)/2;    build(k*2,L,mid);build(2*k+1,mid+1,R);    t[k].dis=t[k*2].dis+t[k*2+1].dis;    return ;}inline void cal(int k){    if (t[k].cnt) t[k].real=t[k].dis;    else if(t[k].l==t[k].r) t[k].real=0;    else t[k].real=t[k*2].real+t[k*2+1].real; }inline void add(int k,int l,int r,int opt){    if (t[k].l>=l&&t[k].r<=r)    {        t[k].cnt+=opt;        cal(k);        return ;    }    int mid=(t[k].l+t[k].r)/2;    if (r<=mid) add(k*2,l,r,opt);    else if (l>mid) add(k*2+1,l,r,opt);    else {        add(k*2,l,r,opt);        add(k*2+1,l,r,opt);    }    cal(k);}int main(){    while (scanf("%d",&n)==1&&n)    {        ans=0;        int top=0,all=0;        for (int i=1;i<=n;++i)        {            scanf("%lf%lf%lf%lf",&a[i][0],&a[i][1],&a[i][2],&a[i][3]);            ls[++top]=a[i][0];ls[++top]=a[i][2];        }        sort(ls+1,ls+top+1);        top=unique(ls+1,ls+top+1)-ls-1;        for (int i=1;i<=n;++i)        {            b[++all].l=lower_bound(ls+1,ls+top+1,a[i][0])-ls; ys[b[all].l]=a[i][0];            b[all].r=lower_bound(ls+1,ls+top+1,a[i][2])-ls; ys[b[all].r]=a[i][2];            b[all].h=a[i][1];            b[all].opt=1;            b[++all].l=lower_bound(ls+1,ls+top+1,a[i][0])-ls; ys[b[all].l]=a[i][0];            b[all].r=lower_bound(ls+1,ls+top+1,a[i][2])-ls; ys[b[all].r]=a[i][2];            b[all].h=a[i][3];            b[all].opt=-1;        }        for (int i=1;i<top;++i) data[i]=ys[i+1]-ys[i];        build(1,1,top-1);        sort(b+1,b+1+all,cmp);        double nowh=-0x3f3f3f3f;        for (int i=1;i<=all;++i)        {            ans+=(b[i].h-nowh)*t[1].real;            nowh=b[i].h;            add(1,b[i].l,b[i].r-1,b[i].opt);        }        printf("Test case #%d\n",++kase);        printf("Total explored area: %.2lf\n\n",ans);    }}
0 0
原创粉丝点击