USACO3.14Shaping Regions(离散化)
参考着崔神写的 离散化为小矩形 再依次判断是否在所给矩形中 之前比赛做过一道类似的
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 ID:shangca2 3 PROG:rect1 4 LANG:C++ 5 */ 6 #include <iostream> 7 #include<cstdio> 8 #include<cstring> 9 #include<algorithm> 10 #include<stdlib.h> 11 #include<cmath> 12 using namespace std; 13 int x1[1010],x2[1010],y2[1010]; 14 int x[3010],y[3010],co[1010],cou[3010],fx[10010],fy[10010]; 15 int main() 16 { 17 freopen("rect1.in","r",stdin); 18 freopen("rect1.out","w",stdout); 19 int i,j,a,b,k,n,y1[1010]; 20 cin>>a>>b>>n; 21 for(i = 1 ; i <= n ;i++) 22 { 23 cin>>x1[i]>>y1[i]>>x2[i]>>y2[i]>>co[i]; 24 x[i*2-1] = x1[i]; 25 x[i*2] = x2[i]; 26 y[i*2-1] = y1[i]; 27 y[i*2] = y2[i]; 28 } 29 sort(x+1,x+2*n+1); 30 sort(y+1,y+2*n+1); 31 int num=2; 32 fx[0] = 1; 33 for(i = 1; i <= 2*n ; i++) 34 { 35 if(!fx[x[i]]) 36 fx[x[i]] = num++; 37 } 38 if(!fx[a]) 39 fx[a] = num++; 40 int xx = num-1; 41 num = 2; 42 fy[0] = 1; 43 for(i = 1; i <= 2*n ; i++) 44 { 45 if(!fy[y[i]]) 46 fy[y[i]] = num++; 47 } 48 if(!fy[b]) 49 fy[b]=num++; 50 int yy = num-1,r=1,c=1; 51 for(i = 0 ; i <= 10000 ; i++) 52 { 53 if(fx[i]) 54 x[r++] = i; 55 if(fy[i]) 56 y[c++] = i; 57 } 58 for(i = 1 ; i < xx ; i++) 59 for(j=1 ; j < yy ; j++) 60 { 61 for(k = n ; k >=1 ; k--) 62 { 63 if(x1[k]<=x[i]&&y1[k]<=y[j]&&x2[k]>=x[i+1]&&y2[k]>=y[j+1]) 64 { 65 cou[co[k]]+=(x[i+1]-x[i])*(y[j+1]-y[j]); 66 break; 67 } 68 } 69 if(k==0) 70 cou[1]+=(x[i+1]-x[i])*(y[j+1]-y[j]); 71 } 72 for(i = 1 ; i <= 2500 ; i++) 73 if(cou[i]!=0) 74 cout<<i<<" "<<cou[i]<<endl; 75 return 0; 76 }