POJ 1151 Atlantis 线段树+离散化+扫描线

时间:2021-12-27 21:45:53

这次是求矩形面积并

/*
Problem: 1151 User: 96655
Memory: 716K Time: 0MS
Language: G++ Result: Accepted
*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
using namespace std;
const int maxn=;
double y[maxn*];
struct Line
{
int co;
double x,y1,y2;
void fun(double a,double b,double c,int d)
{
x=a;
y1=b;
y2=c;
co=d;
}
bool operator<(const Line &e)const
{
return x<e.x;
}
}line[maxn*];
struct Node
{
double s,t,len;
int co;
void change(int o)
{
co+=o;
if(co==) len=;
else len=t-s;
}
};
struct Segtree
{
Node tree[maxn*];
void build(int l,int r,int o)
{
tree[o].s=y[l];tree[o].t=y[r];
tree[o].co=;tree[o].len=;
if(l+<r)
{
int m=(l+r)>>;
build(l,m,o*);
build(m,r,o*+);
}
}
void pushup(int o)
{
tree[o].len=tree[o*].len+tree[o*+].len;
}
void update(int l,int r,int o,Line e)
{
if(l+==r)
{
tree[o].change(e.co);
return;
}
int m=(l+r)>>;
if(e.y1<tree[o*].t)update(l,m,o*,e);
if(e.y2>tree[o*+].s)update(m,r,o*+,e);
pushup(o);
}
}seg;
int main()
{
int n,ncase=;
while(~scanf("%d",&n),n)
{
int cnt=;
for(int i=;i<n;i++)
{
double x1,x2,y1,y2;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
line[cnt].fun(x1,y1,y2,);
y[cnt++]=y1;
line[cnt].fun(x2,y1,y2,-);
y[cnt++]=y2;
}
sort(line,line+cnt);
sort(y,y+cnt);
int d=;
for(int i=;i<cnt;i++)
if(y[i]!=y[i-])y[d++]=y[i];
double ans=;
seg.build(,d-,);
seg.update(,d-,,line[]);
for(int i=;i<cnt;i++)
{
ans+=(line[i].x-line[i-].x)*seg.tree[].len;
seg.update(,d-,,line[i]);
}
printf("Test case #%d\nTotal explored area: %.2f\n\n",++ncase,ans);
}
return ;
}