hdu 3642

题目


求长方体相交3次以上的区域的体积


枚举相邻的两个z,选出z坐标包括枚举范围的长方体的底面,然后求面积覆盖3次以上的,再来乘以枚举的z之差,最后取和。



#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<map>
using namespace std;
typedef long long ll;
map<int,int>x;//用来离散x坐标
map<int,int>z;//用来离散z坐标,缩小枚举次数
map<int,int>::iterator it;
int cx[1010*2],cz[1010*2],kx,kz,t,n;
struct spot//长方体
{
    int x1,y1,z1,x2,y2,z2;
}s[1010];

struct line
{
    int s,t,y,cov;
}l[1010*2];
bool cmp(line i,line j)
{
    return i.y<j.y;
}


struct node
{
    int l,r,len1,len2,len3;
    int cov;
}root[1010*2*6];
inline void build(int t,int x,int y)
{
    root[t].l=x,root[t].r=y;
    root[t].cov=root[t].len1=root[t].len2=root[t].len3=0;
    if(x+1==y) return;
    int m=(x+y)>>1;
    build(t*2,x,m);
    build(t*2+1,m,y);
}
inline void Push_up(int t)//pushup学得小媛的
{
    if(root[t].cov>=3)
    {
        root[t].len3=cx[root[t].r]-cx[root[t].l];
        root[t].len2=root[t].len1=0;
    }
    else if(root[t].cov==2)
    {
        if(root[t].r-1==root[t].l)
        {
            root[t].len2=cx[root[t].r]-cx[root[t].l];
            root[t].len3=root[t].len1=0;
        }
        else
        {
            root[t].len3=root[t*2].len3+root[t*2+1].len3+root[t*2].len2+root[t*2+1].len2+root[t*2].len1+root[t*2+1].len1;
            root[t].len2=cx[root[t].r]-cx[root[t].l]-root[t].len3;
            root[t].len1=0;
        }
    }
    else if(root[t].cov==1)
    {
         if(root[t].r-1==root[t].l)
        {
            root[t].len1=cx[root[t].r]-cx[root[t].l];
            root[t].len3=root[t].len2=0;
        }
        else
        {
            root[t].len3=root[t*2].len3+root[t*2+1].len3+root[t*2].len2+root[t*2+1].len2;
            root[t].len2=root[t*2].len1+root[t*2+1].len1;
            root[t].len1=cx[root[t].r]-cx[root[t].l]-root[t].len3-root[t].len2;
        }
    }
    else if(root[t].cov==0)
    {
        if(root[t].r-1==root[t].l)
        {
            root[t].len1=root[t].len3=root[t].len2=0;
        }
        else
        {
            root[t].len1=root[t*2].len1+root[t*2+1].len1;
            root[t].len2=root[t*2].len2+root[t*2+1].len2;
            root[t].len3=root[t*2].len3+root[t*2+1].len3;
        }
    }
}
inline void Modefiy(int t,int x,int y,int cov)
{
    int l=root[t].l,r=root[t].r;
    if(r<x||l>y) return;
    if(l>=x&&r<=y)
    {
        root[t].cov+=cov;
        Push_up(t);
        return;
    }
    if(l+1==r) return;
    Modefiy(t*2,x,y,cov);
    Modefiy(t*2+1,x,y,cov);
    Push_up(t);
}
int main()
{
    scanf("%d",&t);
    for(int Ca=1;Ca<=t;Ca++)
    {
        x.clear(),z.clear();
        kx=1,kz=1;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d%d%d%d",&s[i].x1,&s[i].y1,&s[i].z1,&s[i].x2,&s[i].y2,&s[i].z2);
            if(x[s[i].x1]==0) x[s[i].x1]=1;
            if(x[s[i].x2]==0) x[s[i].x2]=1;
            if(z[s[i].z1]==0) z[s[i].z1]=1;
            if(z[s[i].z2]==0) z[s[i].z2]=1;
        }
        for(it=x.begin();it!=x.end();it++)//离散化
        {
            //   printf("%d\n",it->first);
            x[it->first]=kx;
            cx[kx++]=it->first;
        }
        for(it=z.begin();it!=z.end();it++)
        {
            z[it->first]=kz;
            cz[kz++]=it->first;
        }
        ll ans=0;
        if(n>=3)
        {
            for(int i=1;i+1<kz;i++)
            {
                int sz=cz[i],tz=cz[i+1];
                int num=0;
                for(int j=0;j<n;j++)
                {
                    if(s[j].z1<=sz&&s[j].z2>sz)
                    {
                        l[num].s=s[j].x1,l[num].t=s[j].x2,l[num].y=s[j].y1,l[num++].cov=1;
                        l[num].s=s[j].x1,l[num].t=s[j].x2,l[num].y=s[j].y2,l[num++].cov=-1;
                    }
                }
                sort(l,l+num,cmp);
                build(1,1,kx);
                ll temp=0;
                for(int j=0;j+1<num;j++)
                {
                    int a=x[l[j].s];
                    int b=x[l[j].t];
                    Modefiy(1,a,b,l[j].cov);
                    //    if(root[t].len3<0) printf("z\n");
                    temp+=root[1].len3*1ll*(l[j+1].y-l[j].y);
                }
                ans+=(tz-sz)*temp;
            }
        }
        printf("Case %d: %I64d\n",Ca,ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值