hdu1255覆盖的面积

来源:互联网 发布:mac os x 10.13 镜像 编辑:程序博客网 时间:2024/05/13 14:24

覆盖的面积

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6441    Accepted Submission(s): 3275


Problem Description
给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.


 

Input
输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1<=N<=1000),代表矩形的数量,然后是N行数据,每一行包含四个浮点数,代表平面上的一个矩形的左上角坐标和右下角坐标,矩形的上下边和X轴平行,左右边和Y轴平行.坐标的范围从0到100000.

注意:本题的输入数据较多,推荐使用scanf读入数据.
 

Output
对于每组测试数据,请计算出被这些矩形覆盖过至少两次的区域的面积.结果保留两位小数.
 

Sample Input
251 1 4 21 3 3 72 1.5 5 4.53.5 1.25 7.5 46 3 10 730 0 1 11 0 2 12 0 3 1
 

Sample Output
7.630.00

思路:这题运用线段树,用一个cnt判断这个区间是否覆盖过两次,如果cnt>1,那么就是覆盖了两次,如果cnt==1,加上孩子区间李覆盖了一次的就是覆盖了两次的,如果cnt==0,加上孩子区间覆盖了2次的,,这样就不会遗漏。


代码:

#include <iostream>#include <stdio.h>#include <string.h>#include <map>#include <algorithm>using namespace std;double hh[2005],sum[2005*4];int mark[2005*4];struct node{    double l,r,h;    int d;}a[2005*4];struct no{    int l,r,cnt;    double s,ss;}t[2005*4];bool cmp(node a,node b){    return a.h<b.h;}void build(int i,int l,int r){    t[i].l=l;    t[i].r=r;    t[i].ss=t[i].s=t[i].cnt=0;    if(l==r)return;    int mid=(t[i].l+t[i].r)>>1;    build(i*2,l,mid);    build(i*2+1,mid+1,r);}void upfather(int i){    if(t[i].cnt)t[i].s=hh[t[i].r+1]-hh[t[i].l];    else if(t[i].l==t[i].r)t[i].s=0;    else t[i].s=t[i*2].s+t[i*2+1].s;    if(t[i].cnt>1)    {        t[i].ss=hh[t[i].r+1]-hh[t[i].l];    }   else if(t[i].l==t[i].r)t[i].ss=0;   else if(t[i].cnt == 1)        t[i].ss = t[i*2].s + t[i*2+1].s;    else        t[i].ss = t[i*2].ss+ t[i*2+1].ss;}void update(int l,int r,int d,int n){    if(l==t[n].l&&t[n].r==r)    {        t[n].cnt+=d;        upfather(n);        return ;    }    int mid=(t[n].l+t[n].r)>>1;    if(r<=mid)update(l,r,d,n*2);    else  if(l>mid)update(l,r,d,n*2+1);    else    {        update(l,mid,d,n*2);        update(mid+1,r,d,n*2+1);    }    upfather(n);}int findx(double m,int x,int y){    while(x<=y)    {         int mid=(x+y)>>1;         if(hh[mid]==m)return mid;         else if(hh[mid]>m)y=mid-1;         else x=mid+1;    }    return -1;}int main(){    int T;    cin>>T;    while(T--)    {        int n;        int i;        scanf("%d",&n);        double x1,x2,y1,y2;        int k=0;        double ans1=0;        for(i=0;i<n;i++)        {            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);            if(x1>x2)swap(x1,x2);            if(y1>y2)swap(y1,y2);            a[k].l=x1;            a[k].r=x2;            a[k].h=y1;            a[k].d=1;            hh[k++]=x1;            a[k].l=x1;            a[k].r=x2;            a[k].h=y2;            a[k].d=-1;            hh[k++]=x2;        }        sort(hh,hh+k);        sort(a,a+k,cmp);        int z=1;        for(i=1;i<k;i++)        {            if(hh[i]!=hh[i-1])hh[z++]=hh[i];        }        double ans=0;        build(1,0,z-1);        for(i=0;i<k-1;i++)        {            int  L=findx(a[i].l,0,z-1);            int  R=findx(a[i].r,0,z-1)-1;            update(L,R,a[i].d,1);            ans+=t[1].ss*(a[i+1].h-a[i].h);        }        printf("%.2lf\n",ans);    }    return 0;}


原创粉丝点击