HDU 1255 覆盖的面积

题目链接:HDU-1255

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

HDU-1542的代码稍微改改即可。

代码如下:

#include<cstdio>
#include<set>
#include<map>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;

const int MAXN=2010;
struct Line
{
    int x1,x2,d;
    double y;
    bool operator < (Line x)
    {
        return y<x.y;
    }
    void operator = (Line x)
    {
        x1=x.x1;
        x2=x.x2;
        y=x.y;
        d=x.d;
    }
};
double dict[MAXN];
map<double,int> index;
double xx1[MAXN],xx2[MAXN],yy1[MAXN],yy2[MAXN];
set<double> S;
Line lines[MAXN];

LL mark[MAXN<<2];
double sum[MAXN<<2];
void modify(LL o,LL l,LL r,LL ml,LL mr,LL num)
{
    if(ml<=l && r<=mr)
        mark[o]+=num;
    if(r!=l)
    {
        LL m=(r-l)/2+l;
        if(ml<=m) modify(o*2,l,m,ml,mr,num);
        if(mr>m) modify(o*2+1,m+1,r,ml,mr,num);
    }
    if(mark[o]>=2) sum[o]=(dict[r+1]-dict[l]);
    else if(l==r) sum[o]=0;
    else sum[o]=sum[o*2]+sum[o*2+1];
}
int main()
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif
    int n,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int dictnum=0;
        index.clear();
        S.clear();
        memset(sum,0,sizeof(sum));
        memset(mark,0,sizeof(mark));
        double ans=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&xx1[i],&yy1[i],&xx2[i],&yy2[i]);
            S.insert(xx1[i]);
            S.insert(xx2[i]);
        }
        for(set<double>::iterator it=S.begin();it!=S.end();it++)
        {
            dict[++dictnum]=*it;
            index[*it]=dictnum;
        }
        for(int i=1;i<=n;i++)
        {
            Line tmp;
            tmp.x1=index[xx1[i]]; tmp.x2=index[xx2[i]];
            tmp.y=yy1[i]; tmp.d=1;
            lines[2*i-1]=tmp;
            tmp.y=yy2[i]; tmp.d=-1;
            lines[2*i]=tmp;
        }
        sort(lines+1,lines+1+2*n);
        for(int now=1;now<=n*2-1;now++)
        {
            double h=lines[now+1].y-lines[now].y;
            Line ing=lines[now];
            modify(1,1,dictnum-1,ing.x1,ing.x2-1,ing.d);
            ans+=h*sum[1];
        }
        printf("%.2lf
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zarth/p/6378389.html