poj1151 Atlantis

来源:互联网 发布:php配置环境工具 编辑:程序博客网 时间:2024/05/29 21:18
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 18723 Accepted: 7105

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 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

210 10 20 2015 15 25 25.50

Sample Output

Test case #1Total explored area: 180.00 

Source

Mid-Central European Regional Contest 2000


以前线段树的写法十分没效率……写了个更屌的

#include<map>#include<string>#include<cstring>#include<cstdio>#include<cstdlib>#include<cmath>#include<queue>#include<vector>#include<iostream>#include<algorithm>#include<bitset>#include<climits>#include<list>#include<iomanip>#include<stack>#include<set>using namespace std;double hs[210];struct Tree{int l,r,num;double val;}tree[200<<2];void create(int l,int r,int k){tree[k].l=l;tree[k].r=r;tree[k].val=0;tree[k].num=0;if(l+1==r)return;int m=l+r>>1;create(l,m,k<<1);create(m,r,k<<1|1);}void update(int l,int r,int k,int flag){if(tree[k].l==l&&tree[k].r==r){tree[k].num+=flag;if(tree[k].num==0)tree[k].val=l+1==r?0:tree[k<<1].val+tree[k<<1|1].val;elsetree[k].val=hs[r]-hs[l];return;}int m=tree[k].l+tree[k].r>>1;if(r<=m)update(l,r,k<<1,flag);else if(l>=m)update(l,r,k<<1|1,flag);else{update(l,m,k<<1,flag);update(m,r,k<<1|1,flag);}if(tree[k].num==0)tree[k].val=tree[k<<1].val+tree[k<<1|1].val;}struct Seg{int flag;double l,r,y;bool operator <(Seg one)const{return y<one.y;}}seg[210];int main(){int n,cs=0;while(cin>>n&&n!=0){vector<double>box;for(int i=0;i<n;i++){double x1,y1,x2,y2;cin>>x1>>y1>>x2>>y2;seg[i<<1].l=seg[i<<1|1].l=x1;seg[i<<1].r=seg[i<<1|1].r=x2;seg[i<<1].y=y1;seg[i<<1|1].y=y2;seg[i<<1].flag=1;seg[i<<1|1].flag=-1;box.push_back(x1);box.push_back(x2);}sort(box.begin(),box.end());box.erase(unique(box.begin(),box.end()),box.end());for(int i=0;i<box.size();i++)hs[i+1]=box[i];n*=2;sort(seg,seg+n);double ans=0;create(1,box.size(),1);for(int i=0;i<n;i++){int l=1+lower_bound(box.begin(),box.end(),seg[i].l)-box.begin();int r=1+lower_bound(box.begin(),box.end(),seg[i].r)-box.begin();if(i!=n-1){update(l,r,1,seg[i].flag);ans+=tree[1].val*(seg[i+1].y-seg[i].y);}}printf("Test case #%d\n",++cs);printf("Total explored area: %.2f\n\n",ans);}}

0 0
原创粉丝点击