Atlantis
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Appoint description:
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
没个下边必有上边对应,不需要pushdown,pushup检查当前边的存在状态
#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
//#pragma comment(linker, "/STACK:1024000000,1024000000");
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 400005
vector<double>v;
int cnt;
struct edge
{
double l,r;
double h;
int tag;
friend bool operator < (edge a,edge b)
{
return a.h<b.h;
}
} line[maxn];
struct node
{
int l,r;
int add;
double sum;
} T[maxn];
void add_edge(double x,double y,double h,int d)
{
line[cnt].l=x;
line[cnt].r=y;
line[cnt].h=h;
line[cnt++].tag=d;
}
void init(int l,int r,int k)
{
T[k].l=l;
T[k].r=r;
T[k].add=0;
T[k].sum=0;
if(l+1==r) return ;
int mid=(l+r)>>1;
init(l,mid,2*k);
init(mid,r,2*k+1);
}
inline void pushup(int k)
{
if(T[k].add) T[k].sum=v[T[k].r]-v[T[k].l];
else T[k].sum=T[2*k].sum+T[2*k+1].sum;
}
void Insert(int d,int l,int r,int k)
{
if(T[k].l==l&&T[k].r==r)
{
T[k].add+=d;
if(T[k].add) T[k].sum=v[r]-v[l];
else T[k].sum=T[2*k].sum+T[2*k+1].sum;
return ;
}
int mid=(T[k].l+T[k].r)>>1;
if(r<=mid) Insert(d,l,r,2*k);
else if(l>=mid) Insert(d,l,r,2*k+1);
else
{
Insert(d,l,mid,2*k);
Insert(d,mid,r,2*k+1);
}
pushup(k);
}
int main()
{
int n;
int c=1;
while(scanf("%d",&n)!=EOF&&n)
{
v.clear();
cnt=0;
for(int i=0; i<n; i++)
{
double x1,y1,x2,y2;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
add_edge(x1,x2,y1,-1);
add_edge(x1,x2,y2,1);
v.push_back(x1);
v.push_back(x2);
}
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
sort(line,line+cnt);
init(0,v.size()+1,1);
double ans=0;
for(int i=0; i<cnt; i++)
{
int x=lower_bound(v.begin(),v.end(),line[i].l)-v.begin();
int y=lower_bound(v.begin(),v.end(),line[i].r)-v.begin();
if(i)
{
ans+=T[1].sum*(line[i].h-line[i-1].h);
}
Insert(line[i].tag,x,y,1);
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n",c++,ans);
}
return 0;
}