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 int maxn = 4005;

vector<int> pos;

#define lson(x) ((x)<<1)
#define rson(x) (((x)<<1)|1)
int lc[maxn << 2], rc[maxn << 2], v[maxn << 2], s[maxn << 2][5];

inline void pushup(int u) {
    memset(s[u], 0, sizeof(s[u]));

    if (v[u] >= 3)
        s[u][3] = pos[rc[u]+1] - pos[lc[u]];
    else {
        if (lc[u] == rc[u])
            s[u][v[u]] = pos[rc[u]+1] - pos[lc[u]];
        else if (v[u] == 2) {
            s[u][2] = s[lson(u)][0] + s[rson(u)][0];
            for (int i = 1; i <= 3; i++)
                s[u][3] += s[lson(u)][i] + s[rson(u)][i];
        } else if (v[u] == 1) {
            s[u][1] = s[lson(u)][0] + s[rson(u)][0];
            s[u][2] = s[lson(u)][1] + s[rson(u)][1];
            for (int i = 2; i <= 3; i++)
                s[u][3] += s[lson(u)][i] + s[rson(u)][i];
        } else {
            for (int i = 0; i <= 3; i++)
                s[u][i] = s[lson(u)][i] + s[rson(u)][i];
        }
    }
}

inline void maintain(int u, int d) {
    v[u] += d;
    pushup(u);
}

void build (int u, int l, int r) {
    lc[u] = l;
    rc[u] = r;
    v[u] = 0;

    if (l == r) {
        maintain(u, 0);
        return ;
    }

    int mid = (l + r) / 2;
    build (lson(u), l, mid);
    build (rson(u), mid + 1, r);
    pushup(u);
}

void modify (int u, int l, int r, int d) {

    if (l <= lc[u] && rc[u] <= r) {
        maintain(u, d);
        return;
    }

    int mid = (lc[u] + rc[u]) / 2;
    if (l <= mid)
        modify(lson(u), l, r, d);
    if (r > mid)
        modify(rson(u), l, r, d);
    pushup(u);
}

struct Seg {
    int x, l, r, d;
    Seg (int x = 0, int l = 0, int r = 0, int d = 0) {
        this->x = x;
        this->l = l;
        this->r = r;
        this->d = d;
    }
};

typedef long long ll;
vector<Seg> g[1005];

inline bool cmp (const Seg& a, const Seg& b) {
    return a.x < b.x;
}

void init () {
    int n, x1, x2, y1, y2, z1, z2;

    scanf("%d", &n);
    for (int i = 0; i <= 1000; i++)
        g[i].clear();

    for (int i = 0; i < n; i++) {
        scanf("%d%d%d%d%d%d", &x1, &y1, &z1, &x2, &y2, &z2);
        for (int i = z1; i < z2; i++) {
            g[i + 500].push_back(Seg(x1, y1, y2, 1));
            g[i + 500].push_back(Seg(x2, y1, y2, -1));
        }
    }
}

inline int find (int k) {
    return lower_bound(pos.begin(), pos.end(), k) - pos.begin();
}

ll solve (int idx) {

    if (g[idx].size() == 0)
        return 0;

    ll ret = 0;
    pos.clear();
    sort(g[idx].begin(), g[idx].end(), cmp);

    for (int i = 0; i < g[idx].size(); i++) {
        pos.push_back(g[idx][i].l);
        pos.push_back(g[idx][i].r);
    }

    sort(pos.begin(), pos.end());
    build(1, 0, pos.size());

    for (int i = 0; i < g[idx].size(); i++) {
        modify(1, find(g[idx][i].l), find(g[idx][i].r) - 1, g[idx][i].d);
        if (i + 1 != g[idx].size())
            ret += 1LL * s[1][3] * (g[idx][i+1].x - g[idx][i].x);
    }
    return ret;
}

int main () {
    int cas;
    scanf("%d", &cas);
    for (int kcas = 1; kcas <= cas; kcas++) {
        init();

        ll ans = 0;
        for (int i = 0; i <= 1000; i++)
            ans += solve(i);

        printf("Case %d: %I64d\n", kcas, ans);
    }
    return 0;
}
时间: 2024-10-15 13:12:10

hdu 3642 Get The Treasury(扫描线)的相关文章

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 线段树+离散化+扫描线

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

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 - 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

HDU 3642 Get The Treasury (线段树扫描线)

题意:给你一些长方体,问你覆盖三次及以上的体积有多大 首先我们观察x轴y轴一样很大,但是z轴很小,所以我们可以枚举z轴(-500,500),注意我们枚举的是每一段长度为一的z轴的xy轴的面积而不是点.接着就是求在这一段内的矩形面积并的变形 注意我们要首先计算,再插入线段求面积并 #include<set> #include<map> #include<queue> #include<stack> #include<cmath> #include&

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(线段树+扫描线)

三维扫描线,枚举z寻找相交区间的立方体,然后直接扫描线求xy平面的相交三次及以上面积,乘以z区间求和就可以了 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; const int maxn=1e6+100; const int maxm=2000+500; int col[maxn<<2]; int

hdu 1255 覆盖的面积(扫描线)

http://acm.hdu.edu.cn/showproblem.php?pid=1255 一道挺简单的题,让我折腾了许久.主要卡在了更新节点后维护父亲节点上.后来思路明确了就很容易了. 节点信息: l,r:区间端点 cnt:区间被覆盖的次数,cnt = 0说明没有被完全覆盖. len1:区间被覆盖的长度 len2:区间至少被两条线段覆盖的长度. 只要找到父亲节点与子节点在len1,len2,cnt的关系就简单了. #include <stdio.h> #include <iostre