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 ...
随机推荐
- python模块以及导入出现ImportError: No module named ‘xxx‘问题
python中,每个py文件被称之为模块,每个具有__init__.py文件的目录被称为包.只要模块或者包所在的目录在sys.path中,就可以使用import 模块或import 包来使用如果你要使 ...
- Poj1704:staircase nim【博弈】
题目大意:有一个无限长的一维的棋盘,棋盘上N个格子放置着棋子.两个人轮流操作,每次操作能选择其中一个棋子向左移动,但不能越过其它棋子或者两枚棋子放在同一格中,最后不能操作的人算输,问先手是否必胜? 思 ...
- 用DW制作简单的浮动广告
原文发布时间为:2008-11-08 -- 来源于本人的百度文章 [由搬家工具导入] 浮动广告可以用层和时间轴结合做出,先选择你的dreamweaver“窗口”,然后从“窗口”菜单中选择“时间轴”,时 ...
- Mysqli的常用函数
Mysqli的常用函数 连接数据库: $res = @mysqli_connect($host,$username,$pass,$db_name); if (mysqli_connect_errno( ...
- HDU 5521 Meeting【最短路】
今天旁观了Angry_Newbie的模拟区域赛(2015shenyang) 倒着看最先看的M题,很明显的最短路问题,在我看懂的时候他们已经开始敲B了. 后来听说D过了很多人.. D题一看是个博弈,给了 ...
- PAT (Advanced Level) 1032. Sharing (25)
简单题,不过数据中好像存在有环的链表...... #include<iostream> #include<cstring> #include<cmath> #inc ...
- eslint (js代码检查)
eslint 是一个应用广泛的javascript代码检查工具. 能检测变量名重复等等... 1.安装 npm install -g eslint 2.初始化 会在当前目录下生成一个.eslintrc ...
- how to read openstack code: loading process
之前我们了解了neutron的结构,plugin 和 extension等信息.这一章我们看一下neutron如何加载这些plugin和extension.也就是neutron的启动过程.本文涉及的代 ...
- linux的shell的until循环举例说明
执行脚本: sh login.sh user,其中user为第一个参数 如下所示,如果用户‘user’登录,'who | grep "$1"'为true,until循环结束,程序继 ...
- Linux下的lds链接脚本简介(一)
转载自:http://linux.chinaunix.net/techdoc/beginner/2009/08/12/1129972.shtml 一. 概论 每一个链接过程都由链接脚本(linker ...