Q - Get The Treasury - HDU 3642 (扫面线求体积)

题意:求被三个或三个以上立方体重合的体积

分析:就是平面面积的加强,不过归根还是一样的,可以把z轴按照从小向大分区间N个,然后可以得到N个平面,用平面重复三次以上的在和高度计算体积。

************************************************************************

#include<stdio.h>
#include<algorithm>
using namespace std;

#define Lson r<<1
#define Rson r<<1|1

const int MAXN = 2005;

struct Edge{int x, y1, y2, dir;}e[MAXN];
struct point{int x1,x2, y1,y2, z1,z2;}p[MAXN];
struct segmentTree
{///cover 表示覆盖的次数
    int L, R, cover;
    long long len1, len2, len3;
    int Mid(){return (L+R)>>1;}
}a[MAXN<<2];
int Hash_Y[MAXN], ny, Hash_Z[MAXN], nz;

bool cmp(Edge n1, Edge n2)
{
    return n1.x < n2.x;
}
int  FindSegLen(int y1, int y2)
{
    return Hash_Y[y2] - Hash_Y[y1];
}
void BuildTree(int r, int L, int R)
{
    a[r].L = L, a[r].R = R;
    a[r].len1 = a[r].len2 = a[r].len3 = a[r].cover = 0;

if(L == R-1)return ;

BuildTree(Lson, L, a[r].Mid());
    BuildTree(Rson, a[r].Mid(), R);
}
void PushUp(int r)
{///合并,注意要最大区间更新
    if( a[r].cover > 2 )
        a[r].len1 = a[r].len2 = a[r].len3 = FindSegLen( a[r].L, a[r].R );
    else if( a[r].L == a[r].R-1 && a[r].cover == 2 )
        a[r].len3 = 0, a[r].len2 = a[r].len1 = FindSegLen( a[r].L, a[r].R );
    else if( a[r].L == a[r].R-1 && a[r].cover == 1 )
        a[r].len3 = a[r].len2 = 0, a[r].len1 = FindSegLen( a[r].L, a[r].R );
    else if( a[r].L == a[r].R-1 )
        a[r].len1 = a[r].len2 = a[r].len3 = 0;
    else if( a[r].cover == 2 )
    {
        a[r].len3 = a[Lson].len1 + a[Rson].len1;
        a[r].len2 = a[r].len1 = FindSegLen( a[r].L, a[r].R );;
    }
    else if( a[r].cover == 1 )
    {
        a[r].len3 = a[Lson].len2 + a[Rson].len2;
        a[r].len2 = a[Lson].len1 + a[Rson].len1;
        a[r].len1 = FindSegLen( a[r].L, a[r].R );
    }
    else
    {
        a[r].len3 = a[Lson].len3 + a[Rson].len3;
        a[r].len2 = a[Lson].len2 + a[Rson].len2;
        a[r].len1 = a[Lson].len1 + a[Rson].len1;
    }
}
void UpData(int r, int L, int R, int dir)
{
    if( a[r].L == L && a[r].R == R )
    {
        a[r].cover += dir;
        PushUp(r);

return ;
    }

if(R <= a[r].Mid())
        UpData(Lson, L, R, dir);
    else if(L >= a[r].Mid())
        UpData(Rson, L, R, dir);
    else
    {
        UpData(Lson, L, a[r].Mid(), dir);
        UpData(Rson, a[r].Mid(), R, dir);
    }

PushUp(r);
}

int main()
{
    int T, t=1;

scanf("%d", &T);

while(T--)
    {
        int i, j, k, N;
        long long V=0;

scanf("%d", &N);

for(nz=i=0; i<N; i++)
        {
            scanf("%d%d%d%d%d%d", &p[i].x1,&p[i].y1,&p[i].z1,&p[i].x2,&p[i].y2,&p[i].z2);
            Hash_Z[nz++] = p[i].z1, Hash_Z[nz++] = p[i].z2;
        }

sort(Hash_Z, Hash_Z+nz);
        nz = unique(Hash_Z, Hash_Z+nz) - Hash_Z;

for(i=0; i<nz-1; i++)
        {///以z轴底部开始,判断每层是否有三个区间重合的
            for(ny=k=j=0; j<N; j++)
            {
                if( Hash_Z[i] >= p[j].z1 && Hash_Z[i] < p[j].z2 )
                {
                    e[k].x=p[j].x1, e[k].y1=p[j].y1, e[k].y2=p[j].y2, e[k++].dir=1;
                    e[k].x=p[j].x2, e[k].y1=p[j].y1, e[k].y2=p[j].y2, e[k++].dir=-1;
                    Hash_Y[ny++] = p[j].y1, Hash_Y[ny++] = p[j].y2;
                }
            }

sort(Hash_Y, Hash_Y+ny);
            ny = unique(Hash_Y, Hash_Y+ny)-Hash_Y;
            BuildTree(1, 0, ny-1);

sort(e, e+k, cmp);

long long S = 0;

for(j=0; j<k-1; j++)
            {
                int L = lower_bound(Hash_Y, Hash_Y+ny, e[j].y1) - Hash_Y;
                int R = lower_bound(Hash_Y, Hash_Y+ny, e[j].y2) - Hash_Y;

UpData(1, L, R, e[j].dir);

S += (long long)a[1].len3 * ( e[j+1].x - e[j].x );
            }

V += S * (Hash_Z[i+1] - Hash_Z[i]);
        }

printf("Case %d: %lld\n", t++, V);
    }

return 0;
}

时间: 2024-08-11 09:45:06

Q - Get The Treasury - HDU 3642 (扫面线求体积)的相关文章

Get The Treasury HDU - 3642(体积扫描线)

给出n个立方体,要你求这些立方体至少被覆盖三次的部分. 先把这个立方体的信息存在来,发现Z的范围不大,z范围是是[-500,500],所以我们可以先离散化,然后枚举Z, 然后对于每一段Z的区域内,在当前的区域内对xoy轴使用一次扫描线,找到当前这个区域内被覆盖三次的体积,然后每次枚举Z,每次相加,就是最后的答案. #include<map> #include<set> #include<ctime> #include<cmath> #include<s

hdu 3642 Get The Treasury(扫描线)

题目链接:hdu 3642 Get The Treasury 题目大意:三维坐标系,给定若干的长方体,问说有多少位置被覆盖3次以上. 解题思路:扫描线,将第三维分离出来,就是普通的二维扫描线,然后对于每个节点要维护覆盖0,1,2,3以上这4种的覆盖面积. #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const

hdu 3642 Get The Treasury (三维的扫描线)

题目大意: 给出N个立方体. 求一个三维空间中被包围三次的空间的体积之和. 思路分析: 发现Z的范围很小.那么我们可以枚举Z轴,然后对 x y做扫描线. 而且不用枚举所有的Z ,只需要将Z离散化之后枚举. #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #define maxn 2222 #define debug puts("fuck!&q

HDU 3642 Get The Treasury 线段树+扫描线

反向标记是错的,要对矩形进行拆分 #include <cstdio> #include <algorithm> #include <cstring> #include <vector> typedef long long LL; using namespace std; #define lson rt << 1,l,mid #define rson rt << 1 | 1,mid + 1,r const int maxn = 5e4

HDU 3642 Get The Treasury(线段树)

HDU 3642 Get The Treasury 题目链接 题意:给定一些立方体,求体积重叠超过3次的 思路:由于z坐标只有500,那么就可以枚举z坐标,每次做x,y的面积并即可,用线段树维护 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 1005; const int INF = 0x3f3f3f3f; typedef

HDU 3642 线段树+离散化+扫描线

题意:给你N个长方体的左下角和右上角坐标,问你空间中有多少体积是被大于两个不同的立方体覆盖的.x,y~10^6 z~500 考虑到给的z比较小,所以可以直接枚举z,然后跑二维的扫描线就好. 关于处理被不同的线段覆盖三次的问题,可以维护四个信息,cnt,once,twice,more,然后相互推出结果就好. #include <cstdio> #include <cstring> #include <vector> #include <algorithm> #

poj 2932 Coneology 扫面线法

题意: 给n个无交点的圆,求这n个圆中不被其它圆包含的圆. 分析: 扫面线法,用二叉树(set+lowerbound方法)维护最外圆的集合. 代码: #include <iostream> #include <algorithm> #include <vector> #include <set> using namespace std; const int maxN=40012; double r[maxN],x[maxN],y[maxN]; int n;

hdu 3642 Get The Treasury

Get The Treasury http://acm.hdu.edu.cn/showproblem.php?pid=3642 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Jack knows that there is a great underground treasury in a secret region. And he

HDU 3642 - Get The Treasury - [加强版扫描线+线段树]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3642 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description Jack knows that there is a great underground treasury in a secret region. And he has a special d