http://www.elijahqi.win/archives/997
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
#include<cstdio>
#include<algorithm>
#define N 110
using namespace std;
struct node{
double pos,l,r;int op;
}q[N<<1];
double a[N<<1];
struct node1{
int l,r,left,right,count;double sum,lf,rf;
}tree[N<<2];
inline bool cmp(node a,node b){return a.pos<b.pos;}
int num,root,n;double x1,x2,y1,y2;
void build(int &x,int l,int r){
x=++num;tree[x].l=l;tree[x].r=r;tree[x].lf=a[l];tree[x].rf=a[r];tree[x].sum=tree[x].count=0;
if (l+1==r) return;
int mid=l+r>>1;
build(tree[x].left,l,mid);build(tree[x].right,mid,r);
}
inline void update(int x){
if (tree[x].count){tree[x].sum=tree[x].rf-tree[x].lf;return;}
if (tree[x].l+1==tree[x].r) {tree[x].sum=0;return;}
int l=tree[x].left,r=tree[x].right;
tree[x].sum=tree[l].sum+tree[r].sum;
}
void insert1(int x,node le){
if(le.l==tree[x].lf&&le.r==tree[x].rf){
tree[x].count+=le.op;update(x);return;
}
int l=tree[x].left,r=tree[x].right;
if (le.r<=tree[l].rf) insert1(tree[x].left,le);else
if (le.l>=tree[r].lf) insert1(tree[x].right,le);else {
node tmp=le;tmp.r=tree[l].rf;insert1(tree[x].left,tmp);
tmp=le;tmp.l=tree[r].lf;insert1(tree[x].right,tmp);
}
update(x);
}
int main(){
freopen("hdu1542.in","r",stdin);
int tot=0;
while (~scanf("%d",&n)&&n){int top=0;num=0;
for (int i=1;i<=n;++i){
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
a[++top]=x1;q[top].pos=y1;q[top].l=x1;q[top].r=x2;q[top].op=1;
a[++top]=x2;q[top].pos=y2;q[top].l=x1;q[top].r=x2;q[top].op=-1;
}
sort(a+1,a+top+1);sort(q+1,q+top+1,cmp);
//for (int i=1;i<=top;++i) printf("%lf ",a[i]);
//for (int i=1;i<=top;++i) printf("%lf %lf\n",q[i].l,q[i].r);
build(root,1,top);insert1(root,q[1]);double ans=0;
for (int i=2;i<=top;++i){
ans+=tree[root].sum*(q[i].pos-q[i-1].pos);insert1(root,q[i]);
}
printf("Test case #%d\n",++tot);
printf("Total explored area: %.2f\n\n",ans);
}
return 0;
}