天天看點

hdu 3642 求長方體的體積交

做了兩個多小時。。。。其實也還好

求至少有三個長方體重疊的區域的體積交

對z軸坐标離散化,對于每個高度,記錄包含該高度的長方體的下表面,求至少覆寫三次的面積并

求面積交同那道“覆寫的面積”,隻不過這裡是覆寫三次,稍微想的仔細一點應該不成問題

再乘以(z[i+1]-z[i]),易知z[i+1]==所記錄的長方體上表面的高的最小值,是以不用擔心這樣做的正确性

View Code

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef __int64 lld;
const int maxn = 2222;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int x[maxn],z[maxn],n,cntx,cntz;
int sum[maxn<<2],once[maxn<<2],twice[maxn<<2];
int cover[maxn<<2];
struct seg{
    int l,r,h;
    int flag;
    seg(){};
    seg(int _l,int _r,int _h,int _flag):l(_l),r(_r),h(_h),flag(_flag){}
    bool operator < (const seg &cmp)const {
        if(h==cmp.h) return flag>cmp.flag;
        return h<cmp.h;
    }
}ss[maxn];
struct point {
    int x,y,z;
    void get()
    {
        scanf("%d%d%d",&x,&y,&z);
    }
};
struct pp{
    point a,b;
}cube[1111];
void pushup(int rt,int l,int r)
{
    if(cover[rt]==3)
    {
        sum[rt]=x[r+1]-x[l];
        once[rt]=0;
        twice[rt]=0;
        return ;
    }
    if(cover[rt]==2)
    {
        sum[rt]=once[rt<<1]+once[rt<<1|1]+sum[rt<<1]+sum[rt<<1|1]+twice[rt<<1]+twice[rt<<1|1];
        once[rt]=0;
        twice[rt]=x[r+1]-x[l]-sum[rt];
        return ;
    }
    if(cover[rt]==1)
    {
        sum[rt]=twice[rt<<1]+twice[rt<<1|1]+sum[rt<<1]+sum[rt<<1|1];
        twice[rt]=once[rt<<1]+once[rt<<1|1];
        once[rt]=x[r+1]-x[l]-sum[rt]-twice[rt];
        return ;
    }
    if(cover[rt]==0)
    { 
        sum[rt]=sum[rt<<1]+sum[rt<<1|1];
        once[rt]=once[rt<<1]+once[rt<<1|1];
        twice[rt]=twice[rt<<1]+twice[rt<<1|1];
        return ;
    }
}
void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        cover[rt]+=c;
        pushup(rt,l,r);
        return ;
    }
    int m=(l+r)>>1;
    if(L<=m) update(L,R,c,lson);
    if(R>m) update(L,R,c,rson);
    pushup(rt,l,r);
}
lld solve()
{
    int i,j;
    sort(x,x+cntx);
    sort(z,z+cntz);
    cntx=unique(x,x+cntx)-x;
    cntz=unique(z,z+cntz)-z;
    lld ans=0;
    for(i=0;i<cntz-1;i++)
    {
        int tot=0;
        for(j=1;j<=n;j++)
        {
            if(cube[j].a.z<=z[i] && cube[j].b.z>z[i])
            {
                int x1=cube[j].a.x,x2=cube[j].b.x;
                int y1=cube[j].a.y,y2=cube[j].b.y;
                ss[tot++]=seg(x1,x2,y1,1);
                ss[tot++]=seg(x1,x2,y2,-1);
            }
        }
        memset(cover,0,sizeof(cover));
        memset(sum,0,sizeof(sum));
        memset(once,0,sizeof(0));
        memset(twice,0,sizeof(twice));
        sort(ss,ss+tot);
        lld area=0;
        int left=lower_bound(x,x+cntx,ss[0].l)-x;
        int right=lower_bound(x,x+cntx,ss[0].r)-x-1;
        update(left,right,ss[0].flag,0,cntx-1,1);
        for(j=1;j<tot;j++)
        {
            area+=(lld)sum[1]*(ss[j].h-ss[j-1].h);
            left=lower_bound(x,x+cntx,ss[j].l)-x;
            right=lower_bound(x,x+cntx,ss[j].r)-x-1;
            update(left,right,ss[j].flag,0,cntx-1,1);
        }
        ans+=area*(z[i+1]-z[i]);
    }
    return ans;
}
int main()
{
    int t,ca=1,i,j;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        cntx=cntz=0;
        for(i=1;i<=n;i++)
        {
            cube[i].a.get();
            x[cntx++]=cube[i].a.x;
            z[cntz++]=cube[i].a.z;
            cube[i].b.get();
            x[cntx++]=cube[i].b.x;
            z[cntz++]=cube[i].b.z;
        }
        if(n<3) 
        {
            printf("Case %d: 0\n",ca++);
            continue;
        }
        lld ans=solve();
        printf("Case %d: %I64d\n",ca++,ans);
    }
    return 0;
}