Get The Treasury HDU - 3642线段树+扫描线 解决立方体体积

求立方体三次重叠的体积

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<stack>
#include<set>
#include<ctime>
#include<algorithm>
#define eps 1e-14
#define pi acos(-1)
#define ll long long
#define RD T*(rand()*2-RAND_MAX)
#define Drand (long double)rand()/RAND_MAX
#define INF 0x3f3f3f3f
#define endl '\n'
using namespace std;
const int maxn=1e3+100;
const int mod=1e4+7;

int z[maxn<<1],x[maxn<<1],y[maxn],len1[maxn<<2],len2[maxn<<2],len3[maxn<<2],cov[maxn<<2];

struct nn
{
    int x1,x2,y1,y2,z1,z2;
    nn(){}
    nn(int x1,int y1,int z1,int x2,int y2,int z2):x1(x1),
    y1(y1),z1(z1),x2(x2),y2(y2),z2(z2){}
}cube[maxn<<1];

struct Line
{
    int x1,x2,y,f;
    Line(){}
    Line(int x1,int x2,int y,int f):x1(x1),x2(x2),y(y),f(f){}
}l[maxn<<1];

int cmp(Line a,Line b)
{
    return a.y<b.y;
}

void build(int l,int r,int rt)
{
    len1[rt]=0;
    len2[rt]=0;
    len3[rt]=0;
    cov[rt]=0;
    if(l==r)return;
    int mid=(l+r)>>1;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
}

void pushup(int l,int r,int rt)
{
    if(cov[rt]>=1){
        len1[rt]=x[r+1]-x[l];
    }
    else if(l==r)len1[rt]=0;
    else{
        len1[rt]=len1[rt<<1]+len1[rt<<1|1];
    }

    if(cov[rt]>=2){
        len2[rt]=x[r+1]-x[l];
    }
    else if(l==r)len2[rt]=0;
    else if(cov[rt]==1){
        len2[rt]=len1[rt<<1]+len1[rt<<1|1];
    }
    else{
        len2[rt]=len2[rt<<1]+len2[rt<<1|1];
    }

    if(cov[rt]>=3){
        len3[rt]=x[r+1]-x[l];
    }
    else if(l==r)len3[rt]=0;
    else if(cov[rt]==1){
        len3[rt]=len2[rt<<1]+len2[rt<<1|1];
    }
    else if(cov[rt]==2){
        len3[rt]=len1[rt<<1]+len1[rt<<1|1];
    }
    else{
        len3[rt]=len3[rt<<1]+len3[rt<<1|1];
    }
}

void update(int L,int R,int l,int r,int rt,int f)
{
    if(L<=l && r<=R){
        cov[rt]+=f;
        pushup(l,r,rt);
        return;
    }
    int mid=(l+r)>>1;
    if(L<=mid)update(L,R,l,mid,rt<<1,f);
    if(R>mid)update(L,R,mid+1,r,rt<<1|1,f);
    pushup(l,r,rt);
}

int main()
{
//    freopen("in.txt","r",stdin);
    int t,cas=1;
    scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
        int x1,x2,y1,y2,z1,z2;
        int cntz=0;
        for(int i=1;i<=n;i++){
            scanf("%d%d%d%d%d%d",&x1,&y1,&z1,&x2,&y2,&z2);
            nn c(x1,y1,z1,x2,y2,z2);
            cube[i]=c;
            z[cntz++]=z1;
            z[cntz++]=z2;
        }
        sort(z,z+cntz);
        cntz=unique(z,z+cntz)-z;
        ll ans=0;
        for(int i=0;i<cntz-1;i++){
            int cntl=1,cntx=1;
            for(int j=1;j<=n;j++){
                if(cube[j].z1<=z[i] && cube[j].z2>=z[i+1]){
                    x[cntx++]=cube[j].x1;
                    x[cntx++]=cube[j].x2;
                    l[cntl++]=Line(cube[j].x1,cube[j].x2,cube[j].y1,1);
                    l[cntl++]=Line(cube[j].x1,cube[j].x2,cube[j].y2,-1);
                }
            }
            sort(l+1,l+cntl,cmp);
            sort(x+1,x+cntx);
            cntx=unique(x+1,x+cntx)-x-1;
            ll area=0;
            for(int j=1;j<cntl-1;j++){
                int L=lower_bound(x+1,x+cntx,l[j].x1)-x;
                int R=lower_bound(x+1,x+cntx,l[j].x2)-x-1;
                update(L,R,1,cntx-1,1,l[j].f);
                area+=(ll)len3[1]*(ll)(l[j+1].y-l[j].y);
            }
            ans+=area*(ll)(z[i+1]-z[i]);
            memset(len1,0,sizeof len1);
            memset(len2,0,sizeof len2);
            memset(len3,0,sizeof len3);
            memset(cov,0,sizeof cov);
        }
        printf("Case %d: %lld\n",cas++,ans);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值