首页 > 代码库 > hdu 1542 Atlantis(线段树)
hdu 1542 Atlantis(线段树)
Atlantis
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6899 Accepted Submission(s): 3022
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
2 10 10 20 20 15 15 25 25.5 0
Sample Output
Test case #1 Total explored area: 180.00
给定一个矩形的左下角坐标和右上角坐标分别为:(x1,y1)、(x2,y2),对这样的一个矩形,我们构造两条线段,一条定位在x1,它在y坐标的区间是[y1,y2],并且给定一个cover域值为1;另一条线段定位在x2,区间一样是[y1,y2],给定它一个cover值为-1。根据这样的方法对每个矩形都构造两个线段,最后将所有的线段根据所定位的x从左到右进行排序。
刚刚开始的时候,线段树上的cover值都为0,但第一根线段(x==0)插入线段树的之后,我们将线段树上的cover加上该线段的cover,那么,此时线段树上被该线段覆盖的位置上的cover的值就为1,下次再插入第二根线段(x==1)此时发现该线段所覆盖的区间内,有一部分线段树的cover为0,另有一部分为1,仔细观察,但插入第二个线段的时候,如果线段树上cover已经为1的那些区间,和现在要插入的第二根线段之间,是不是构成了并面积?
也就是说,我们插入某跟线段的时候,只要看该线段所在区间上的cover是否大于等于1,如果是,那么就可以将并面积值加上(目前线段的x定位 - 上一线段的x定位)*(该区间的大小)
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; const int maxn=110; int n,index=0; double x1,x2,y1,y2,y[2*maxn]; struct LINE { double x,y_down,y_up; int flag; bool operator<(const LINE &a)const { return x<a.x; } }line[2*maxn]; struct TREE { double y_down,y_up,x; int cover; bool flag; }tree[1000*maxn]; void build(int i,int l,int r) { tree[i].x=-1; tree[i].cover=0; tree[i].y_down=y[l]; tree[i].y_up=y[r]; tree[i].flag=false; if(l+1==r) { tree[i].flag=true; return ; } int mid=(l+r)>>1; build(2*i,l,mid); build(2*i+1,mid,r); } double insert(int i,double x,double l,double r,int flag) { if(r<=tree[i].y_down||l>=tree[i].y_up) return 0; if(tree[i].flag) { if(tree[i].cover>0) { double temp_x=tree[i].x; double ans=(x-temp_x)*(tree[i].y_up-tree[i].y_down); tree[i].x=x; tree[i].cover+=flag; return ans; } else { tree[i].cover+=flag; tree[i].x=x; return 0; } } double ans1,ans2; ans1=insert(2*i,x,l,r,flag); ans2=insert(2*i+1,x,l,r,flag); return ans1+ans2; } int main() { int count=0; while(scanf("%d",&n)!=EOF&&n) { index=1; for(int i=1;i<=n;i++) { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); y[index]=y1; line[index].x=x1; line[index].y_down=y1; line[index].y_up=y2; line[index].flag=1; index++; y[index]=y2; line[index].x=x2; line[index].y_down=y1; line[index].y_up=y2; line[index].flag=-1; index++; } sort(&y[1],&y[index]); sort(&line[1],&line[index]); build(1,1,index-1); double ans=0; for(int i=1;i<index;i++) ans+=insert(1,line[i].x,line[i].y_down,line[i].y_up,line[i].flag); printf("Test case #%d\nTotal explored area: %.2f\n\n",++count,ans); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。