登 录
poj 1177
http://acm.pku.edu.cn/JudgeOnline/problem?id=1177
求N 个矩形并的边的周长。用离散话通过了,还不知道如果用线段树做,在学习中ing , 发现自己好菜。
#include "iostream" #include "cstdlib" #include "cstdio" #include "algorithm" using namespace std; const int MaxN = 5010; int X[MaxN << 1], Y[MaxN << 1]; int XX[MaxN << 1], YY[MaxN << 1]; int x1,x2,y1,y2; int N; typedef struct superSeg{ int x1,x2,y; bool ceil; void insert(int _x1,int _x2,int _y, bool _ceil){ x1 = _x1; x2 = _x2; y = _y; ceil = _ceil; } }superSeg; superSeg Xray[MaxN <<1], Yray[MaxN <<1]; bool compar1(const superSeg& a,const superSeg& b){ if(a.y > b.y) return true; else return false; } bool compar2(const superSeg& a,const superSeg& b){ if(a.y < b.y) return true; else return false; } void input(){ scanf("%d",&N); int k = 0; for(int i = 0 ;i < N; ++i){ scanf("%d%d%d%d",&x1,&y1,&x2,&y2); X[k] = x1; Xray[k].insert(x1,x2,y1,false); Y[k] = y1; Yray[k].insert(y1,y2,x1,true); ++k; X[k] = x2; Xray[k].insert(x1,x2,y2,true); Y[k] = y2; Yray[k].insert(y1,y2,x2,false); ++k; } } int unique(int arr[],int ARR[]){ sort(arr, arr + 2 * N); int cnt = 0; int i ,j,k; i = j = 0; while(j < 2 * N){ if(arr[i] == arr[j]) ++j; else{ ARR[cnt++] = arr[i]; i = j; ++j; } } ARR[cnt++] = arr[j -1]; return cnt; } int solve(){ int ret = 0; int cnt1 = unique(X,XX); int cnt2 = unique(Y,YY); sort(Xray,Xray + 2 * N,compar1); sort(Yray,Yray + 2 * N,compar2); for(int i = 0 ;i < cnt1 -1; ++i){ int count = 0; for(int j = 0; j < 2 * N; ++j){ if(Xray[j].x1 <= XX[i] && Xray[j].x2 >= XX[i+1] && Xray[j].ceil){ ++count; if(count == 1) ret += XX[i+1] - XX[i]; }else if(Xray[j].x1 <= XX[i] && Xray[j].x2 >= XX[i+1]){ --count; if(count == 0) ret += XX[i+1] - XX[i]; } } } for(int i = 0 ;i < cnt2 -1; ++i){ int count = 0; for(int j = 0 ;j < 2 * N; ++j){ if(Yray[j].x1 <= YY[i] && Yray[j].x2 >= YY[i+1] && Yray[j].ceil){ ++count; if(count == 1) ret += YY[i+1] - YY[i]; }else if(Yray[j].x1 <= YY[i] && Yray[j].x2 >= YY[i+1]){ --count; if(count == 0) ret += YY[i+1] - YY[i]; } } } return ret; } int main(){ input(); printf("%d/n",solve()); return 0; }
要好好再学一下线段树,o -.-b o
抱歉!评论已关闭.