HDU3642 Get The Treasury —— 求矩形交体积 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/HDU-3642
Jack can’t get the total volume of the treasury because these regions don’t always contain treasury. Through years of experience, he discovers that if a region is detected that may have treasury at more than two different spots, the region really exist treasure. And now Jack only wants to know the minimum volume of the treasury.
Now Jack entrusts the problem to you.
InputThe first line of the input file contains a single integer t, the number of test cases, followed by the input data for each test case.
Each test case is given in some lines. In the first line there is an integer n (1 ≤ n ≤ 1000), the number of spots on the surface of the earth that he had detected. Then n lines follow, every line contains six integers x 1, y 1, z 1, x 2, y 2 and z2, separated by a space. The absolute value of x and y coordinates of the vertices is no more than 10 6, and that of z coordinate is no more than 500.
OutputFor each test case, you should output “Case a: b” in a single line. a is the case number, and b is the minimum volume of treasury. The case number is counted from one.
Sample Input
2
1
0 0 0 5 6 4
3
0 0 0 5 5 5
3 3 3 9 10 11
3 3 3 13 20 45
Sample Output
Case 1: 0
Case 2: 8
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e3+; int times[MAXN<<]; //times为该区间被覆盖的次数
int one[MAXN<<], two[MAXN<<], more[MAXN<<];
int Z[MAXN<<], X[MAXN<<]; //Z、X分别用于离散化Z坐标和X坐标 struct Cube
{
int x1, y1, z1,x2, y2, z2;
}cube[MAXN]; struct Line
{
int le, ri, h, id;
bool operator<(const Line &a)const{
return h<a.h;
} }line[MAXN<<]; void push_up(int u, int l, int r)
{
if(times[u]>=) //该区间被覆盖三次
{
more[u] = X[r] - X[l];
two[u] = X[r] - X[l];
one[u] = X[r] - X[l];
}
else if(times[u]==) //两次
{
more[u] = (l+==r)?:(one[u*]+one[u*+]);
two[u] = X[r] - X[l];
one[u] = X[r] - X[l];
}
else if(times[u]==) //一次
{
more[u] = (l+==r)?:(two[u*]+two[u*+]);
two[u] = (l+==r)?:(one[u*]+one[u*+]);
one[u] = X[r] - X[l];
}
else //没有被覆盖
{
more[u] = (l+==r)?:(more[u*]+more[u*+]);
two[u] = (l+==r)?:(two[u*]+two[u*+]);
one[u] = (l+==r)?:(one[u*]+one[u*+]);
}
} //此种线段树的操作对象为连续型,即最小的元素为长度为1的区间[l,r],其中l和r只代表端点(r-l>=1),用于确定
//区间的位置和长度,l和r本身没有特别的含义。而以往做的什么单点更新之类的,都属于离散型,在l处和r处是有含义的
void add(int u, int l, int r, int x, int y, int v)
{
if(x<=l && r<=y)
{
times[u] += v;
push_up(u, l, r);
return;
} int mid = (l+r)>>;
if(x<=mid-) add(u*, l, mid, x, y, v);
if(y>=mid+) add(u*+, mid, r, x, y, v);
push_up(u, l, r);
} int main()
{
int T, n;
scanf("%d", &T);
for(int kase = ; kase<=T; kase++)
{
scanf("%d", &n);
int numZ = ;
for(int i = ; i<=n; i++)
{
scanf("%d%d%d", &cube[i].x1,&cube[i].y1,&cube[i].z1);
scanf("%d%d%d", &cube[i].x2,&cube[i].y2,&cube[i].z2);
Z[++numZ] = cube[i].z1; Z[++numZ] = cube[i].z2;
} sort(Z+, Z++numZ); //离散化Z坐标
numZ = unique(Z+, Z++numZ) - (Z+); LL volume = ;
for(int i = ; i<numZ; i++) //枚举每一个平面(平面垂直于Z轴)
{
int numLine = , numX = ;
for(int j = ; j<=n; j++)
if(cube[j].z1<=Z[i] && Z[i+]<=cube[j].z2) //获得在此平面有效的长方体,然后保存他们在此平面的上下边。
{
line[++numLine].le = cube[j].x1; line[numLine].ri = cube[j].x2;
line[numLine].h = cube[j].y1; line[numLine].id = ;
line[++numLine].le = cube[j].x1; line[numLine].ri = cube[j].x2;
line[numLine].h = cube[j].y2; line[numLine].id = -;
X[++numX] = cube[j].x1; X[++numX] = cube[j].x2;
} sort(line+, line++numLine); //在此平面中,根据高度(即y坐标)对线段进行排序
sort(X+, X++numX);
numX = unique(X+, X++numX) - (X+); //离散化X坐标 memset(times, , sizeof(times));
memset(more, , sizeof(more));
memset(two, , sizeof(two));
memset(one, , sizeof(one)); LL area = ;
for(int j = ; j<numLine; j++) //计算此平面的有效面积
{
int l = upper_bound(X+, X++numX, line[j].le) - (X+);
int r = upper_bound(X+, X++numX, line[j].ri) - (X+);
add(, , numX, l, r, line[j].id);
area += 1LL*more[]*(line[j+].h-line[j].h);
}
volume += 1LL*area*(Z[i+]-Z[i]); //计算两个平面之间的体积,然后再累加
}
printf("Case %d: %lld\n", kase, volume);
}
}
HDU3642 Get The Treasury —— 求矩形交体积 线段树 + 扫描线 + 离散化的更多相关文章
- HDU1255 覆盖的面积 —— 求矩形交面积 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/HDU-1255 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input输入数据的第一行是一个正整数T(1<= ...
- HDU1542 Atlantis —— 求矩形面积并 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/HDU-1542 There are several ancient Greek texts that contain descript ...
- POJ-1151-Atlantis(线段树+扫描线+离散化)[矩形面积并]
题意:求矩形面积并 分析:使用线段树+扫描线...因为坐标是浮点数的,因此还需要离散化! 把矩形分成两条边,上边和下边,对横轴建树,然后从下到上扫描上去,用col表示该区间有多少个下边,sum代表该区 ...
- POJ 1177 Picture(线段树 扫描线 离散化 求矩形并面积)
题目原网址:http://poj.org/problem?id=1177 题目中文翻译: 解题思路: 总体思路: 1.沿X轴离散化建树 2.按Y值从小到大排序平行与X轴的边,然后顺序处理 如果遇到矩形 ...
- hdu 4419 线段树 扫描线 离散化 矩形面积
//离散化 + 扫描线 + 线段树 //这个线段树跟平常不太一样的地方在于记录了区间两个信息,len[i]表示颜色为i的被覆盖的长度为len[i], num[i]表示颜色i 『完全』覆盖了该区间几层. ...
- POJ1177 Picture —— 求矩形并的周长 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/POJ-1177 A number of rectangular posters, photographs and other pict ...
- poj 1177 Picture (线段树 扫描线 离散化 矩形周长并)
题目链接 题意:给出n个矩形,每个矩形给左下 和 右上的坐标,求围成的周长的长度. 分析: 首先感谢大神的博客,最近做题经常看大神的博客:http://www.cnblogs.com/kuangbin ...
- HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- 覆盖的面积 HDU - 1255 线段树+扫描线+离散化 求特定交叉面积
#include<cstdio> #include<map> #include<algorithm> using namespace std; ; struct N ...
随机推荐
- Struts2执行原理
[原理图] [MVC] [执行过程(重要!!!!!)] 1) 客户端浏览器发出请求时,被Tomcat服务器所接收.Tomcat容器将用户的请求封装为HttpServletRequest对象 2) 请求 ...
- Haproxy的安装与配置
一.Haproxy概念 Haproxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.Haproxy特别适用于那些负载特大的web站点,这 ...
- HDU-3746Cyclic Nacklace,next数组简单应用。
Cyclic Nacklace 节省篇幅不粘题面了... 看懂题后脑袋里略过KMP,学过但没怎么用过,又直接跳下一题了.. 题意:给定一个字符串,可以从两边加上一些字符使其有循环节..求最少需要加多少 ...
- POJ 3248 Catch That Cow
http://poj.org/problem?id=3278 二维BFS #include <iostream> #include <stdio.h> #include < ...
- eclipse 修改Java代码 不用重新启动tomcat
例子: 1.在tomcat server.xml文件配置加上这句话: <Context debug="0" docBase="C:\Users\admin\Desk ...
- Hadoop经典书籍资料收藏(35本)转
原文地址:http://www.hadoopor.com/thread-5128-1-2.html 1."Hadoop.Operations.pdf.zip" http://vdi ...
- java开发面试大全刷题整理
题目源自Java团长公众号,内容个人整理,来源于各大博客,未经允许,不准摘抄,仅供分享,不做商业使用. 本分享多数为浅层知识体系,更为底层的还请自行多写写代码,若有不对之处,望广大的人才指点,不喜勿喷 ...
- Spring Data Redis与Jedis的选择(转)
说明:内容可能有点旧,需要在业务上做权衡. Redis的客户端有两种实现方式,一是可以直接调用Jedis来实现,二是可以使用Spring Data Redis,通过Spring的封装来调用.应该使用哪 ...
- 框架-弹出选择框(Jquery传递Json数组)
给一个button按钮,执行方法 Json传值$("body").on("click", "#btnsure", function() { ...
- how to read openstack code: action extension
之前我们看过了core plugin, service plugin 还有resource extension. resource extension的作用是定义新的资源.而我们说过还有两种exten ...