Hdu 1542 Atlantis 线段树 求矩形面积并
来源:互联网 发布:成都蜀美网络 编辑:程序博客网 时间:2024/05/22 15:20
Atlantis
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4691 Accepted Submission(s): 2095
Problem Description
There are several ancient Greek texts that contain descriptions of the 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.
Input
The input file consists of several test cases. Each test case starts with a 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.
The input file is terminated by a line containing a single 0. Don’t process it.
Output
For each test case, your program should output one section. The first line 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.
Output a blank line after each test case.
Sample Input
210 10 20 2015 15 25 25.50
Sample Output
Test case #1Total explored area: 180.00
转题解:
题意:矩形面积并
思路:浮点数先要离散化;然后把矩形分成两条边,上边和下边,对横轴建树,然后从下到上扫描上去,用cnt表示该区间下边比上边多几个,sum代表该区间内被覆盖的线段的长度总和
这里线段树的一个结点并非是线段的一个端点,而是该端点和下一个端点间的线段,所以题目中r+1,r-1的地方可以自己好好的琢磨一下
思路:浮点数先要离散化;然后把矩形分成两条边,上边和下边,对横轴建树,然后从下到上扫描上去,用cnt表示该区间下边比上边多几个,sum代表该区间内被覆盖的线段的长度总和
这里线段树的一个结点并非是线段的一个端点,而是该端点和下一个端点间的线段,所以题目中r+1,r-1的地方可以自己好好的琢磨一下
线段树操作:update:区间增减 query:直接取根节点的值
--------------------------
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int N=22222;struct Seg{ double l,r,h; int s; Seg(){} Seg(double a,double b,double c,int d):l(a),r(b),h(c),s(d){} bool operator < (const Seg& cmp) const { return h<cmp.h; }}a[N];double X[N];struct TREE{ int l; int r; int cnt; double sum;}tree[N*4];void build(int root,int l,int r){ tree[root].l=l; tree[root].r=r; tree[root].cnt=0; tree[root].sum=0; if (l==r) { return; } int mid=(l+r)/2; build(root<<1,l,mid); build(root<<1|1,mid+1,r);}void push_up(int root){ if (tree[root].cnt) { tree[root].sum=X[tree[root].r+1]-X[tree[root].l]; } else if (tree[root].l==tree[root].r) { tree[root].sum=0; } else { tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum; }}void update(int root,int L,int R,int val){ if (L<=tree[root].l&&R>=tree[root].r) { tree[root].cnt+=val; push_up(root); return; } int mid=(tree[root].l+tree[root].r)/2; if (L<=mid) update(root<<1,L,R,val); if (R>mid) update(root<<1|1,L,R,val); push_up(root);}int main(){ int n,m,k; double x1,y1,x2,y2,ret; int cnt=1; while (~scanf("%d",&n)) { if (n==0) break; m=0; for (int i=0;i<n;i++) { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); X[m]=x1; a[m++]=Seg(x1,x2,y1,1); X[m]=x2; a[m++]=Seg(x1,x2,y2,-1); } sort(X,X+m); sort(a,a+m); k=1; for (int i=1;i<m;i++) { if (X[i]!=X[i-1]) X[k++]=X[i]; } build(1,0,k-1); ret=0; for (int i=0;i<m-1;i++) { int l=lower_bound(X,X+k,a[i].l)-X; int r=lower_bound(X,X+k,a[i].r)-X-1; if (l<=r) update(1,l,r,a[i].s); ret+=tree[1].sum*(a[i+1].h-a[i].h); } printf("Test case #%d\nTotal explored area: %0.2lf\n\n",cnt++,ret); } return 0;}
- hdu 1542(线段树 求矩形面积并)Atlantis
- Hdu 1542 Atlantis 线段树 求矩形面积并
- HDU 1542 Atlantis(线段树求矩形面积并)
- HDU 1542 Atlantis(线段树求矩形面积并)
- hdu 1542 Atlantis (线段树求矩形面积并)
- HDU-1542 Atlantis (线段树 求所有矩形面积和)
- Hdu 1542/ POj 1151 Atlantis 线段树+矩形面积并
- hdu 1542 Atlantis 线段树矩形面积并+离散化
- 【HDU】1542 Atlantis 矩形面积并->线段树
- poj 1151 hdu 1542 hoj1119 Atlantis 线段树扫描线求矩形面积并
- hdu 1542 (poj1151) Atlantis ( 线段树求矩形面积并)
- POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并
- HDU 1542 Atlantis (线段树扫描线求面积并)
- POJ 1151 Atlantis (线段树求矩形面积并)
- Hdu 1542 Atlantis + Hdu 1255 覆盖的面积 (线段树矩形面积并)
- 【HDU1542】Atlantis【线段树】【矩形面积并】
- hdu1542 Atlantis【矩形面积并+线段树】
- poj 1151 Atlantis / hdu 1542 线段树扫描线 矩形面积并
- 硬盘SMART参数(05,C5)与(C7)解惑
- ViewPager
- linux下mysql连接失败: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
- java泛型之2
- zend studio小技巧
- Hdu 1542 Atlantis 线段树 求矩形面积并
- Git详解之四 服务器上的Git
- 数据库并发控制几只——事务
- Android Studio 在 win7 下的安装
- hdu1546-Idiomatic Phrases Gamehttp
- 安卓推送
- PHP正则表达式
- android 左右页面滑动(滑屏)增加layout文件 而不是drawable(还有activity)
- Sql Server高手必备