UVA 1493 Draw a Mess 并查集

                                                      Draw a Mess

It’s graduated season, every students should leave something on the wall, so....they draw a lot ofgeometry shape with different color.When teacher come to see what happened, without getting angry, he was surprised by the talentedachievement made by students. He found the wall full of color have a post-modern style so he want tohave 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 wehave got the order of coming students who drawing on the wall. We found that all students draw fourkinds of geometry shapes in total that is Diamond, Circle, Rectangle and Triangle. When a studentdraw a shape in pixel (i, j) with color c (1 ≤ c ≤ 9), no matter it is covered before, it will be coveredby 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 ≤ r
2 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.
Hint: The final distribution of different colors:
00000000
03300000
03310000
00111000
00022240
00002444
00004444
00000444
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

题意:

给你一个n*m平面,有4种操作,对应把相应的图形区域颜色涂成c(1<= c <= 9)。
现在经过q次操作以后,要求9种颜色的小格子各有多少个

题解:

我倒过来做,就是划过的小格子就不能再画,求各个颜色最多画几个。
并查集记录横向200的图,当前点能够指向同行的那个点,利用并查集加速找到联通的区间。

#include<bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int  N = 50000 + 10;
const int M = 50000 +10;
int parent[211][N],n,m,q,ans[N];
struct ss{
    int sign, xc, yc, a, b ,v;
    ss(){}
    ss(int sign, int xc,int yc,int a,int b,int v) {
        this -> sign = sign;
        this -> xc = xc;
        this -> yc = yc;
        this -> a = a;
        this -> b = b;
        this -> v = v;
    }
}op[M];
void init() {
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m + 1; j++) parent[i][j] = j;
    }
    memset(ans,0,sizeof(ans));
    char ch[50]; int a,b,c,d,e;
    for(int i = 1; i <= q; i++) {
        scanf("%s%d%d%d%d",ch, &a, &b, &c, &d);
        a++,b++;
        if(ch[0] == 'C') op[i] = ss(0,a,b,c,0,d);
        if(ch[0] == 'D') op[i] = ss(1,a,b,c,0,d);
        if(ch[0] == 'R') cin>>e,op[i] = ss(2,a,b,c,d,e);
        if(ch[0] == 'T') op[i] = ss(3,a,b,c,0,d);
    }
}
int finds(int *parent, int x) {
    return parent[x] == x ? x : parent[x] = finds(parent, parent[x]);
}
void gao (int l,int r,int *parent,int v) {
    l = finds(parent, l);
    while(l <= r) {
        ans[v] ++;
        int tmp = finds(parent, l + 1);
        parent[l] = tmp;
        l = tmp;
    }
}
void cir(int a,int b,int r,int v) {
    for(int i = max(1, a - r); i <= min(n, a + r); i++) {
        int len =  (int) sqrt(r * r - (i - a) * (i - a));
        int l = max(1, b - len), r = min(m, b + len);
        gao(l,r,parent[i],v);//cout<<1<<endl;
    }
}
void diamond (int xc,int yc,int r,int v) {
    for(int i = max(1,xc - r); i <= min(n,xc + r); i++) {
         int len = r - abs(i - xc);
        int l = max(1, yc - len) , r = min(m, yc + len);
        gao(l,r,parent[i], v);
    }
}
void rec (int xc,int yc,int h,int w,int v) {
    for(int i = max(1,xc); i <= min(n,xc + h - 1); i++) {
        int l = max(1,yc), r = min(m, yc + w -1);
        gao(l,r,parent[i], v);
    }
}
void tri(int xc,int yc,int w,int v) {
    for(int i = max(1,xc); i <= min(n,xc + (w + 1) /2 - 1); i++) {
        int len = (w - 1) / 2 - i + xc;
        int l = max(1, yc - len), r = min(m, yc + len);
        gao(l, r, parent[i], v);
    }
}
void solve() {
    for(int  i = q; i >= 1; i--) {
        if (op[i].sign == 0)
            cir(op[i].xc, op[i].yc, op[i].a, op[i].v);
        if (op[i].sign == 1)
            diamond(op[i].xc, op[i].yc, op[i].a, op[i].v);
        if (op[i].sign == 2)
            rec(op[i].xc, op[i].yc, op[i].a, op[i].b, op[i].v);
        if (op[i].sign == 3)
            tri(op[i].xc, op[i].yc, op[i].a, op[i].v);
    }
    for(int i = 1; i <= 9; i++) printf("%d%c", ans[i], i == 9 ? '
' : ' ');
}
int main() {
    while(scanf("%d%d%d",&n,&m,&q)!=EOF) {
        init();
        solve();
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zxhl/p/5170207.html