扫描线权值线段树

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#pragma GCC optimize(3,"Ofast","inline")
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
const int N=100010;
int n;
vector<double>ys;
struct node{
double x,y1,y2;
int d;
bool operator<(const node &qwe)const{
return x<qwe.x;
}
}seg[N*2];
struct no{
int l,r;
int cnt;
double len;
}tr[N*8];
void build(int u,int l,int r){
tr[u]={l,r,0,0};
if(l!=r){
int mid=(l+r)/2;
build(u*2,l,mid);
build(u*2+1,mid+1,r);
}
}
int find(double y){
return lower_bound(ys.begin(),ys.end(),y)-ys.begin();
}
void pushup(int u){
if(tr[u].cnt)
tr[u].len=ys[tr[u].r+1]-ys[tr[u].l];
else if(tr[u].l!=tr[u].r){
tr[u].len=tr[u*2].len+tr[u*2+1].len;
}
else tr[u].len=0;
}
void modify(int u,int l,int r,int d){
if(tr[u].l>=l&&tr[u].r<=r){
tr[u].cnt+=d;
pushup(u);
}
else{
int mid=(tr[u].r+tr[u].l)/2;
if(l<=mid) modify(u*2,l,r,d);
if(r>mid) modify(u*2+1,l,r,d);
pushup(u);
}
}
void solve()
{ int T=1;
while(cin>>n,n!=0){
ys.clear();
int j=0;
for(int i=0;i<n;i++){
double x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
seg[j++]={x1,y1,y2,1};
seg[j++]={x2,y1,y2,-1};
ys.push_back(y1);
ys.push_back(y2);
}
sort(seg,seg+j);
sort(ys.begin(),ys.end());
ys.erase(unique(ys.begin(),ys.end()),ys.end());
build(1,0,ys.size()-2);
double res=0;
for(int i=0;i<j;i++){
if(i!=0)
res+=tr[1].len*(seg[i].x-seg[i-1].x);
modify(1,find(seg[i].y1),find(seg[i].y2)-1,seg[i].d);

}
cout<<"Test case #"<<T++<<endl;
cout<<"Total explored area: "<<fixed<<setprecision(2)<<res<<endl<<endl;
}
}

signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
solve();
return 0;
}