HDU 1542 Atlantis

来源:互联网 发布:品牌营销找优爵网络 编辑:程序博客网 时间:2024/04/30 14:05
#include "cstdio"#include "algorithm"using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn=2222;struct edge{    double a,b,h;    int s;};edge e[maxn];bool cmp(edge &p,edge &q){return p.h<q.h;}double sum[maxn<<2];int cnt[maxn<<2];double x[maxn];void pushup(int l,int r,int rt){    if(cnt[rt])  sum[rt]=x[r+1]-x[l];    else  if(l==r)  sum[rt]=0;    else  sum[rt]=sum[rt<<1]+sum[rt<<1|1];}void update(int L,int R,int c,int l,int r,int rt){    if(L<=l&&R>=r){        cnt[rt]+=c;        pushup(l,r,rt);        return;    }    int m=(l+r)>>1;    if(L<=m)  update(L,R,c,lson);    if(R>m)  update(L,R,c,rson);    pushup(l,r,rt);}int bst(double v,double x[],int n){    int l=0,r=n-1,m;    while(l<=r){        m=(l+r)>>1;        if(x[m]==v)  return m;        if(x[m]<v)  l=m+1;        else  r=m-1;    }    return -1;}int main(){    int n;    int kase=1;    double a,b,c,d;    while(scanf("%d",&n)!=EOF&&n){        int m=0;        double ans=0;        for(int i=0;i<n;i++){            scanf("%lf%lf%lf%lf",&a,&b,&c,&d);            x[m]=a;            e[m].a=a,e[m].b=c,e[m].h=b,e[m++].s=1;            x[m]=c;            e[m].a=a,e[m].b=c,e[m].h=d,e[m++].s=-1;        }        sort(x,x+m);        sort(e,e+m,cmp);        int k=1;        int L,R;        for(int i=1;i<m;i++){            if(x[i]!=x[i-1])  x[k++]=x[i];        }        memset(cnt,0,sizeof(cnt));        memset(sum,0,sizeof(sum));        for(int i=0;i<m-1;i++){            L=bst(e[i].a,x,k);            R=bst(e[i].b,x,k)-1;            if(R>=L)  update(L,R,e[i].s,0,k-1,1);            ans+=sum[1]*(e[i+1].h-e[i].h);            //printf("%.2lf %.2lf\n",sum[1],ans);        }        printf("Test case #%d\nTotal explored area: %.2lf\n\n",kase++,ans);    }    return 0;}

0 0
原创粉丝点击