题目大意:
给出各个房子阴影的长度(以区间表示)和高度,求阴影的总面积。
解题思路:矩形面积并。
下面是代码:
#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; long long y,l,r; } edge[80005]; long long tempx[80005],binx[80005]; long long x1,x2,y1,y2,ans; int cntx,n; bool cmp(node2 a,node2 b) { return a.y<b.y; } int binnum(long long 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 { long long disnow; int cnt; } node[80005<<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)!=EOF) { for(int i=0; i<n; i++) { scanf("%d%d%d",&x1,&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=0; 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("%lld\n",ans); } return 0; }
原文地址:http://blog.csdn.net/lin375691011/article/details/38815821