hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]
Atlantis
  Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11777    Accepted Submission(s): 4983
fabled island Atlantis. Some of these texts even include maps of parts of
the island. But unfortunately, these maps describe different regions of
Atlantis. Your friend Bill has to know the total area for which maps exist.
You (unwisely) volunteered to write a program that calculates this quantity.
line containing a single integer n (1<=n<=100) of available maps. The n
following lines describe one map each. Each of these lines contains four
numbers x1;y1;x2;y2 (0<=x1<x2<=100000;0<=y1<y2<=100000), not necessarily
integers. The values (x1; y1) and (x2;y2) are the coordinates of the
top-left resp. bottom-right corner of the mapped area.
The input file is terminated by a line containing a single 0. Don’t process
	it.
of each section must be “Test case #k”, where k is the number of the test
case (starting with 1). The second one must be “Total explored area: a”,
where a is the total explored area (i.e. the area of the union of all
rectangles in this test case), printed exact to two digits to the right of
the decimal point.
Output a blank line after each test case.
 
【题意】:
给你n个矩形的左下角(x1,y1),右上角(x2,y2),让你求n个矩形面积的并
【分析】:见这里
【代码】:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define m(s) memset(s,0,sizeof s)
#define lc k<<1
#define rc k<<1|1
using namespace std;
const int N=1e5+;
struct node{
double xl,xr,h;
int id;
bool operator < (const node &a)const{
return h<a.h;
}
}b[N];
int n,kase,tag[N<<];
double sum[N<<],c[N];
void updata(int k,int l,int r){
if(tag[k]) sum[k]=c[r+]-c[l];//当前区间被线段覆盖
else if(l==r) sum[k]=;//已经到了叶子结点且没有被覆盖
else sum[k]=sum[lc]+sum[rc];// 没有完全被覆盖,但是还没到叶子结点,从其子区间中获得信息
}
void change(int k,int l,int r,int x,int y,int val){
if(x<=l&&r<=y){
tag[k]+=val;updata(k,l,r);
return ;
}
int mid=l+r>>;
if(x<=mid) change(lc,l,mid,x,y,val);
if(y>mid) change(rc,mid+,r,x,y,val);
updata(k,l,r);
}
int main(){
while(~scanf("%d",&n)&&n){
int cnt=;double ans=;
double x1,x2,y1,y2;
m(sum);m(tag);
for(int i=;i<=n;i++){
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
b[++cnt]=(node){x1,x2,y1,};c[cnt]=x1;
b[++cnt]=(node){x1,x2,y2,-};c[cnt]=x2;
}
sort(b+,b+cnt+);
sort(c+,c+cnt+);
int t=unique(c+,c+cnt+)-(c+);
for(int i=;i<cnt;i++){
x1=lower_bound(c+,c+t+,b[i].xl)-c;
x2=lower_bound(c+,c+t+,b[i].xr)-c-;
change(,,t,x1,x2,b[i].id);
ans+=(b[i+].h-b[i].h)*sum[];
}
printf("Test case #%d\n",++kase);
printf("Total explored area: %.2f\n\n",ans);
}
return ;
}
/*
奉送一组数据
输入:
3
10 10 20 20
15 15 25 30
22 8 28 17 输出:
Test case #1
Total explored area: 273.00
再送大家一道题:poj 1389
整体思路一模一样,只是一个为整数,一个为实数,输入输出格式不一样。。
*/
-------------------------------------
-------------------------------------
poj 1389
#include<cstdio>
#include<cstring>
#include<algorithm>
#define m(s) memset(s,0,sizeof s)
#define lc k<<1
#define rc k<<1|1
using namespace std;
const int N=1e5+;
struct node{
int xl,xr,h;
int id;
bool operator < (const node &a)const{
return h<a.h;
}
}b[N];
int n,kase,tag[N<<];
int sum[N<<],c[N];
void updata(int k,int l,int r){
if(tag[k]) sum[k]=c[r+]-c[l];
else if(l==r) sum[k]=;
else sum[k]=sum[lc]+sum[rc];
}
void change(int k,int l,int r,int x,int y,int val){
if(x<=l&&r<=y){
tag[k]+=val;updata(k,l,r);
return ;
}
int mid=l+r>>;
if(x<=mid) change(lc,l,mid,x,y,val);
if(y>mid) change(rc,mid+,r,x,y,val);
updata(k,l,r);
}
int main(){
int x1,x2,y1,y2;
while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2),~x1){
int cnt=,ans=;m(sum);m(tag);
b[++cnt]=(node){x1,x2,y1,};c[cnt]=x1;
b[++cnt]=(node){x1,x2,y2,-};c[cnt]=x2;
while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2),~x1){
b[++cnt]=(node){x1,x2,y1,};c[cnt]=x1;
b[++cnt]=(node){x1,x2,y2,-};c[cnt]=x2;
}
sort(b+,b+cnt+);
sort(c+,c+cnt+);
int t=unique(c+,c+cnt+)-(c+);
for(int i=;i<cnt;i++){
x1=lower_bound(c+,c+t+,b[i].xl)-c;
x2=lower_bound(c+,c+t+,b[i].xr)-c-;
change(,,t,x1,x2,b[i].id);
ans+=(b[i+].h-b[i].h)*sum[];
}
printf("%d\n",ans);
}
return ;
}
hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]的更多相关文章
- hdu1542 线段树扫描线求矩形面积的并
		题意: 给你n个正方形,求出他们的所占面积有多大,重叠的部分只能算一次. 思路: 自己的第一道线段树扫描线题目,至于扫描线,最近会写一个总结,现在就不直接在这里写了,说下我的方 ... 
- POJ 1151 - Atlantis 线段树+扫描线..
		离散化: 将所有的x轴坐标存在一个数组里..排序.当进入一条线段时..通过二分的方式确定其左右点对应的离散值... 扫描线..可以看成一根平行于x轴的直线..至y=0开始往上扫..直到扫出最后一条平行 ... 
- HDU 1542"Atlantis"(线段树+扫描线求矩形面积并)
		传送门 •题意 给你 n 矩形,每个矩形给出你 $(x_1,y_1),(x_2,y_2)$ 分别表示这个矩形的左下角和右上角坐标: 让你求这 n 个矩形并的面积: 其中 $x \leq 10^{5} ... 
- hdu1828 线段树扫描线求矩形面积的周长
		题意: 给你n个矩形,问你这n个矩形所围成的图形的周长是多少. 思路: 线段树的扫描线简单应用,这个题目我用的方法比较笨,就是扫描两次,上下扫描,求出多边形的上下边长和,然后同 ... 
- 2015 UESTC 数据结构专题E题  秋实大哥与家 线段树扫描线求矩形面积交
		E - 秋实大哥与家 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 De ... 
- poj  1177 --- Picture(线段树+扫描线 求矩形并的周长)
		题目链接 Description A number of rectangular posters, photographs and other pictures of the same shape a ... 
- HDU 1828“Picture”(线段树+扫描线求矩形周长并)
		传送门 •参考资料 [1]:算法总结:[线段树+扫描线]&矩形覆盖求面积/周长问题(HDU 1542/HDU 1828) •题意 给你 n 个矩形,求矩形并的周长: •题解1(两次扫描线) 周 ... 
- POJ 1151 Atlantis 线段树+离散化+扫描线
		这次是求矩形面积并 /* Problem: 1151 User: 96655 Memory: 716K Time: 0MS Language: G++ Result: Accepted */ #inc ... 
- hdu1542 Atlantis 线段树--扫描线求面积并
		There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ... 
随机推荐
- 【Visual Studio】MFC编辑框自动换行,垂直滚动条自动下移(转)
			原文转自 http://blog.csdn.net/wu_lai_314/article/details/8317395 1.新建一个编辑框控件(Edit Control),将其多行(Multilin ... 
- 转载 关于malloc
			1.函数原型及说明: void *malloc(long NumBytes):该函数分配了NumBytes个字节,并返回了指向这块内存的指针.如果分配失败,则返回一个空指针(NULL). 关于分配失败 ... 
- checkbox 复选框只能选中一次,之后不能用
			<td> <input type="checkbox" name="is_check" id="is_check" val ... 
- Unsafe  in Java
			http://www.cnblogs.com/xrq730/p/4976007.html http://www.importnew.com/14511.html http://blog.csdn.ne ... 
- chpasswd、dd命令、find实战、添加系统服务、buffer、cached
			1.如果两个文件的每一行想一一对应 paste 1.txt 2.txt # 文件3.txt中存放着用户跟密码,想要添加用户并设置密码: # 用户必须存在,文件格式必须是--用户名:密码 chpassw ... 
- list all of the Oracle 12c hidden undocumented parameters
			http://www.dba-oracle.com/t_12c_hidden_undocumented_parameters.htm select ksppinm, ksppdesc from x$k ... 
- Div 浮动到另一个div之上
			转自原文Div 浮动到另一个div之上 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <met ... 
- iOS  -- SKTexture类
			SKTexture类 继承自 NSObject 符合 NSCodingNSCopyingNSObject(NSObject) 框架 /System/Library/Frameworks/Sprite ... 
- scala工具sbt的安装和使用;idea如何创建scala项目
			scala的sbt类似于java的maven mac:brew install sbt linux:yum Install sbt 或者下载二机制包 使用sbt需要想mvn一样搭建公司私服,不然,下载 ... 
- TCP通过滑动窗口和拥塞窗口实现限流,能抵御ddos攻击吗
			tcp可以通过滑动窗口和拥塞算法实现流量控制,限制上行和下行的流量,但是却不能抵御ddos攻击. 限流只是限制访问流量的大小,是无法区分正常流量和异常攻击流量的. 限流可以控制本软件或者应用的流量大小 ... 
