题目大意:
给出n个矩形,形式是左下点和右上点。求它们的面积并。
解题思路:
扫描线算法,对Y进行扫描,线段树查询Y轴扫描某段距离后X轴一共有多长的距离有边,并计算面积。
下面是代码:
#include <set> #include <map> #include <queue> //#include <math.h> #include <vector> #include <string> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> #define eps 1e-8 #define pi acos(-1.0) #define inf 107374182 #define inf64 1152921504606846976 #define lc l,m,tr<<1 #define rc m + 1,r,tr<<1|1 #define iabs(x) ((x) > 0 ? (x) : -(x)) #define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (SIZE)) #define clearall(A, X) memset(A, X, sizeof(A)) #define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE)) #define memcopyall(A, X) memcpy(A , X ,sizeof(X)) #define max( x, y ) ( ((x) > (y)) ? (x) : (y) ) #define min( x, y ) ( ((x) < (y)) ? (x) : (y) ) using namespace std; struct node2 { int num; double y,l,r; } edge[1000]; double tempx[1000],binx[1000]; double x1,x2,y1,y2,ans; int cntx,n; bool cmp(node2 a,node2 b) { return a.y<b.y; } int binnum(double num) { int ll=0,m,rr=cntx-1; while(rr>ll) { m=(ll+rr)>>1; if(binx[m]==num)return m; else if(binx[m]<num)ll=m+1; else rr=m-1; } return ll; } struct node1 { double disnow; int cnt; } node[205<<2]; inline void PushUp(int l,int r,int tr) { if(node[tr].cnt)node[tr].disnow=binx[r+1]-binx[l]; else if(l==r)node[tr].disnow=0; else node[tr].disnow=node[tr<<1].disnow+node[tr<<1|1].disnow; } void update(int L,int R,int num,int l,int r,int tr) { if(L<=l&&r<=R) { node[tr].cnt+=num; PushUp(l,r,tr); return ; } int m=(l+r)>>1; if(L<=m)update(L,R,num,l,m,tr<<1); if(m<R)update(L,R,num,m+1,r,tr<<1|1); PushUp(l,r,tr); } int main() { int case1=1; while(scanf("%d",&n),n) { for(int i=0; i<n; i++) { scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); tempx[2*i]=x1; tempx[2*i+1]=x2; edge[2*i].l=x1; edge[2*i].r=x2; edge[2*i+1]=edge[2*i]; edge[2*i].y=y1; edge[2*i+1].y=y2; edge[2*i].num=1; edge[2*i+1].num=-1; } sort(tempx,tempx+2*n); binx[0]=tempx[0]; cntx=1; for(int i=1; i<2*n; i++) { if(tempx[i]!=binx[cntx-1]) { binx[cntx++]=tempx[i]; } } sort(edge,edge+2*n,cmp); clearall(node,0); ans=0; x1=binnum(edge[0].l); x2=binnum(edge[0].r); x2--; update(x1,x2,edge[0].num,0,cntx-2,1); y1=edge[0].y; for(int i=1; i<2*n; i++) { ans+=node[1].disnow*(edge[i].y-y1); y1=edge[i].y; x1=binnum(edge[i].l); x2=binnum(edge[i].r); x2--; update(x1,x2,edge[i].num,0,cntx-2,1); } printf("Test case #%d\n",case1++); printf("Total explored area: %.2lf\n\n",ans); } return 0; }
POJ 1151 Atlantis,布布扣,bubuko.com
原文地址:http://blog.csdn.net/lin375691011/article/details/38711867