zoj 3544 路径压缩

 
Draw a Mess

Time Limit: 5 Seconds      Memory Limit: 262144 KB

It's graduated season, every students should leave something on the wall, so....they draw a lot of geometry shape with different color.

When teacher come to see what happened, without getting angry, he was surprised by the talented achievement made by students. He found the wall full of color have a post-modern style so he want to have an in-depth research on it.

To simplify the problem, we divide the wall into n*m (1 ≤ n ≤ 200, 1 ≤ m ≤ 50000) pixels, and we have got the order of coming students who drawing on the wall. We found that all students draw four kinds of geometry shapes in total that is Diamond, Circle, Rectangle and Triangle. When a student draw a shape in pixel (i, j) with color c (1 ≤ c ≤ 9), no matter it is covered before, it will be covered by color c.

There are q (1 ≤ q ≤ 50000) students who have make a drawing one by one. And after q operation we want to know the amount of pixels covered by each color.

Input

There are multiple test cases.

In the first line of each test case contains three integers n, m, q. The next q lines each line contains a string at first indicating the geometry shape:

  • Circle: given xc, yc, r, c, and you should cover the pixels(x, y) which satisfied inequality (x - xc)2 + (y - yc)2 ≤ r2 with color c;
  • Diamond: given xc, yc, r, c, and you should cover the pixels(x, y) which satisfied inequality abs(x - xc) + abs(y - yc) ≤ r with color c;
  • Rectangle: given xc, yc, l, w, c, and you should cover the pixels(x, y) which satisfied xc ≤ x ≤ xc+l-1, yc ≤ y ≤ yc+w-1 with color c;
  • Triangle: given xc, yc, w, c, W is the bottom length and is odd, the pixel(xc, yc) is the middle of the bottom. We define this triangle is isosceles and the height of this triangle is (w+1)/2, you should cover the correspond pixels with color c;

Note: all shape should not draw out of the n*m wall! You can get more details from the sample and hint. (0 ≤ xc, xn-1, 0 ≤ yc, ym-1)

Output

For each test case you should output nine integers indicating the amount of pixels covered by each color.

Sample Input
8 8 4
Diamond 3 3 1 1
Triangle 4 4 3 2
Rectangle 1 1 2 2 3
Circle 6 6 2 4
Sample Output
4 4 4 11 0 0 0 0 0
Hint

The final distribution of different colors:
00000000
03300000
03310000
00111000
00022240
00002444
00004444
00000444

线段树做一直超时

从后往前涂色,每涂一次色,就把对应格子删除,记next[i] = i; 当i这个格子删除的时候 next[i] = i+1 ,再利用路径压缩,和并查集的思想是一样的,这样平摊的复杂度分析为 O(n*m)

# include <math.h>
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <algorithm>
# include <iostream>
# include <string>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <vector>
# include <cstring>
# include <list>
# include <ctime>

# define For(i,a)   for((i)=0;i<(a);(i)++)
# define MAX(x,y)   ((x)>(y)? (x):(y))
# define MIN(x,y)   ((x)<(y)? (x):(y))
# define MEM(a)     (memset((a),0,sizeof(a)))
# define MEME(a)    (memset((a),-1,sizeof(a)))
# define MEMX(a)    (memset((a),0x7f,sizeof(a)))

using namespace std;

typedef long long           ll      ;
typedef unsigned long long  ull     ;
typedef unsigned int        uint    ;
typedef unsigned char       uchar   ;

#define N 205
#define M 100500
char str[M][25];
int xx[M],yy[M],p[M],t[M],c[M];
int next[N][M];
int find(int i,int t)
{
    if(next[i][t]!=t)
        next[i][t]=find(i,next[i][t]);
    return next[i][t];
}
int dele(int i,int l,int r)
{
    int ret=0;
    l=find(i,l);
    while(l<=r)
    {
        ret++;
        next[i][l]=l+1;
        l=find(i,l);
    }
    return ret;
}
inline ll isqrt(long long x) //开方取整
{
     long long l = 0, r = 100860, m;
     while (l < r)
     {
         m = (l + r) / 2;
         if (m * m <= x)
            l = m + 1;
         else
            r = m;
     }
     return r - 1;
}

int main()
{
    int n,m,q;
    int i,j,x,y,r,w,l,h,color;
    int tmp1,tmp2,tmp3;
    int ans[10];
    while(scanf("%d%d%d",&n,&m,&q)!=EOF)
    {
        for(i=0;i<n;i++)
            for(j=0;j<=m;j++)
                next[i][j]=j;
        memset(ans,0,sizeof(ans));
        for(i=0;i<q;i++)
        {
            scanf("%s%d%d%d%d",str[i],&xx[i],&yy[i],&tmp1,&tmp2);
            if(str[i][0]!='R')
                p[i]=tmp1,c[i]=tmp2;
            else
            {
                scanf("%d",&tmp3);
                p[i]=tmp1;t[i]=tmp2;c[i]=tmp3;
            }
        }
        for(i=q-1;i>=0;i--)
        {
            x=xx[i],y=yy[i];
            if(str[i][0]=='D')
            {
                r=p[i],color=c[i];
                for(j=MAX(0,x-r);j<=MIN(n-1,x+r);j++)
                    ans[color]+=dele(j,MAX(0,y-r+abs(x-j)),MIN(m-1,y+r-abs(x-j)));
            }
            else if(str[i][0]=='T')
            {
                w=p[i],color=c[i];
                h=(w+1)/2;
                for(j=MAX(0,x);j<MIN(n,x+h);j++)
                    ans[color]+=dele(j,MAX(0,y-h+j-x+1),MIN(m-1,y+h-j+x-1));
            }
            else if(str[i][0]=='R')
            {
                l=p[i],w=t[i],color=c[i];
                for(j=MAX(0,x);j<=MIN(n-1,x+l-1);j++)
                    ans[color]+=dele(j,y,MIN(m-1,y+w-1));
            }
            else if(str[i][0]=='C')
            {
                r=p[i];color=c[i];
                for(j=MAX(0,x-r);j<=MIN(n-1,x+r);j++)
                {
                    int tmp=isqrt(1LL*r*r-(j-x)*(j-x));
                    ans[color]+=dele(j,MAX(0,y-tmp),MIN(m-1,y+tmp));
                }
            }
        }
        for(i=1;i<=9;i++)
            printf("%d%c",ans[i],(i==9)?'\n':' ');
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值