具体的思路有空补上,先贴代码
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int const nMax = 100000;
struct T{
int y[2];
double f[2];
double x;
int left;
void make(double a,double b,double c,int d){
x=a,f[0]=b,f[1]=c;
left = d;
}
bool operator < (const T& a) const {return x < a.x;}
} node[nMax];
struct A{
double x;
int id;
bool operator < (const A& b) const {return x < b.x;}
};
A tmp[nMax];
double f[nMax];
int cov[nMax << 2];
double len[nMax<<2];
int flag[nMax << 2];
int n,m;
#define lson (u<<1)
#define rson ((u<<1)+1)
void build(int u,int l,int r){
cov[u] = len[u] = 0;
if(1+l==r) return ;
int m=(l+r)>>1;
build(lson,l,m);
build(rson,m,r);
}
void setlen(int u,int l,int r){
if(cov[u]>0) len[u] = f[r]-f[l];
else {
if(r==l+1) len[u]=0;
else len[u] = len[lson] + len[rson];
}
}
void ins(int u,int l,int r,int a,int b,int k){
if(l==a && r==b){
cov[u] += k;
setlen(u,l,r);
return ;
}
int m = (l+r) >> 1;
if(b<=m)
ins(lson,l,m,a,b,k);
else if(a>=m)
ins(rson,m,r,a,b,k);
else
{
ins(lson,l,m,a,m,k);
ins(rson,m,r,m,b,k);
}
setlen(u,l,r);
}
double x[2],y[2];
double const eps = 1.0e-9;
int dcmp(double x) {
if(x>-eps && x<eps) return 0;
return x > 0 ? 1 : -1;
}
int main() {
int cas = 1;
while(scanf("%d",&n),n){
m = 1;
for(int i=0;i<n;i++) {
scanf("%lf%lf%lf%lf",&x[0],&y[0],&x[1],&y[1]);
node[m++].make(x[0],y[0],y[1],1);
node[m++].make(x[1],y[0],y[1],-1);
}
int cnt = 0;
for(int i=1;i<m;i++) {
tmp[cnt].x=node[i].f[0];tmp[cnt].id=-i;cnt++;
tmp[cnt].x=node[i].f[1];tmp[cnt].id=i;cnt++;
}
sort(tmp,tmp+cnt);
double now=tmp[0].x;int cur=0;
for(int i=0;i<cnt;i++) {
if(dcmp(tmp[i].x-now)){
now = tmp[i].x;
cur ++;
}
if(tmp[i].id>0) {
node[tmp[i].id].y[1] = cur;
}else node[-tmp[i].id].y[0] = cur;
f[cur] = now;
}
build(1,0,cur);
sort(node+1,node+m);
double ans=0;
for(int i=1;i<m-1;i++) {
ins(1,0,cur,node[i].y[0],node[i].y[1],node[i].left);
ans += len[1] * (node[i+1].x-node[i].x);
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n",cas++,ans);
return 0;
}