Qiuqiqiu  
不管道路多么崎岖坎坷,我永远不停下追逐梦想的脚步!

http://acm.hdu.edu.cn/showproblem.php?pid=1542

面积并

#include <cstdio>
#include <algorithm>
using namespace std;

const int N=210;
struct segment
{
    double x1,x2,y;
    int d;
    segment(){}
    segment(double _x1,double _x2,double _y,int _d)
    :x1(_x1),x2(_x2),y(_y),d(_d){}
    bool operator < (const segment &a)const
    {
        return y<a.y;
    }
}seg[N];
double X[N];
struct segtree
{
    int l,r,c;
    double s;
}st[N<<2];
#define lch rt<<1
#define rch rt<<1|1
int drt(double *x,int n)
{
    sort(x,x+n);
    int m=1;
    for(int i=1;i<n;i++)
        if(x[i]!=x[i-1]) x[m++]=x[i];
    return m;
}
int bfind(double x,int l,int r,double *A)
{
    while (l<=r)
    {
        int m=(l+r)/2;
        if (A[m]==x) return m;
        if (x<A[m]) r=m-1;
        else l=m+1;
    }
    return -1;
}
void build(int l,int r,int rt)
{
    st[rt].l=l; st[rt].r=r;
    st[rt].s=st[rt].c=0;
    if(l==r) return;
    int m=(l+r)/2;
    build(l,m,lch);
    build(m+1,r,rch);
}
void pushup(int rt)
{
    if(st[rt].c) st[rt].s=X[st[rt].r]-X[st[rt].l-1];
    else
    {
        if(st[rt].l==st[rt].r) st[rt].s=0;
        else st[rt].s=st[lch].s+st[rch].s;
    }
}
void update(int L,int R,int x,int rt)
{
    int l=st[rt].l, r=st[rt].r;
    if(L<=l && r<=R)
    {
        st[rt].c+=x;
        pushup(rt);
        return;
    }
    int m=(l+r)/2;
    if(L<=m) update(L,R,x,lch);
    if(R>m) update(L,R,x,rch);
    pushup(rt);
}
int main()
{
    int n,C=0;
    while(scanf("%d",&n),n)
    {
        int ns=0,nx=0;
        for(int i=0;i<n;i++)
        {
            double x1,y1,x2,y2;
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            if(x1>x2) swap(x1,x2);
            if(y1>y2) swap(y1,y2);
            X[nx++]=x1; X[nx++]=x2;
            seg[ns++]=segment(x1,x2,y1,1);
            seg[ns++]=segment(x1,x2,y2,-1);
        }
        nx=drt(X,nx);
        sort(seg,seg+ns);
        build(1,nx-1,1);
        double ans=0;
        for(int i=0;i<ns;i++)
        {
            int l=bfind(seg[i].x1,0,nx-1,X)+1;
            int r=bfind(seg[i].x2,0,nx-1,X);
            update(l,r,seg[i].d,1);
            ans+=st[1].s*(seg[i+1].y-seg[i].y);
        }
        printf("Test case #%d\n",++C);
        printf("Total explored area: %.2lf\n\n",ans);
    }
    return 0;
}

 

 

posted on 2012-03-01 22:18  Qiuqiqiu  阅读(383)  评论(0编辑  收藏  举报