您的位置:首页 > 其它

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

2014-12-26 19:54 405 查看
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
| x - xc| + | 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,
x

n - 1,
0

yc,
y

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: