hdu 1255 矩形覆盖面积(面积交)

http://www.cnblogs.com/scau20110726/archive/2013/04/14/3020998.html

面积交和面积并基本上差不多。在面积并里,len[]记录的是覆盖一次或以上的长度。如果想要知道覆盖两次或以上的长度,可以加入一个len2[]数组。

1.col[rt]>=2 : 说明该区间被覆盖两次或以上,那么长度就可以直接计算,就是该区间的长度

2.先看叶子节点,因为是叶子没有孩子了,所以被覆盖两次货以上的长度就是0(无论col[rt]=1或col[rt]=0都是0,因为是叶子。。。)

3.不是叶子节点 ,且col[rt]=1.注意这里,col[rt]=1确切的意义是什么,应该是,可以确定,这个区间被完全覆盖了1次,而有没有被完全覆盖两次或以上则不知道无法确定,那么怎么怎么办了,只要加上len[rson] + len[rson]  即,看看左右孩子区间被覆盖了一次或以上的长度,那么叠加在双亲上就是双亲被覆盖两次或以上的长度

3.不是叶子节点,且col[rt]=0,确切的意义应该是不完全不知道被覆盖的情况(不知道有没有被覆盖,被覆盖了几次,长度是多少都不知道),这种情况,只能由其左右孩子的信息所得

len2[lson] + len2[rson]  , 即直接将左右孩子给覆盖了两次或以上的长度加起来,这样才能做到不重不漏

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!
")
#define MAXN 2000+5
#define MAX(a,b) a>b?a:b
#define blank pf("
")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue
#define INF 0x3f3f3f3f

#define ls (rt<<1)
#define rs (rt<<1|1)

int n,m;

double hh[MAXN],col[MAXN<<2],len[MAXN<<2],len2[MAXN<<2];

struct node
{
    double l,r,x,c;
    node(){}
    node(double a,double b,double c,double d):l(a),r(b),x(c),c(d){}
    bool operator < (const node &b) const
    {
        return x<b.x;
    }
}a[MAXN];

void PushUp(int rt,int l,int r)
{
    if(col[rt]>=2)
    {
        len[rt] = len2[rt] = hh[r+1] - hh[l];
    }
    else if(col[rt] == 1)
    {
        len[rt] = hh[r+1]-hh[l];
        if(l == r) len2[rt] = 0;
        else len2[rt] = len[rs]+len[ls];
    }
    else if(l==r) len[rt] = len2[rt] = 0;
    else
    {
        len[rt] = len[ls]+len[rs];
        len2[rt] = len2[ls]+len2[rs];
    }
}

void update(int val,int L,int R,int l,int r,int rt)
{
    if(L<=l && r<=R)
    {
        col[rt] += val;
        PushUp(rt,l,r);
        return;
    }
    int mid = (l+r)>>1;
    if(L <= mid) update(val,L,R,l,mid,ls);
    if(R > mid) update(val,L,R,mid+1,r,rs);
    PushUp(rt,l,r);
}

int main()
{
    int i,j,k,t,kase=1;
    sf("%d",&t);
    while(t--)
    {
        sf("%d",&n);
        int v=0;
        double sum = 0;
        for(i=0;i<n;i++)
        {
            double x1,y1,x2,y2;
            sf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            hh[v] = y1;
            a[v++] = node(y1,y2,x1,1);
            hh[v] = y2;
            a[v++] = node(y1,y2,x2,-1);
        }
        sort(hh,hh+v);
        sort(a,a+v);
        int d = 1;
        for(i=1;i<v;i++)
        {
            if(hh[i]!=hh[i-1]) hh[d++] = hh[i];
        }
        mem(len,0);
        mem(col,0);
        for(i=0;i<v-1;i++)
        {
            int l = lower_bound(hh,hh+d,a[i].l)-hh;
            int r = lower_bound(hh,hh+d,a[i].r)-hh-1;
            update(a[i].c,l,r,0,d-1,1);
            sum+=len2[1]*(a[i+1].x-a[i].x);
            //pf("%lf %lf
",sum,len[1]);
        }
        pf("%.2lf
",sum);
    }

    return 0;
}
原文地址:https://www.cnblogs.com/qlky/p/5765617.html