线段树的节点对应的是一个区间 而不是区间的端点。。
#include <bits/stdc++.h>
#define lson (rt << 1)
#define rson (rt << 1 | 1)
#define mid (L + R >> 1)
using namespace std;
const int N = 1e5+10;
vector<double> pos;
struct Seg
{
double l,r,h;
int f;
Seg(double a,double b,double c,int d)
{
l = a, r = b;
h = c, f = d;
}
bool operator < (const Seg &b)const
{
return h < b.h;
}
};
struct node
{
int cnt;
double len;
}t[N<<4];
vector<Seg> segs;
int n;
int getid(double x)
{
return lower_bound(pos.begin(),pos.end(),x) - pos.begin() + 1;
}
void pushdown(int L,int R,int rt)
{
if(t[rt].cnt) t[rt].len = pos[R]-pos[L-1];
else if(L == R) t[rt].len = 0;
else t[rt].len = t[lson].len + t[rson].len;
}
void update(int l,int r,int v,int L = 1,int R = pos.size(),int rt = 1)
{
if(l > r) return;
if(l <= L && R <= r)
{
t[rt].cnt += v;
pushdown(L,R,rt);
return;
}
if(l <= mid) update(l,r,v,L,mid,lson);
if(r > mid) update(l,r,v,mid+1,R,rson);
pushdown(L,R,rt);
}
int main()
{
int id = 1;
double x1,y1,x2,y2;
while(cin >> n && n)
{
segs.clear(); pos.clear();
for(int i = 0; i < n; ++i)
{
cin >> x1 >> y1 >> x2 >> y2;
segs.push_back(Seg(x1,x2,y1,1));
segs.push_back(Seg(x1,x2,y2,-1));
pos.push_back(x1); pos.push_back(x2);
}
sort(pos.begin(),pos.end());
sort(segs.begin(),segs.end());
pos.erase(unique(pos.begin(),pos.end()),pos.end());
double ans = 0;
for(int i = 0; i < segs.size(); ++i)
{
int l = getid(segs[i].l), r = getid(segs[i].r)-1;
update(l,r,segs[i].f);
ans += t[1].len * (segs[i+1].h - segs[i].h);
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n",id++,ans);
}
}