取出纵向边按x坐标排序,在y方向上建立线段树。
每次查询当前有效长度len,ans += len*(x[i]-x[i-1]); 其中len为T[rt].len;
查询完毕后更新y方向上线段树,入边+1, 出边-1。
#include<bits/stdc++.h>
using namespace std;
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
typedef long long ll;
struct L{
double x, y1, y2;
int d;
L(){}
L(double x, double y1, double y2, int d): x(x), y1(y1), y2(y2), d(d){}
};
L line[];
bool cmp(L A, L B){
return A.x < B.x;
}
double y[]; struct Node{
int d;
double len;
};
Node T[<<];
void init(){
memset(T, , sizeof(T));
}
void pushup(int rt, int l, int r){
if(T[rt].d)
T[rt].len = y[r]-y[l-];
else
T[rt].len = l == r? : T[rt<<].len+T[rt<<|].len;
}
void update(int L, int R, int d, int l, int r, int rt){
if(L <= l&&r <= R){
T[rt].d += d;
pushup(rt, l , r);
return ;
}
int m = (l+r)>>;
if(L <= m) update(L, R, d, lson);
if(R > m) update(L, R, d, rson);
pushup(rt, l, r);
} int main(){
int n, ca = ;
double x1, y1, x2, y2;
while(scanf("%d", &n), n){
for(int i = ; i < n; i++){
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
line[i*] = L(x1, y1, y2, );
line[i*+] = L(x2, y1, y2, -);
y[i*] = y1, y[i*+] = y2;
}
sort(line, line+*n, cmp);
sort(y, y+*n); init();
double ans = ;
for(int i = ; i < *n; i++){
if(i&&line[i].x != line[i-].x)
ans += (line[i].x-line[i-].x)*T[].len;
int l = lower_bound(y, y+*n, line[i].y1)-y+, r = lower_bound(y, y+*n, line[i].y2)-y;
if(l <= r)
update(l, r, line[i].d, , *n, );
}
printf("Test case #%d\n", ca++);
printf("Total explored area: %.2f\n\n", ans);
}
return ;
}
无pushdown()函数,每条线段只存一次。d表示被覆盖的次数,len表示至少被覆盖一次的合法长度。详见pushup()函数。
ans += T[1].len*(x[i]-x[i-1]);
求面积交:方法同求面积并,外加len2表示至少被覆盖两次的合法长度,ans += T[1].len2*(x[i]-x[i-1]);
求周长并:方法同面积并,扫描两次,分别沿x方向和y方向,每次加上 更新前后T[1].len的差值的绝对值。