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 ...
随机推荐
- Couchbase IV(管理与维护)
Couchbase IV(管理与维护) 管理 常用命令 Command Description server-list List all servers in a cluster server-inf ...
- 七、整合SQL基础和PL-SQL基础
--Oracle数据库重要知识点整理 2017-01-24 soulsjie 目录 --一.创建及维护表... 2 --1.1 创建... 2 --1.2 维护表... 2 --二.临时表的分类.创建 ...
- C# 通过HTTP代理访问Socket来获取邮件
C# 通过HTTP代理访问Socket来获取邮件 关键穿透代理的代码(通过HTTP代理获取TcpClent) public class ClientHelper { public static Tcp ...
- asp.net 引发类型为“System.OutOfMemoryException”的异常
asp.net 引发类型为“System.OutOfMemoryException”的异常通常发生在IIS进程获取不到内存时. 临时解决方法是: 回收IIS的应用程序池. 如果要比较好的解决办法是: ...
- Flex嵌入HTML页面
这段时间一直在苦心研究Flex,今天突然想,我们平时都是把swf放到网页中,怎么才能把网页嵌入到Flex中呢?我查了一些资料,然后经过自己的不懈努力,终于搞定. 为了方便,写了个嵌入HTML页面的代理 ...
- 将Sublime Text 2搭建成一个好用的IDE(转)
原文地址 将Sublime Text 2搭建成一个好用的IDE 说起编辑器,可能大部分人要推荐的是Vim和Emacs,本人用过Vim,功能确实强大,但是不是很习惯,之前一直有朋友推荐SUblime T ...
- java基础语法1
一:基础语法之--标识符,修饰符,关键字 1.标识符: 定义:类名.变量名以及方法名都被称为标识符.自定义的名字. 注意: ·所有的标识符都应该以字母(A-Z或者a-z),美元符($).或者下划线(_ ...
- MongoDB学习day05--MongDB开启权限验证,创建用户
一.MongoDB账户权限配置 1.创建超级管理员用户 use admin db.createUser({ user:'admin', pwd:'123456', roles:[{role:'root ...
- ARM PPC 交叉编译环境搭建
ARM: 1,下载cross-3.4.1.tar.bz2 2,在/usr/local目录下建立arm目录 3,将cross-3.4.1.tar.bz2拷贝到arm目录下 4,tar jxvf cros ...
- 中间件序列TDATASET为BUFFER演示代码
procedure SendStream(const AStream: TStream);var Buffer: array[0..4095] of Byte; // 每包最大4K StartPos, ...