HDU 1255 覆盖的面积 线段树+扫描线

时间:2023-03-08 19:34:55

同 POJ1151 这次是两次

#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 N=;
double y[N<<];
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[N*];
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[N*];
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,T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&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("%.2f\n",ans);
}
return ;
}