http://acm.hdu.edu.cn/showproblem.php?pid=1543
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn 10000 5 using namespace std; 6 7 int h,w,n; 8 int X[maxn],Y[maxn],m[500][500],clo[maxn]; 9 struct node 10 { 11 int x1,y1,x2,y2,c; 12 }p[maxn*4]; 13 14 int bs(int key,int l,int r,int a[]) 15 { 16 int low=l,high=r; 17 while(low<=high) 18 { 19 int mid=(low+high)>>1; 20 if(a[mid]==key) 21 { 22 return mid; 23 } 24 if(a[mid]<key) 25 low=mid+1; 26 else if(a[mid]>key) 27 high=mid-1; 28 } 29 } 30 int main() 31 { 32 int case1=0; 33 while(scanf("%d%d",&h,&w)!=EOF) 34 { 35 if(h==0&&w==0) break; 36 scanf("%d",&n); 37 int x1,y1,x2,y2,c; 38 int t=0; 39 memset(X,0,sizeof(X)); 40 memset(Y,0,sizeof(Y)); 41 for(int i=0; i<n; i++) 42 { 43 scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&c); 44 if(x1>x2) swap(x1,x2); 45 if(y1>y2) swap(y1,y2); 46 p[i].x1=x1;p[i].x2=x2;p[i].y1=y1;p[i].y2=y2;p[i].c=c; 47 X[t]=x1;Y[t++]=y1; 48 X[t]=x2;Y[t++]=y2; 49 } 50 sort(X,X+t); 51 sort(Y,Y+t); 52 int t1=1,t2=1; 53 memset(m,0,sizeof(m)); 54 for(int i=1; i<t; i++) if(X[i]!=X[i-1]) X[t1++]=X[i]; 55 for(int i=1; i<t; i++) if(Y[i]!=Y[i-1]) Y[t2++]=Y[i]; 56 for(int i=0; i<n; i++) 57 { 58 int xx1=bs(p[i].x1,0,t1-1,X); 59 int yy1=bs(p[i].y1,0,t2-1,Y); 60 int xx2=bs(p[i].x2,0,t1-1,X); 61 int yy2=bs(p[i].y2,0,t2-1,Y); 62 for(int j=xx1; j<xx2; j++) 63 { 64 for(int k=yy1; k<yy2; k++) 65 { 66 m[j][k]=p[i].c; 67 } 68 } 69 } 70 memset(clo,0,sizeof(clo)); 71 for(int i=0; i<t1; i++) 72 { 73 for(int j=0; j<t2; j++) 74 { 75 if(m[i][j]) 76 { 77 clo[m[i][j]]+=(X[i+1]-X[i])*(Y[j+1]-Y[j]); 78 } 79 } 80 } 81 int t3=0; 82 if(case1) printf(" "); 83 printf("Case %d: ",++case1); 84 for(int i=1; i<=100; i++) 85 { 86 if(clo[i]) {t3++;printf("%d %d ",i,clo[i]);} 87 } 88 if(t3==1) 89 { 90 printf("There is 1 color left on the wall. "); 91 } 92 else 93 printf("There are %d colors left on the wall. ",t3); 94 95 } 96 }