hdu-1542 Atlantis(离散化+线段树+扫描线算法)
2016-04-07 23:56
381 查看
题目链接:
Memory Limit: 65536/32768 K (Java/Others)
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
[align=left] [/align]
2
10 10 20 20
15 15 25 25.5
0
[align=left]Sample Output[/align]
[align=left] [/align]
Test case #1
Total explored area: 180.00
题意:
给了n个矩形的左下角和右上角的坐标,问这些矩形的面积和是多少;
思路:
离散化后用线段树结合扫描线算法可以做,是扫描线算法的模板题;
AC代码:
Atlantis
Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
[align=left] [/align]
2
10 10 20 20
15 15 25 25.5
0
[align=left]Sample Output[/align]
[align=left] [/align]
Test case #1
Total explored area: 180.00
题意:
给了n个矩形的左下角和右上角的坐标,问这些矩形的面积和是多少;
思路:
离散化后用线段树结合扫描线算法可以做,是扫描线算法的模板题;
AC代码:
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e5+10; int n; double rec[2*N],xa,xb,ya,yb; struct Tree { int l,r,cover; double sum; }; Tree tree[4*N]; struct Line { double l,r,h; int flag; }; Line line[4*N]; int cmp(Line x,Line y) { return x.h<y.h; } void build(int node,int L,int R) { tree[node].sum=0; tree[node].cover=0; tree[node].l=L; tree[node].r=R; if(L>=R)return ; int mid=(L+R)>>1; build(2*node,L,mid); build(2*node+1,mid+1,R); } void Pushup(int node) { if(tree[node].cover) { tree[node].sum=rec[tree[node].r+1]-rec[tree[node].l]; } else { if(tree[node].l!=tree[node].r) tree[node].sum=tree[2*node].sum+tree[2*node+1].sum; else tree[node].sum=0; } } void update(int node,int L,int R,int x) { if(L<=tree[node].l&&R>=tree[node].r) { tree[node].cover+=x; Pushup(node); return ; } int mid=(tree[node].l+tree[node].r)>>1; if(R<=mid)update(2*node,L,R,x); else if(L>mid)update(2*node+1,L,R,x); else { update(2*node,L,mid,x); update(2*node+1,mid+1,R,x); } Pushup(node); } map<double,int>mp; int main() { int Case=1; while(1) { scanf("%d",&n); if(n==0)break; printf("Test case #%d\n",Case++); int cnt=1; for(int i=0;i<n;i++) { scanf("%lf%lf%lf%lf",&xa,&ya,&xb,&yb); rec[cnt]=line[cnt].l=xa; line[cnt].r=xb; line[cnt].flag=1; line[cnt++].h=ya; line[cnt].l=xa; rec[cnt]=line[cnt].r=xb; line[cnt].h=yb; line[cnt++].flag=-1; } sort(line+1,line+cnt,cmp); sort(rec+1,rec+cnt); int num=2; for(int i=2;i<cnt;i++) { if(rec[i]!=rec[i-1])rec[num++]=rec[i]; } for(int i=1;i<num;i++) { mp[rec[i]]=i; } build(1,1,num-1); double ans=0; for(int i=1;i<cnt-1;i++) { int fx=mp[line[i].l]; int fy=mp[line[i].r]; update(1,fx,fy-1,line[i].flag); ans+=tree[1].sum*(line[i+1].h-line[i].h); } printf("Total explored area: %.2lf\n",ans); printf("\n"); } return 0; }
相关文章推荐
- azure blobs
- [IOS]Label-UIMenuController
- Google Test(GTest)使用方法和源码解析——断言的使用方法和解析
- Google Test(GTest)使用方法和源码解析——Listener技术分析和应用
- 分布式服务框架 dubbo/dubbox 入门示例
- java io(一)
- 数组I冒泡法排序
- IOS学习笔记——UIView家族
- Google Test(GTest)使用方法和源码解析——结果统计机制分析
- SQL Server ->> T-SQL查询面试题之实例版
- python获取绑定的IP,并动态指定出口IP
- bug_ _小心android-support-v4.jar版本混乱造成的NoClassDefFoundError
- 使用异步任务下载--疯狂android讲义实例
- android最佳实践指南
- ON_MESSAGE
- 周总结
- java中静态变量和实例变量到底有什么区别
- ubuntu下编写简单的helloworld程序
- Google Test(GTest)使用方法和源码解析——自动调度机制分析
- Google Test(GTest)使用方法和源码解析——概况