HDU1255 覆盖的面积 —— 求矩形交面积 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/HDU-1255
Input输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1<=N<=1000),代表矩形的数量,然后是N行数据,每一行包含四个浮点数,代表平面上的一个矩形的左上角坐标和右下角坐标,矩形的上下边和X轴平行,左右边和Y轴平行.坐标的范围从0到100000.
注意:本题的输入数据较多,推荐使用scanf读入数据. 
Output对于每组测试数据,请计算出被这些矩形覆盖过至少两次的区域的面积.结果保留两位小数. 
Sample Input
2
5
1 1 4 2
1 3 3 7
2 1.5 5 4.5
3.5 1.25 7.5 4
6 3 10 7
3
0 0 1 1
1 0 2 1
2 0 3 1
Sample Output
7.63
0.00
代码如下:
#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 = 1e5+; struct line
{
double le, ri, h;
int id;
bool operator<(const line &a)const{
return h<a.h;
}
}Line[MAXN]; int times[MAXN<<];
double X[MAXN], one[MAXN], more[MAXN<<];
//X用于离散化横坐标,times为此区间被覆盖的次数,
//one为此区间被覆盖一次的长度和, more为此区间至少被覆盖两次的长度和 void push_up(int u, int l, int r)
{
if(times[u]>=) //该区间被覆盖了两次
{
more[u] = X[r] - X[l];
one[u] = X[r] - X[l];
}
else if(times[u]==) //该区间被覆盖了一次
{
more[u] = one[u*]+one[u*+];
one[u] = X[r] - X[l];
}
else //该区间没有被覆盖
{
if(l+==r) //该区间为单位区间
more[u] = one[u] = ;
else
{
more[u] = more[u*]+more[u*+];
one[u] = 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 val)
{
if(x<=l && r<=y)
{
times[u] += val;
push_up(u, l, r);
return;
} int mid = (l+r)>>;
if(x<=mid-) add(u*, l, mid, x, y, val);
if(y>=mid+) add(u*+, mid, r, x, y, val);
push_up(u, l, r);
} int main()
{
int n, T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = ; i<=n; i++)
{
double x1, y1, x2, y2;
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
Line[i].le = Line[i+n].le = x1;
Line[i].ri = Line[i+n].ri = x2;
Line[i].h = y1; Line[i+n].h = y2;
Line[i].id = ; Line[i+n].id = -;
X[i] = x1; X[i+n] = x2;
} sort(Line+, Line++*n);
sort(X+, X++*n);
int m = unique(X+, X++*n) - (X+); //去重 memset(more, , sizeof(more));
memset(one, , sizeof(one));
memset(times, , sizeof(times)); double ans = ;
for(int i = ; i<=*n-; i++)
{
int l = upper_bound(X+, X++m, Line[i].le) - (X+);
int r = upper_bound(X+, X++m, Line[i].ri) - (X+);
add(, , m, l, r, Line[i].id);
ans += more[]*(Line[i+].h-Line[i].h);
}
printf("%.2f\n", ans);
}
}
HDU1255 覆盖的面积 —— 求矩形交面积 线段树 + 扫描线 + 离散化的更多相关文章
- HDU3642 Get The Treasury —— 求矩形交体积 线段树 + 扫描线 + 离散化
		
题目链接:https://vjudge.net/problem/HDU-3642 Jack knows that there is a great underground treasury in a ...
 - POJ-1151-Atlantis(线段树+扫描线+离散化)[矩形面积并]
		
题意:求矩形面积并 分析:使用线段树+扫描线...因为坐标是浮点数的,因此还需要离散化! 把矩形分成两条边,上边和下边,对横轴建树,然后从下到上扫描上去,用col表示该区间有多少个下边,sum代表该区 ...
 - POJ 1177 Picture(线段树 扫描线 离散化 求矩形并面积)
		
题目原网址:http://poj.org/problem?id=1177 题目中文翻译: 解题思路: 总体思路: 1.沿X轴离散化建树 2.按Y值从小到大排序平行与X轴的边,然后顺序处理 如果遇到矩形 ...
 - HDU1542 Atlantis —— 求矩形面积并 线段树 + 扫描线 + 离散化
		
题目链接:https://vjudge.net/problem/HDU-1542 There are several ancient Greek texts that contain descript ...
 - hdu 4419 线段树 扫描线 离散化 矩形面积
		
//离散化 + 扫描线 + 线段树 //这个线段树跟平常不太一样的地方在于记录了区间两个信息,len[i]表示颜色为i的被覆盖的长度为len[i], num[i]表示颜色i 『完全』覆盖了该区间几层. ...
 - HDU 1255 覆盖的面积 (线段树+扫描线+离散化)
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 题意很清楚,就是让你求矩阵之间叠加层数大于1的矩形块的面积和. 因为n只有1000,所以我离散化 ...
 - HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)
		
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
 - POJ1177 Picture —— 求矩形并的周长 线段树 + 扫描线 + 离散化
		
题目链接:https://vjudge.net/problem/POJ-1177 A number of rectangular posters, photographs and other pict ...
 - 覆盖的面积 HDU - 1255 线段树+扫描线+离散化  求特定交叉面积
		
#include<cstdio> #include<map> #include<algorithm> using namespace std; ; struct N ...
 
随机推荐
- struts2访问或添加几个属性(request/session/application属性)
			
https://blog.csdn.net/hebiao100/article/details/7385055 struts2添加request.session.application属性 第一种方法 ...
 - java数据结构简单点
			
java常用的数据结构一 集合框架主要就是Collection和Map: 1.Collection包含了List和Set两大分支. (1)List是一个有序的集合,每一个元素都有它的索引.第一个元素的 ...
 - HA架构
			
HA架构是个什么东西? 阅读文章:浅谈web应用的负载均衡.集群.高可用(HA)解决方案
 - BZOJ 1022: [SHOI2008]小约翰的游戏John【anti-SG】
			
Description 小约翰经常和他的哥哥玩一个非常有趣的游戏:桌子上有n堆石子,小约翰和他的哥哥轮流取石子,每个人取的时候,可以随意选择一堆石子,在这堆石子中取走任意多的石子,但不能一粒石子也不取 ...
 - hdu 4045 Machine scheduling [ dp + 斯特林数]
			
传送门 Machine scheduling Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
 - Windows Server 2003的一些优化设置 (转至网络)
			
2003序列号:JCHKR-888KX-27HVK-DT88X-T767M1.禁用配置服务器向导: 禁止“配置你的服务器”(Manage Your Server)向导的出现:在控制面板(Control ...
 - 库操作&表操作
			
系统数据库 ps:系统数据库: mysql 授权库,主要存储系统用户的 权限信息 test MySQL数据库系统自动创建的 测试数据库 ination_schema 虚拟库,不占用磁盘空间,存储的是数 ...
 - Codeforces 645D Robot Rapping Results Report【拓扑排序+二分】
			
题目链接: http://codeforces.com/problemset/problem/645/D 题意: 给定n个机器人的m个能力大小关系,问你至少要前几个大小关系就可以得到所有机器人的能力顺 ...
 - SpringMVC Ueditor1.4.3 未找到上传数据
			
ueditor自事的fileupload组件与spring的有冲突.将那个类BinaryUploader 重写就可以了 return storageState; ...
 - Delphi中匿名方法动态绑定事件
			
应恢弘之约,写了一个对其发布的匿名函数动态绑定到事件的封装,代码如下: type TAnonEvent=class public class function Wrap<T1,T2>(On ...