和hdu1255、hdu1264 一个类型 就不多做解释
详见:
1255 http://blog.csdn.net/u011742541/article/details/13298237
1264 http://blog.csdn.net/u011742541/article/details/13233973
#include "stdio.h"
#include "algorithm"
using namespace std;
const int maxn = 210;
int n;
double y[maxn];
struct node
{
double x,y1,y2; //x 边的位置 y边的区间大小
int f; //f 标记 前边还是后边
}Line[maxn];
struct node1
{
double ld,rd,len;
int l,r,c;
}tree[maxn*4];
bool cmp( node a,node b )
{
return a.x < b.x;
}
void buildtree( int ld,int rd,int t) // 建树
{
tree[t].c = 0; tree[t].len = 0;
tree[t].l = ld; tree[t].r = rd;
tree[t].ld = y[ld]; tree[t].rd = y[rd];
if( ld+1 == rd )
return;
int mid = ( ld+rd )/2;
buildtree( ld,mid,t*2 );
buildtree( mid,rd,t*2+1 );
}
void calen( int t ) //求线段树被覆盖长度
{
if( tree[t].c > 0 ) // t区间f是否被覆盖
{
tree[t].len = tree[t].rd - tree[t].ld;
return;
}
if( tree[t].l +1 == tree[t].r ) tree[t].len = 0; //
else
tree[t].len = tree[2*t].len + tree[2*t+1].len; // 回带len的值
}
void updata( int t,node e )
{
if( e.y1 == tree[t].ld && e.y2 == tree[t].rd ) //在线段树中找到边e所在的区间 更新f求出长度冷
{
tree[t].c += e.f;
calen(t);
return;
}
if( e.y2 <= tree[2*t].rd ) //如果e全在tree[t]的左半边
updata( 2*t,e );
else if( e.y1 >= tree[2*t+1].ld ) //e在tree[t]的右半边
updata( 2*t+1,e );
else //一部分在左半边 一部分在右半边
{
node temp = e;
temp.y2 = tree[2*t].rd;
updata( 2*t,temp );
temp = e;
temp.y1 = tree[2*t+1].ld;
updata( 2*t+1,temp );
}
calen(t);
}
int main()
{
int i,j,cas=1,pos;
double x1,y1,x2,y2,ans;
while( scanf("%d",&n)!=EOF , n)
{
pos = 0;
for( i=1; i<=n; i++ )
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
pos++;
Line[pos].x = x1; //存前边
Line[pos].y1 = y1;
Line[pos].y2 = y2;
Line[pos].f = 1;
y[pos] = y1;
pos++;
Line[pos].x = x2; //存后边
Line[pos].y1 = y1;
Line[pos].y2 = y2;
Line[pos].f = -1;
y[pos] = y2;
}
sort( y+1,y+pos+1);
sort( Line+1,Line+pos+1,cmp );
buildtree( 1,pos,1 );
ans = 0;
for( i=1; i<=pos; i++ )
{
ans += tree[1].len * ( Line[i].x - Line[i-1].x ); //tree[1].len 表示f为1的线段树长度 Line[i].x - Line[i-1].x相邻2边的距离
updata( 1,Line[i] );
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n",cas++,ans);
}
return 0;
}