Explain about the scan lines because what is not drawing
a direct reference this blog Figure understand
Note that the nodes in the line segment tree maintain line segments,
so if tree[l] is a leaf node, it does not maintain the interval [l,l] but the line segment [l,l+1], [L, R] is [L,R+1],
so in pushup is lsh[r+1]-lsh[l];
and when the left and right coordinates are updated between the two partitions, because updating 0-2 is maintaining 0-3, so r The position of the node must be -1
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5+7;
struct node
{
double l,r,h;
int f;//l线段左端点 r右端点 h高度 f标记当前这条线段是矩形的上边界还是下边界
}line[MAXN];
double lsh[MAXN];
double tree[MAXN<<2];
int ct[MAXN];
int n;
bool cmp(node a,node b){
return a.h < b.h;
}
void pushup(int rt,int l,int r)
{
if(ct[rt]) tree[rt] = lsh[r+1]-lsh[l];
else if(l == r) tree[rt] = 0;
else tree[rt] = tree[rt<<1]+tree[rt<<1|1];
}
void build(int rt,int l,int r)
{
tree[rt] = 0;
ct[rt] = 0;
if(l == r) return ;
int mid = (l+r)>>1;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
}
void update(int rt,int l,int r,int ul,int ur,int x)
{
if(l>=ul&&r<=ur){
ct[rt] += x;
pushup(rt,l,r);//覆盖标记发生了变化 因此需要 下推一下 重新复值
return ;
}
int mid = (l+r)>>1;
if(ul <= mid) update(rt<<1,l,mid,ul,ur,x);
if(ur > mid) update(rt<<1|1,mid+1,r,ul,ur,x);
pushup(rt,l,r);
}
int main()
{
int cas = 0;
double x1,x2,y1,y2;
while(~scanf("%d",&n)&&n){
int cnt = 0;
for(int i = 1;i <= n;i ++){
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
lsh[++cnt] = x1;//因为坐标会有小数 所以需要离散化 才能用线段树维护
line[cnt].l = x1;
line[cnt].r = x2;
line[cnt].h = y1;
line[cnt].f = 1;//因为要从下往上扫 所以把下面的当做1
lsh[++cnt] = x2;
line[cnt].l = x1;
line[cnt].r = x2;
line[cnt].h = y2;
line[cnt].f = -1;
}
sort(lsh+1,lsh+cnt+1);
sort(line+1,line+cnt+1,cmp);
int num = unique(lsh+1,lsh+1+cnt)-lsh-1;
//printf("%d\n",num);
build(1,1,num);
double ans = 0;
for(int i = 1;i <= cnt;i ++){
int l = lower_bound(lsh+1,lsh+1+num,line[i].l)-lsh;
int r = lower_bound(lsh+1,lsh+1+num,line[i].r)-lsh-1;
//printf("%d %d\n",l,r);
update(1,1,num,l,r,line[i].f);
ans += tree[1]*(line[i+1].h-line[i].h);
}
printf("Test case #%d\n",++cas);
printf("Total explored area: %.2f\n",ans);
printf("\n");
}
return 0;
}