hdu Paint the Wall 1543

Paint the Wall

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 607    Accepted Submission(s): 208


Problem Description
Here comes our future artist. See, he is painting again, this time on home's white wall, using different color of paint. Let me see, red, yellow, black, green... but why does he just paint rectangles? Pretty guy, seems he is fond of it.

So, after he's done his great job, the white wall has been filled with so many blocks of color. Of course, some color previously painted has been covered by some color painted later. Now, the little guy has some doubt that how many different colors have been left on the wall, and what are the areas of them. As a seven-year-old boy, he has just learned painting in kindergarten, math seems too difficult for him. So he turns to you, a college student good at math and programming, to help him figuring it out. Don't make him disappointed.
 

Input
Input consists of multiple test cases, each describing "a great job" done by out little guy.

Each case begins with a line containing two integers, Height and Width, the size of the wall. The next line contains an integer N, which is the number of rectangles that have been painted. N lines follow, describing the rectangles in the order they were painted. Each line contains five integers, Top, Left, Bottom, Right, and Color, giving out the position, size and color information of the rectangle.

The range of Height and Width is [1, 10000]. There will be at least 1, and at most 100 rectangles to be painted. For each rectangle, Top and Bottom is in the range [0, Height], Left and Right is in the range [0, Width]. Bottom is strictly greater than Top, and Right is strictly greater than Left. Color will be in the range [1, 100].

The top-left coordinate of the wall is (0, 0), and the bottom-right coordinate of the wall is (Height, Width), as shown below.


(0,0) (0,W)
---------------
| |
| |
| |
| |
| |
---------------
(H,0) (H,W)



The last case is followed by a line containing two zeroes.

There is a blank line between two test cases.
 

Output
For each painting, first output "Case X:" in a single line where X is the case number starting form 1. Then output the colors left and their areas, one color per line, in the order of color numbers (increasing). For each color, you should output the color number, a blank space, and the area of this color on the wall. After that, you should output a single line "There is M color left on the wall." or "There are M colors left on the wall.", depending on M, which is the number of colors left on the wall.

Output a blank line between two test cases.
 

Sample Input
10 5
1
1 1 2 2 2
4 4
2
0 0 3 3 1
2 2 4 4 2
 
0 0
 

Sample Output
Case 1: 2 1 There is 1 color left on the wall. Case 2: 1 8 2 4 There are 2 colors left on the wall.
 

Author
DAI, Wenbin
 

Source
 

Recommend
linle
//直接离散化处理 46Ms
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define N 102

struct node
{
    int x1,y1,x2,y2,color;
}rct[N];   //储存矩形
int x[N<<1],y[N<<1],yhash[10002]; yhash[i]代表y坐标为i的y在离散化后数组中的位置
int hash[N<<1],rc[N]; rc用来加点速度
int area[N];
int main()
{
    int h,w,T=0;
    int n,nn,yid,rcid;
    int i,j;
    while(scanf("%d %d",&h,&w),h|w)
    {
        memset(rc,0,sizeof(rc));
        memset(area,0,sizeof(area));
        scanf("%d",&n);
        for(j=i=0;i<n;i++)
        {
            scanf("%d %d %d %d %d",&rct[i].x1,&rct[i].y1,&rct[i].x2,&rct[i].y2,&rct[i].color);
            rc[rct[i].color]=1;
            x[j]=rct[i].x1;
            y[j]=rct[i].y1;
            j++;
            x[j]=rct[i].x2;
            y[j]=rct[i].y2;
            j++;
        }
        nn=n<<1;
        sort(x,x+nn);
        sort(y,y+nn);
        rcid=0;
        for(i=0;i<N;i++)
         if(rc[i])
         rc[rcid++]=i;
        yid=0;yhash[y[0]]=0;
        for(i=1;i<nn;i++)//离散化
         if(y[i]!=y[i-1])
         {
             y[++yid]=y[i];
             yhash[y[i]]=yid;
         }
        for(i=1;i<nn;i++)
        {
            if(x[i]==x[i-1]) continue;

            for(j=0;j<n;j++)//判断哪个矩形适用该坐标区间
              if(rct[j].x1<=x[i-1]&&rct[j].x2>=x[i])
              {
                  for(int k=yhash[rct[j].y1];y[k]<rct[j].y2;k++)
                    hash[k]=rct[j].color;
              }
            for(j=0;j<yid;j++)
            if(hash[j])
             {
                 area[hash[j]]+=(x[i]-x[i-1])*(y[j+1]-y[j]);
                 hash[j]=0;
             }
        }
       if(T) printf("\n");
       printf("Case %d:\n",++T);
       for(j=0,i=0;i<rcid;i++)
        if(area[rc[i]])
         {
             j++;
             printf("%d %d\n",rc[i],area[rc[i]]);
         }
       if(j<=1)
        printf("There is %d color left on the wall.\n",j);
       else
        printf("There are %d colors left on the wall.\n",j);
    }

    return 0;
}


//离散化再线段树 93Ms ,估计数据太小,没体现出来优势、
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define N 102
#define lson l,m,k<<1
#define rson m,r,k<<1|1
struct node
{
    int x1,y1,x2,y2,color;
}rct[N];

int color[N<<5];

int x[N<<1],y[N<<1];
int rc[N];
int area[N];

/*void build(int l,int r,int k)
{
    color[k]=-1;//这里写-1是错滴、、-1代表杂色,开始啥也没,哪来杂色、要是写0倒可以
    if(l+1==r)
     return ;
    int m=(l+r)>>1;
    build(lson);
    build(rson);
}*/
void down(int &k)
{
    color[k<<1]=color[k];
    color[k<<1|1]=color[k];
    color[k]=-1;
}
int flag;
void update(int &y1,int &y2,int l,int r,int k)
{
     if(y1<=y[l]&&y2>=y[r])
     {
         color[k]=flag;
         return ;
     }
     if(color[k]!=-1)
       down(k);
     int m=(l+r)>>1;
     if(y1<y[m]) update(y1,y2,lson);
     if(y2>y[m]) update(y1,y2,rson);
}
int differ;
void query(int l,int r,int k)
{
    if(color[k]!=-1)
    {
        color[k]?area[color[k]]+=differ*(y[r]-y[l]):1;
        return ;
    }
    int m=(l+r)>>1;
    query(lson);
    query(rson);
}
int main()
{
    int h,w,T=0;
    int n,nn,yid,rcid;
    int i,j;
    while(scanf("%d %d",&h,&w),h|w)
    {
        memset(rc,0,sizeof(rc));
        memset(area,0,sizeof(area));
        scanf("%d",&n);
        for(j=i=0;i<n;i++)
        {
            scanf("%d %d %d %d %d",&rct[i].x1,&rct[i].y1,&rct[i].x2,&rct[i].y2,&rct[i].color);
            rc[rct[i].color]=1;
            x[j]=rct[i].x1;
            y[j]=rct[i].y1;
            j++;
            x[j]=rct[i].x2;
            y[j]=rct[i].y2;
            j++;
        }
        nn=n<<1;
        sort(x,x+nn);
        sort(y,y+nn);
        rcid=0;
        for(i=0;i<N;i++)
         if(rc[i])
         rc[rcid++]=i;
        yid=0;
        for(i=1;i<nn;i++)
         if(y[i]!=y[i-1])
            y[++yid]=y[i];
       // build(0,yid,1);
        for(i=1;i<nn;i++)
        {
            if(x[i]==x[i-1]) continue;
             for(j=0;j<n;j++)
              if(rct[j].x1<=x[i-1]&&rct[j].x2>=x[i])
              {
                  flag=rct[j].color;
                  update(rct[j].y1,rct[j].y2,0,yid,1);
              }
              differ=x[i]-x[i-1];
              query(0,yid,1);
              color[1]=0;
        }

       if(T) printf("\n");
       printf("Case %d:\n",++T);
       for(j=0,i=0;i<rcid;i++)
        if(area[rc[i]])
         {
             j++;
             printf("%d %d\n",rc[i],area[rc[i]]);
         }
       if(j<=1)
        printf("There is %d color left on the wall.\n",j);
       else
        printf("There are %d colors left on the wall.\n",j);
    }

    return 0;
}

 

posted on 2012-10-14 19:19  江财小子  阅读(502)  评论(0编辑  收藏  举报