Discretization: store all X axis coordinates in an array... sort. When entering a line segment, determine the discrete values corresponding to the left and right points by means of binary...
Scanning line .. it can be seen as a straight line parallel to the X axis .. start scanning up until y = 0 .. until the last side parallel to the X axis is scanned .. but when we do it .. you do not need to fully simulate this process .. the scanning line starts from the bottom side to the top side.
Line Segment tree ..
Several diagrams describe the process of Line Scanning... line segment tree maintenance ..:
Initial status
Sweep to the bottom line and click Update 1 ~ 3 is 1
Sweep to the second line. At this time, the length of the counter is not 0 * the length of the two lines online, get the green area, and add it to the answer. Then update the count.
Similarly, add the yellow area to the answer.
Similarly, add the gray area to the answer.
Similarly, add the purple area to the answer.
Similarly, add the blue area to the answer.
#include<iostream>#include<stdio.h>#include<string.h>#include<set>#include <ctime>#include<queue>#include<algorithm>#include<cmath>#define oo 1000000007#define ll long long#define pi acos(-1.0)#define MAXN 405using namespace std;struct node{ double l,r,y; int tp; bool operator <(node a) const { return y<a.y; }}line[MAXN<<2];int n,Times[MAXN<<2];double X[MAXN<<2],sum[MAXN];int b_search(double x){ int l,r,mid; l=0,r=n+1; while (r-l>1) { mid=(l+r)>>1; if (X[mid]<=x) l=mid; else r=mid; } return l;}void update(int x,int c,int l,int r,int now){ if (l==r) { Times[x]+=c; if (Times[x]) sum[now]=X[x+1]-X[x]; if (!Times[x]) sum[now]=0; return; } int mid=(l+r)/2; if (x<=mid) update(x,c,l,mid,now<<1); if (mid<x) update(x,c,mid+1,r,(now<<1)|1); sum[now]=sum[now<<1]+sum[(now<<1)|1]; return;}int main(){ int i,j,num,T=0; double ans=0; while (~scanf("%d",&n) && n) { num=0; for (i=1;i<=n;i++) { double x1,y1,x2,y2; scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); line[i*2-1].y=y1,line[i*2-1].l=x1,line[i*2-1].r=x2,line[i*2-1].tp=1; line[i*2].y=y2,line[i*2].l=x1,line[i*2].r=x2,line[i*2].tp=-1; X[++num]=x1,X[++num]=x2; } n=n*2; sort(X+1,X+1+num); sort(line+1,line+1+n); memset(sum,0,sizeof(sum)); memset(Times,0,sizeof(Times)); ans=0; for (i=1;i<=n;i++) { ans+=sum[1]*(line[i].y-line[i-1].y); int l,r; l=b_search(line[i].l); r=b_search(line[i].r)-1; for (j=l;j<=r;j++) update(j,line[i].tp,1,n-1,1); } printf("Test case #%d\nTotal explored area: %.2f\n\n",++T,ans); } return 0;}