HDU 1542-Atlantis(线段树+扫描线)
2016-05-05 20:05
232 查看
A - Atlantis
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU
1542
Appoint description:
System Crawler (2016-05-04)
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.
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.
Sample Input
2
10 10 20 20
15 15 25 25.5
0
Sample Output
Test case #1
Total explored area: 180.00
题意: 给出左上角和右下角坐标,叫你把矩形覆盖的面积求出来。
AC代码:
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU
1542
Appoint description:
System Crawler (2016-05-04)
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.
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.
Sample Input
2
10 10 20 20
15 15 25 25.5
0
Sample Output
Test case #1
Total explored area: 180.00
题意: 给出左上角和右下角坐标,叫你把矩形覆盖的面积求出来。
AC代码:
#include<iostream> #include<algorithm> #include<cstring> #include<string> #include<cstdio> using namespace std; #define T 10005 #define lson (rt<<1) #define rson (rt<<1|1) typedef long long ll; int n; //离散化数组 double pos[T]; //保存y在x区间的边 struct line { double y,x_up,x_down; int flag; bool operator<(const line& a)const{ return y<a.y; } line(double _1,double _2,double _3,int _4): y(_1),x_up(_2),x_down(_3),flag(_4){} line(){} }p[T]; //线段树数组 struct node { int L,R; double len; int mid; int flag; }tree[T]; //向上更新 void pushup(int rt) { if(tree[rt].flag){//完全覆盖区间 tree[rt].len = pos[tree[rt].R-1]-pos[tree[rt].L-1]; } else if(tree[rt].L+1==tree[rt].R){//叶子节点 tree[rt].len = 0; } else//跨越几个区间合并值 { tree[rt].len = tree[lson].len+tree[rson].len; } } //建树 void build(int rt,int L,int R) { tree[rt].L = L; tree[rt].R = R; tree[rt].flag = 0; tree[rt].len = 0; tree[rt].mid = (L+R)>>1; if(L+1!=R){ build(lson,L,tree[rt].mid); build(rson,tree[rt].mid,R); } } //更新 void update(int rt,int L,int R,int w) { if(L<=tree[rt].L&&tree[rt].R<=R){ tree[rt].flag+=w; pushup(rt); return; } if(R<=tree[rt].mid) update(lson,L,R,w); else if(L>=tree[rt].mid) update(rson,L,R,w); else{ update(lson,L,tree[rt].mid,w); update(rson,tree[rt].mid,R,w); } pushup(rt); } int main() { #ifdef zsc freopen("input.txt","r",stdin); #endif int cas=0; int i,c; double x1,x2,y1,y2; while(scanf("%d",&n),n) { c = 0; for(i=0;i<n;++i){ scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); pos[c] = x1; p[c++] = line(y1,x1,x2,1); pos[c] = x2; p[c++] = line(y2,x1,x2,-1); } sort(pos,pos+c); int d = unique(pos,pos+c)-pos; build(1,1,d); sort(p,p+c); double ans=0; printf("Test case #%d\n",++cas); for(i=0;i<c-1;++i){ //因为离散化了,所以要找x的下标 int a = lower_bound(pos,pos+d,p[i].x_up)-pos+1; int b = lower_bound(pos,pos+d,p[i].x_down)-pos+1; //更新x区间(a,b)的值 update(1,a,b,p[i].flag); ans+=tree[1].len*(p[i+1].y-p[i].y); } printf("Total explored area: %.2lf\n",ans); printf("\n"); } return 0; }
相关文章推荐
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- 1611:The Suspects
- POJ1089 区间合并
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- POJ1050 最大子矩阵和
- 用单调栈解决最大连续矩形面积问题
- 2632 Crashing Robots的解决方法
- 1573 Robot Motion (简单题)
- POJ 1200 Crazy Search(简单哈希)