http://acm.hdu.edu.cn/showproblem.php?pid=1543
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 10000
using namespace std; int h,w,n;
int X[maxn],Y[maxn],m[][],clo[maxn];
struct node
{
int x1,y1,x2,y2,c;
}p[maxn*]; int bs(int key,int l,int r,int a[])
{
int low=l,high=r;
while(low<=high)
{
int mid=(low+high)>>;
if(a[mid]==key)
{
return mid;
}
if(a[mid]<key)
low=mid+;
else if(a[mid]>key)
high=mid-;
}
}
int main()
{
int case1=;
while(scanf("%d%d",&h,&w)!=EOF)
{
if(h==&&w==) break;
scanf("%d",&n);
int x1,y1,x2,y2,c;
int t=;
memset(X,,sizeof(X));
memset(Y,,sizeof(Y));
for(int i=; i<n; i++)
{
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&c);
if(x1>x2) swap(x1,x2);
if(y1>y2) swap(y1,y2);
p[i].x1=x1;p[i].x2=x2;p[i].y1=y1;p[i].y2=y2;p[i].c=c;
X[t]=x1;Y[t++]=y1;
X[t]=x2;Y[t++]=y2;
}
sort(X,X+t);
sort(Y,Y+t);
int t1=,t2=;
memset(m,,sizeof(m));
for(int i=; i<t; i++) if(X[i]!=X[i-]) X[t1++]=X[i];
for(int i=; i<t; i++) if(Y[i]!=Y[i-]) Y[t2++]=Y[i];
for(int i=; i<n; i++)
{
int xx1=bs(p[i].x1,,t1-,X);
int yy1=bs(p[i].y1,,t2-,Y);
int xx2=bs(p[i].x2,,t1-,X);
int yy2=bs(p[i].y2,,t2-,Y);
for(int j=xx1; j<xx2; j++)
{
for(int k=yy1; k<yy2; k++)
{
m[j][k]=p[i].c;
}
}
}
memset(clo,,sizeof(clo));
for(int i=; i<t1; i++)
{
for(int j=; j<t2; j++)
{
if(m[i][j])
{
clo[m[i][j]]+=(X[i+]-X[i])*(Y[j+]-Y[j]);
}
}
}
int t3=;
if(case1) printf("\n");
printf("Case %d:\n",++case1);
for(int i=; i<=; i++)
{
if(clo[i]) {t3++;printf("%d %d\n",i,clo[i]);}
}
if(t3==)
{
printf("There is 1 color left on the wall.\n");
}
else
printf("There are %d colors left on the wall.\n",t3); }
}