不得不说,这是一题很经典的体积并。。

然而还是debug了2个多小时...

首先思路:按z的大小排序。

然后相当于扫描面一样,,从体积的最下方向上方扫描,遇到这个面

就将相应的两条线增加到set中,或者从set中删除,然后再对set中的全部边,求一次面积并

因为最后求出来的是至少有3个体积叠加的部分的体积。

所以须要维护3个节点,然后push_up会略微啰嗦一点...

刚開始在set插入线段和删除线段的时候。搞错了线段所在面的编号,。然后一直例子都过不去也是醉了...

总的来说代码比較冗长须要足够细心才干敲对..

#include<map>
#include<set>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional> using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define root 1,rear-1,1 int const MX = 4e3 + 5; struct Side {
int z, x1, y1, x2, y2, d, id;
bool operator<(const Side &b)const {
if(z == b.z) return d < b.d;
return z < b.z;
}
Side() {}
Side(int _z, int _id, int _x1, int _y1, int _x2, int _y2, int _d) {
z = _z; x1 = _x1; y1 = _y1; x2 = _x2; y2 = _y2; d = _d; id = _id;
}
} A[MX]; struct Line {
int y, x1, x2, d, id;
bool operator<(const Line &b)const {
if(y == b.y) {
if(d == b.d) return id < b.id;
return d < b.d;
}
return y < b.y;
}
Line() {}
Line(int _y, int _id, int _d, int _x1, int _x2) {
y = _y; x1 = _x1; x2 = _x2; d = _d; id = _id;
}
}; int X[MX], rear;
int S1[MX << 2], S2[MX << 2], S3[MX << 2], cnt[MX << 2];
set<Line>work; int BS(int x) {
int L = 1, R = rear, m;
while(L <= R) {
m = (L + R) >> 1;
if(X[m] == x) return m;
if(X[m] > x) R = m - 1;
else L = m + 1;
}
return -1;
} void push_up(int l, int r, int rt) {
if(cnt[rt]) {
S1[rt] = X[r + 1] - X[l];
if(cnt[rt] == 1) {
S2[rt] = S1[rt << 1] + S1[rt << 1 | 1];
S3[rt] = S2[rt << 1] + S2[rt << 1 | 1];
} else {
S2[rt] = X[r + 1] - X[l];
if(cnt[rt] == 2) {
S3[rt] = S1[rt << 1] + S1[rt << 1 | 1];
} else {
S3[rt] = X[r + 1] - X[l];
}
}
} else if(l == r) S1[rt] = S2[rt] = S3[rt] = 0;
else {
S1[rt] = S1[rt << 1] + S1[rt << 1 | 1];
S2[rt] = S2[rt << 1] + S2[rt << 1 | 1];
S3[rt] = S3[rt << 1] + S3[rt << 1 | 1];
}
} void update(int L, int R, int d, int l, int r, int rt) {
if(L <= l && r <= R) {
cnt[rt] += d;
push_up(l, r, rt);
return;
} int m = (l + r) >> 1;
if(L <= m) update(L, R, d, lson);
if(R > m) update(L, R, d, rson);
push_up(l, r, rt);
} LL solve() {
memset(S1, 0, sizeof(S1));
memset(S2, 0, sizeof(S2));
memset(cnt, 0, sizeof(cnt)); LL ans = 0;
int last = 0;
for(set<Line>::iterator it = work.begin(); it != work.end(); it++) {
ans += (LL)(it->y - last) * S3[1];
update(BS(it->x1), BS(it->x2) - 1, it->d, root);
last = it->y;
}
return ans;
} int main() {
//freopen("input.txt", "r", stdin);
int T, n, ansk = 0;
scanf("%d", &T);
while(T--) {
rear = 0;
work.clear(); scanf("%d", &n);
for(int i = 1; i <= n; i++) {
int x1, y1, z1, x2, y2, z2;
scanf("%d%d%d%d%d%d", &x1, &y1, &z1, &x2, &y2, &z2);
A[i] = Side(z1, i, x1, y1, x2, y2, 1);
A[i + n] = Side(z2, i, x1, y1, x2, y2, -1);
X[++rear] = x1; X[++rear] = x2;
}
sort(A + 1, A + 1 + 2 * n);
sort(X + 1, X + 1 + rear);
rear = unique(X + 1, X + 1 + rear) - X - 1; LL ans = 0;
int last = 0;
printf("Case %d: ", ++ansk);
for(int i = 1; i <= 2 * n; i++) {
ans += (LL)(A[i].z - last) * solve(); if(A[i].d > 0) {
work.insert(Line(A[i].y1, A[i].id, 1, A[i].x1, A[i].x2));
work.insert(Line(A[i].y2, A[i].id, -1, A[i].x1, A[i].x2));
} else {
work.erase(Line(A[i].y1, A[i].id, 1, A[i].x1, A[i].x2));
work.erase(Line(A[i].y2, A[i].id, -1, A[i].x1, A[i].x2));
}
last = A[i].z;
}
printf("%I64d\n", ans);
}
return 0;
}

线段树 hdu3642 Get The Treasury的更多相关文章

  1. HDU3642 Get The Treasury —— 求矩形交体积 线段树 + 扫描线 + 离散化

    题目链接:https://vjudge.net/problem/HDU-3642 Jack knows that there is a great underground treasury in a ...

  2. hdu3642 Get The Treasury 线段树--扫描线

    Jack knows that there is a great underground treasury in a secret region. And he has a special devic ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3642 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  4. HDU 3642 Get The Treasury 线段树+分层扫描线

    http://www.acmerblog.com/hdu-3642-get-the-treasury-6603.html 学习:三维就是把竖坐标离散化分层,每一层进行线段树二维面积并就好了

  5. HDU 3642 Get The Treasury ( 线段树 求长方体体积并 )

    求覆盖三次及其以上的长方体体积并. 这题跟 http://wenku.baidu.com/view/d6f309eb81c758f5f61f6722.html 这里讲的长方体体积并并不一样. 因为本题 ...

  6. [转载]完全版线段树 by notonlysuccess大牛

    原文出处:http://www.notonlysuccess.com/ (好像现在这个博客已经挂掉了,在网上找到的全部都是转载) 今天在清北学堂听课,听到了一些很令人吃惊的消息.至于这消息具体是啥,等 ...

  7. 【转】线段树完全版~by NotOnlySuccess

    线段树完全版  ~by NotOnlySuccess 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章了,觉 ...

  8. 《完全版线段树》——notonlysuccess

    转载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文 ...

  9. 【转】 线段树完全版 ~by NotOnlySuccess

    载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章 ...

随机推荐

  1. iOS数据持久化 -- Core Data

    Core Data是一个功能强大的层,位于SQLite数据库之上,它避免了SQL的复杂性,能让我们以更自然的方式与数据库进行交互.Core Data将数据库行转换为OC对象(托管对象)来实现,这样无需 ...

  2. Pocket英语语法---三、英语动词的特点是什么

    Pocket英语语法---三.英语动词的特点是什么 一.总结 一句话总结:即表示时间(时态),又表示人数(单复数) 1.第十七讲,不定量表达法? 1.a few为肯定含义几个,few为否定含义没几个, ...

  3. Kali linux 2016.2(Rolling)里Metasploit的OpenVAS

    不多说,直接上干货! 关于OpenAVS的概念,我这里不多赘述. 前提得,大家要先安装好OpenVAS!!! 我们都知道,BT5中已经预先安装好了OpenVAS网络漏洞扫描工具,我们只需进行一些配置即 ...

  4. jquery实现上下浮动

    jquery实现上下浮动: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  5. 禁用cache

    Z:\src\services\network\network_context.cc:http_cache_enabled

  6. bzoj2763 [JLOI]飞行路线 分层图最短路

    问题描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的 ...

  7. celery 学习

    1. 列出计划的ETA任务(worker) celery -A proj inspect scheduled 参考文档:http://docs.celeryproject.org/en/latest/ ...

  8. UVALive 5790 Ball Stacking DP

    DP的方向真的很重要,这题做的时候死活想不出来,看了题解以后恍然大悟原来这么简单. 题意: 有n层堆成金字塔状的球,若你要选一个球,你必须把它上面那两个球取了,当然也可以一个不取.求选的球最大的权值和 ...

  9. Struts(21)OGNL具体解释

    Struts2 中内置了OGNL表达式的支持,使得Struts2的具有比Struts1更为强大的数据訪问的功能.本文主要解说OGNL的用法.并不会去解说一些原理性的东西.想要了解的朋友能够自己去查阅相 ...

  10. “Vbox安装CentOS系统”之低级错误一例:版本号的选择

    日        期:2014年7月18日 错误描写叙述:虚拟机系统版本号选择错误,导致无法引导进入光盘安装 错误重演: 因为混淆了centos和rehat的版本号概念.错把centos作为一种新的版 ...