题意:有一个w*h的墙,在墙上涂色n次,后面涂色会把前面同个位置的颜色覆盖掉,问n次涂色之后,墙面上有几个颜色,每种颜色分别有多少块。
思路:因为 0 <= n <= 100,但是 0 <= w,h <= 10000;如果每次都每一个格子遍历过去,那么时间c差不多10000*10000*100肯定要超时了,但是我们发现n很小,所以我们可以用离散化。把x坐标y坐标离散化,再遍历一下就可以了。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 2e2+10;
struct Node{
int x1, y1, x2, y2, c;
}node[maxn];
int Map[maxn][maxn], x[maxn], y[maxn];
int main(){
int w, h, n, Case = 1;;
while(scanf("%d%d",&w,&h)!=EOF){
if(w == 0|| h == 0)
break;
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(y));
memset(Map, 0, sizeof(Map));
scanf("%d",&n);
int n1 = 0, n2 = 0;
for(int i = 1; i <= n; i++){
scanf("%d%d%d%d%d",&node[i].x1,&node[i].y1,&node[i].x2,&node[i].y2,&node[i].c);
x[n1++] = node[i].x1;
y[n2++] = node[i].y1;
x[n1++] = node[i].x2;
y[n2++] = node[i].y2;
}
sort(x, x + n1); //离散化
sort(y, y + n2);
int lx = unique(x, x+n1) - x;
int ly = unique(y, y+n2) - y;
for(int i = 1; i <= n; i++){
int xl = lower_bound(x, x + lx, node[i].x1) - x;
int xr = lower_bound(x, x + lx, node[i].x2) - x;
int yl = lower_bound(y, y + ly, node[i].y1) - y;
int yr = lower_bound(y, y + ly, node[i].y2) - y;
for(int j = xl; j < xr; j++){
for(int k = yl; k < yr; k++){
Map[j][k] = node[i].c;
}
}
}
int color[maxn];
memset(color, 0, sizeof(color));
int ans = 0;
for(int i = 0; i < lx; i++){
for(int j = 0; j < ly; j++){
if(Map[i][j])
color[Map[i][j]] += (x[i+1] - x[i]) * (y[j+1] - y[j]);
}
}
if(Case > 1)
printf("\n");
printf("Case %d:\n",Case++);
int num = 0;
for(int i = 1; i <= 100; i++){
if(color[i]){
printf("%d %d\n",i, color[i]);
num++;
}
}
if(num <= 1){
printf("There is %d color left on the wall.\n", num);
}
else
printf("There are %d colors left on the wall.\n",num);
}
return 0;
}