uva1493 - Draw a Mess 并查集路径压缩

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$ \le$n$ \le$200, 1$ \le$m$ \le$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$ \le$c$ \le$9), no matter it is covered before, it will be covered by color c.

There are q (1$ \le$q$ \le$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$ \le$r2 with color c;
  • Diamond: given xc, yc, r, c, and you should cover the pixels (x, y) which satisfied inequality | x - xc| + | y - yc|$ \le$r with color c;
  • Rectangle: given xc, yc, l, w, c, and you should cover the pixels (x, y) which satisfied xc$ \le$x$ \le$xc + l - 1, yc$ \le$y$ \le$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$ \le$xc, x$ \le$n - 1, 0$ \le$yc, y$ \le$m - 1)

Output 

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

  N*M的矩阵,总共个有9种颜色,有4种操作,每种操作在一定范围内涂上某种颜色,后涂的可以覆盖前面涂的,问最后每种颜色有多少个。

  因为行数很少列数很多,所以对每行进行并查集路径压缩。倒着处理,相当于先涂上的颜色不会被后涂上的颜色覆盖,pa为下一个未涂色的结点,那么对行处理路径压缩就会跳过已经涂色的结点,比如当前行第i列涂完了下次就要涂pa[i+1],并且把pa[i+1]也赋给pa[i]。

 

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MAXNODE 105
#define MOD 10000007
#define SIGMA_SIZE 4
typedef long long LL;
using namespace std;

const int MAXN=210;
const int MAXM=50010;
const int MAXQ=50010;

int N,M,Q,pa[MAXN][MAXM],ans[15];

int findset(int *pa,int x){
    return pa[x]==x?x:pa[x]=findset(pa,pa[x]);
}

struct OP{
    char str[20];
    int xc,yc,l,r,w,c;
    OP(){}
    OP(char* str,int xc,int yc,int l,int r,int w,int c):xc(xc),yc(yc),l(l),r(r),w(w),c(c){
        strcpy(this->str,str);
    }
}op[MAXQ];

void solve(int l,int r,int c,int *pa){
    l=findset(pa,l);
    while(l<=r){
        ans[c]++;
        int t=findset(pa,l+1);
        pa[l]=t;
        l=t;
    }
}


void Circle(int xc,int yc,int r,int c){
    for(int i=max(0,xc-r);i<=min(N-1,xc+r);i++){
        int len=(int)sqrt(r*r-(i-xc)*(i-xc));
        solve(max(0,yc-len),min(M-1,yc+len),c,pa[i]);
    }
}

int Diamond(int xc,int yc,int r,int c){
    for(int i=max(0,xc-r);i<=min(N-1,xc+r);i++){
        int len=r-abs(i-xc);
        solve(max(0,yc-len),min(M-1,yc+len),c,pa[i]);
    }
}

void Rectangle(int xc,int yc,int l,int w,int c){
    for(int i=xc;i<=min(N-1,xc+l-1);i++){
        solve(yc,min(M-1,yc+w-1),c,pa[i]);
    }
}

void Triangle(int xc,int yc,int w,int c){
    for(int i=xc;i<=min(N-1,xc+(w+1)/2-1);i++){
        int len=xc+(w-1)/2-i;
        solve(max(0,yc-len),min(M-1,yc+len),c,pa[i]);
    }
}

int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%d%d%d",&N,&M,&Q)!=EOF){
        memset(ans,0,sizeof(ans));
        for(int i=0;i<N;i++)
            for(int j=0;j<=M;j++) pa[i][j]=j;
        int XC,YC,L,R,W,C;
        char str[10];
        for(int i=0;i<Q;i++){
            scanf("%s",str);
            if(str[0]=='C'||str[0]=='D'){
                scanf("%d%d%d%d",&XC,&YC,&R,&C);
                op[i]=OP(str,XC,YC,0,R,0,C);
            }
            else if(str[0]=='R'){
                scanf("%d%d%d%d%d",&XC,&YC,&L,&W,&C);
                op[i]=OP(str,XC,YC,L,0,W,C);
            }
            else if(str[0]=='T'){
                scanf("%d%d%d%d",&XC,&YC,&W,&C);
                op[i]=OP(str,XC,YC,0,0,W,C);
            }
        }
        for(int i=Q-1;i>=0;i--){
            if(op[i].str[0]=='C') Circle(op[i].xc,op[i].yc,op[i].r,op[i].c);
            else if(op[i].str[0]=='D') Diamond(op[i].xc,op[i].yc,op[i].r,op[i].c);
            else if(op[i].str[0]=='R') Rectangle(op[i].xc,op[i].yc,op[i].l,op[i].w,op[i].c);
            else if(op[i].str[0]=='T') Triangle(op[i].xc,op[i].yc,op[i].w,op[i].c);
        }
        for(int i=1;i<=8;i++) printf("%d ",ans[i]);
        printf("%d\n",ans[9]);
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值