题目链接:https://vjudge.net/problem/HDU-3642

Jack knows that there is a great underground treasury in a secret region. And he has a special device that can be used to detect treasury under the surface of the earth. One day he got outside with the device to ascertain the treasury. He chose many different locations on the surface of the earth near the secret region. And at each spot he used the device to detect treasury and got some data from it representing a region, which may contain treasury below the surface. The data from the device at each spot is six integers x 1, y 1, z 1, x 2, y 2 and z 2 (x 1<x 2, y 1<y 2, z 1<z 2). According to the instruction of the device they represent the range of x, y and z coordinates of the region. That is to say, the x coordinate of the region, which may contain treasury, ranges from x 1 to x 2. So do y and z coordinates. The origin of the coordinates is a fixed point under the ground. 
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);
}
}

最新文章

  1. Java经典实例:使用正则表达式:测试模式
  2. 使用ajaxfileupload.js上传文件
  3. quick-cocos2d-x之testlua之VisibleRect.lua
  4. Jquery中的filter()详细说明和transition的用法
  5. b2c项目基础架构分析(一)b2c 大型站点方案简述 已补充名词解释
  6. linux中的虚拟化网络模型及各种模型实现
  7. javascript笔记——JavaScript经典实例
  8. android studio 真机调试
  9. Apache+Tomcat +mod_proxy集群负载均衡及session
  10. C语言的预编译
  11. java中几种加/解密API
  12. 记录日常Linux常用软件
  13. 解决sqlserver还原数据库失败命令
  14. 解析Java分布式系统中的缓存架构(上)
  15. es6+的javascript拓展内容
  16. Visual Studio Ultimate 2013
  17. 开发一个微信小程序实例教程
  18. Vue.js常用指令:v-for
  19. AM二次开发中选择指定范围内的对象
  20. k8s服务发现和负载均衡(转)

热门文章

  1. Vue如何在webpack设置代理解决跨域问题
  2. java环境配置——工具下载地址
  3. Java学习之理解递归
  4. Python+fiddler(基于Cookie绕过验证码自动登录)
  5. 一个jboss启动shell脚本
  6. [Kubernetes]kubectl命令补全出错
  7. [codeforces538E]Demiurges Play Again
  8. 【最长上升子序列记录路径(n^2)】HDU 1160 FatMouse&#39;s Speed
  9. 【ZJOI2017 Round1练习】D2T2 iqtest(排列组合)
  10. 一段曲折的copy路程