hdu 1543 Paint the Wall

省赛后有一周了,这一周过的很无聊,每天课也不想上,做题吧, 也没有状态,整天过的昏昏噩噩的。。

今天上午又没有去上课,在寝室没事了就想写道题。。

以前看过这道题,没怎么想,感觉就是线段树。今天再一看,只有100个矩形,数据量不是很大,离散化就可以搞定。

离散化+二分。。31msAC。

少加了一个初始化,悲剧了n次。

# include<stdio.h>

# include<stdlib.h>

# include<string.h>

# define N 105

int col[N];

int X[2*N],Y[2*N],xs,ys,adj[2*N][2*N];

int cmp(const void *a,const void *b)

{

    return *(int *)a - *(int *)b;

}

int findx(int ans)

{

    int left,right,mid;

    left=1;

    right=xs;

    while(right>=left)

    {

        mid=(right+left)/2;

        if(X[mid]==ans) return mid;

        else if(X[mid] > ans) right=mid-1;

        else left=mid+1;

    }

}

int findy(int ans)

{

    int left,right,mid;

    left=1;

    right=ys;

    while(right>=left)

    {

        mid=(right+left)/2;

        if(Y[mid]==ans) return mid;

        else if(Y[mid]>ans) right=mid-1;

        else left=mid+1;

    }

}

int main()

{

    int i,j,stx,sty,edx,edy,H,W,num,ncase=0,count,k;

    int x1[N],y1[N],x2[N],y2[N],color[N];

    while(scanf("%d%d",&H,&W)!=EOF)

    {

        if(H==0 && W==0) break;

        scanf("%d",&num);

        ncase++;

        if(ncase!=1) printf("\n");

        k=0;

        for(i=1;i<=num;i++)

        {

            scanf("%d%d%d%d%d",&x1[i],&y1[i],&x2[i],&y2[i],&color[i]);

            k++;

            X[k]=x1[i];

            Y[k]=y1[i];

            k++;

            X[k]=x2[i];

            Y[k]=y2[i];

        }

        qsort(X+1,k,sizeof(X[1]),cmp);

        qsort(Y+1,k,sizeof(Y[1]),cmp);

        xs=1;

        for(i=2;i<=k;i++)

        {

            if(X[i]!=X[i-1])

            {

                xs++;

                X[xs]=X[i];

            }

        }

        ys=1;

        for(i=2;i<=k;i++)

        {

            if(Y[i]!=Y[i-1])

            {

                ys++;

                Y[ys]=Y[i];

            }

        }

        memset(adj,0,sizeof(adj));

        for(i=1;i<=num;i++)

        {

            stx=findx(x1[i]);

            sty=findy(y1[i]);

            edx=findx(x2[i]);

            edy=findy(y2[i]);

            for(j=stx;j<edx;j++)

            {

                for(k=sty;k<edy;k++)

                {

                    adj[j][k]=color[i];

                }    

            }

        }

        memset(col,0,sizeof(col));

        for(i=1;i<xs;i++)

            for(j=1;j<ys;j++)

                col[adj[i][j]]+=(X[i+1]-X[i])*(Y[j+1]-Y[j]);

                printf("Case %d:\n",ncase);

                count=0;

        for(i=1;i<=100;i++)

            if(col[i]!=0) {printf("%d %d\n",i,col[i]);count++;}

            if(count==1||count==0) printf("There is %d color left on the wall.\n",count);

            else printf("There are %d colors left on the wall.\n",count);

    }

    return 0;

}

你可能感兴趣的:(paint)