POJ 1151 Atlantis
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11830 | Accepted: 4588 |
Description
Input
The input file is terminated by a line containing a single 0. Don't process it.
Output
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
Source
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define N 203
#define lson l,m,k<<1
#define rson m,r,k<<1|1
using namespace std;
double rcy[N];
struct segment
{
int cover;
double index;
};
segment st[N<<2];
struct line
{
double x,y1,y2;
int flag;
bool operator<(const line&b)const
{
return x<b.x;
}
};
line li[N];
void build(int l,int r,int k)
{
if(r==l+1)
{
st[k].cover=0; return ;
}
int m=(l+r)>>1;
build(lson);
build(rson);
}
double S;
double index;
int flag;
void update(double &y1,double &y2,int l,int r,int k)
{
if(r==l+1)
{
if(st[k].cover)
S+=(rcy[r]-rcy[l])*(index-st[k].index);
st[k].cover+=flag;
st[k].index=index;
return ;
}
int m=(l+r)>>1;
if(y1<rcy[m]) update(y1,y2,lson);
if(y2>rcy[m]) update(y1,y2,rson);
}
int main()
{
int n;
int i,j,k,t=1;
double x1,x2,y1,y2;
while(scanf("%d",&n),n)
{
for(j=i=0;i<n;i++)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
li[j].x=x1;li[j].flag=1;
li[j].y1=y1;li[j].y2=y2;
rcy[j]=y1;
j++;
rcy[j]=y2;
li[j].x=x2;li[j].flag=-1;
li[j].y1=y1;li[j].y2=y2;
j++;
}
sort(rcy,rcy+j);
sort(li,li+j);
for(i=1,k=0;i<j;i++)
if(rcy[i]!=rcy[k])
rcy[++k]=rcy[i];
build(0,k,1);S=0;
for(i=0;i<j;i++)
{
flag=li[i].flag;
index=li[i].x;
update(li[i].y1,li[i].y2,0,k,1);
}
printf("Test case #%d\n",t++);
printf("Total explored area: %.2lf\n\n",S);
}
return 0;
}
//学了下不用更新到底的方法
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define lson l,m,k<<1
#define rson m,r,k<<1|1
#define N 253
using namespace std;
struct node
{
double len;
int cover;
};
struct line
{
double x,yup,ydown;
int flag;
bool operator <(const line&b)const
{
return x<b.x;
}
};
node st[N<<2];
line li[N];
double rcy[N];
void build(int l,int r,int k)
{
st[k].cover=st[k].len=0;
if(r==l+1)//这里把1写成了l,检查了N久的错误、、、、
return ;
int m=(l+r)>>1;
build(lson);
build(rson);
}
void up(int &k,int &l,int &r)
{
if(st[k].cover)
{
st[k].len=rcy[r]-rcy[l];
}else if(r==l+1)
st[k].len=0;
else
st[k].len=st[k<<1].len+st[k<<1|1].len;
}
int flag;
void update(double &dy,double &uy,int l,int r,int k)
{
if(dy<=rcy[l]&&uy>=rcy[r])
{
st[k].cover+=flag;
up(k,l,r);
return ;
}
int m=(l+r)>>1;
if(dy<rcy[m]) update(dy,uy,lson);
if(uy>rcy[m]) update(dy,uy,rson);
up(k,l,r);
}
int main()
{
int n,i,j,k,t=1;
double x1,x2,y1,y2;
double s=0;
while(scanf("%d",&n),n)
{
for(j=i=0;i<n;i++)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
li[j].ydown=y1; li[j].yup=y2;
li[j].x=x1; li[j].flag=1;
rcy[j]=y1; j++; rcy[j]=y2;
li[j].ydown=y1; li[j].yup=y2;
li[j].x=x2; li[j++].flag=-1;
}
sort(li,li+j);
sort(rcy,rcy+j);
for(k=0,i=1;i<j;i++)
if(rcy[i]!=rcy[k])
rcy[++k]=rcy[i];
build(0,k,1);s=0;
j--;
for(i=0;i<j;i++)
{
flag=li[i].flag;
update(li[i].ydown,li[i].yup,0,k,1);
s+=st[1].len*(li[i+1].x-li[i].x);
}
printf("Test case #%d\n",t++);
printf("Total explored area: %.2lf\n\n",s);
}
return 0;
}