POJ 1177 Picture
题目大意:矩形的周长并(够大意了~)
解题思路:
扫描线,跟面积并一样,只不过处理周长的时候注意边的增加与减少。
下面是代码:
#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; int y,l,r; } edge[10005]; int tempx[10005],binx[10005]; int x1,x2,y1,y2,ans; int cntx,n; bool cmp(node2 a,node2 b) { if(a.y==b.y)return a.num<b.num; return a.y<b.y; } int binnum(int 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 { int disnow; int cnt,segcnt; bool sta1,sta2; } node[20000<<2]; inline void PushUp(int l,int r,int tr) { if(node[tr].cnt) { node[tr].sta1=true; node[tr].sta2=true; node[tr].segcnt=1; node[tr].disnow=binx[r+1]-binx[l]; } else if(l==r) { node[tr].disnow=0; node[tr].segcnt=0; node[tr].sta1=false; node[tr].sta2=false; } else { node[tr].disnow=node[tr<<1].disnow+node[tr<<1|1].disnow; node[tr].sta1=node[tr<<1].sta1; node[tr].sta2=node[tr<<1|1].sta2; node[tr].segcnt=node[tr<<1].segcnt+node[tr<<1|1].segcnt; if(node[tr<<1].sta2&&node[tr<<1|1].sta1) { node[tr].segcnt--; } } } 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,sta; int temp; while(scanf("%d",&n)!=EOF) { for(int i=0; i<n; i++) { scanf("%d%d%d%d",&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); 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; sta=edge[0].num; ans=0; temp=0; for(int i=1; i<2*n; i++) { if(y1!=edge[i].y||sta!=edge[i].num) { ans+=iabs(node[1].disnow-temp); temp=node[1].disnow; sta=edge[i].num; if(y1!=edge[i].y) { ans+=(edge[i].y-y1)*2*node[1].segcnt; 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); } ans+=temp; printf("%d\n",ans); } return 0; }