POJ-1177 Picture 矩形覆盖周长并
题目链接:http://poj.org/problem?id=1177
比矩形面积并麻烦点,需要更新竖边的条数(平行于x轴扫描)。。求横边的时候,保存上一个结果,加上当前长度与上一个结果差的绝对值就行了。。。
1 //STATUS:C++_AC_32MS_1416KB 2 #include <functional> 3 #include <algorithm> 4 #include <iostream> 5 //#include <ext/rope> 6 #include <fstream> 7 #include <sstream> 8 #include <iomanip> 9 #include <numeric> 10 #include <cstring> 11 #include <cassert> 12 #include <cstdio> 13 #include <string> 14 #include <vector> 15 #include <bitset> 16 #include <queue> 17 #include <stack> 18 #include <cmath> 19 #include <ctime> 20 #include <list> 21 #include <set> 22 #include <map> 23 using namespace std; 24 //using namespace __gnu_cxx; 25 //define 26 #define pii pair<int,int> 27 #define mem(a,b) memset(a,b,sizeof(a)) 28 #define lson l,mid,rt<<1 29 #define rson mid+1,r,rt<<1|1 30 #define PI acos(-1.0) 31 //typedef 32 typedef __int64 LL; 33 typedef unsigned __int64 ULL; 34 //const 35 const int N=20010; 36 const int INF=0x3f3f3f3f; 37 const int MOD=100000,STA=8000010; 38 const LL LNF=1LL<<60; 39 const double EPS=1e-8; 40 const double OO=1e15; 41 const int dx[4]={-1,0,1,0}; 42 const int dy[4]={0,1,0,-1}; 43 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 44 //Daily Use ... 45 inline int sign(double x){return (x>EPS)-(x<-EPS);} 46 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;} 47 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;} 48 template<class T> inline T lcm(T a,T b,T d){return a/d*b;} 49 template<class T> inline T Min(T a,T b){return a<b?a:b;} 50 template<class T> inline T Max(T a,T b){return a>b?a:b;} 51 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);} 52 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);} 53 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));} 54 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));} 55 //End 56 57 struct Seg{ 58 int y,x1,x2; 59 int c; 60 Seg(){} 61 Seg(int a,int b,int c,int d):y(a),x1(b),x2(c),c(d){} 62 bool operator < (const Seg& a)const{ 63 return y<a.y; 64 } 65 }seg[N]; 66 bool lnod[N<<2],rnod[N<<2]; 67 int len[N<<2],cnt[N<<2],cnt2[N<<2]; 68 int n,m; 69 70 void pushup(int l,int r,int rt) 71 { 72 if(cnt[rt]){ 73 lnod[rt]=rnod[rt]=true; 74 len[rt]=r-l+1; 75 cnt2[rt]=2; 76 } 77 else if(l==r)cnt2[rt]=lnod[rt]=rnod[rt]=len[rt]=0; 78 else { 79 int ls=rt<<1,rs=rt<<1|1; 80 lnod[rt]=lnod[ls],rnod[rt]=rnod[rs]; 81 len[rt]=len[ls]+len[rs]; 82 cnt2[rt]=cnt2[ls]+cnt2[rs]; 83 if(rnod[ls] && lnod[rs])cnt2[rt]-=2; 84 } 85 } 86 87 void update(int a,int b,int c,int l,int r,int rt) 88 { 89 if(a<=l && r<=b){ 90 cnt[rt]+=c; 91 pushup(l,r,rt); 92 return; 93 } 94 int mid=(l+r)>>1; 95 if(a<=mid)update(a,b,c,lson); 96 if(b>mid)update(a,b,c,rson); 97 pushup(l,r,rt); 98 } 99 100 int main() 101 { 102 // freopen("in.txt","r",stdin); 103 int i,j,k,l,r,a,b,c,d,ans,last; 104 const int M=10000; 105 while(~scanf("%d",&n)) 106 { 107 m=0; 108 for(i=0;i<n;i++){ 109 scanf("%d%d%d%d",&a,&b,&c,&d); 110 a+=M,b+=M,c+=M,d+=M; 111 seg[m++]=Seg(b,a,c,1); 112 seg[m++]=Seg(d,a,c,-1); 113 } 114 sort(seg,seg+m); 115 mem(len,0);mem(cnt,0),mem(cnt2,0); 116 mem(lnod,0);mem(rnod,0); 117 ans=last=0; 118 for(i=0;i<m-1;i++){ 119 update(seg[i].x1,seg[i].x2-1,seg[i].c,0,20000,1); 120 ans+=cnt2[1]*(seg[i+1].y-seg[i].y); 121 ans+=abs(len[1]-last); 122 last=len[1]; 123 } 124 update(seg[i].x1,seg[i].x2-1,seg[i].c,0,20000,1); 125 126 printf("%d\n",ans+abs(len[1]-last)); 127 } 128 return 0; 129 }