题目链接:uva 1493 - Draw a Mess

题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域。

解题思路:用并查集维护每一个位置相应下一个能够上色的位置。然后将上色倒转过来处理,就攻克了颜色覆盖的问题。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm> using namespace std;
const int maxr = 205;
const int maxc = 50005; int N, M, Q, cnt[15];
int f[maxr][maxc]; struct Order {
int sign, star, end;
int x, y, r, w, c;
void set (int sign, int x, int y, int c, int r, int w = 0) {
this->sign = sign;
this->x = x;
this->y = y;
this->c = c;
this->r = r;
this->w = w;
del_star();
} void del_star () {
if (sign < 2) {
star = max(x - r, 0);
end = min(x + r, N - 1);
} else if (sign == 2) {
r = (r + 1) / 2 - 1;
star = x;
end = min(x + r, N-1);
}
} }op[maxc]; int getfar (int* far, int x) {
return x == far[x] ? x : far[x] = getfar(far, far[x]);
} int style (char ch) {
if (ch == 'C')
return 0;
else if (ch == 'D')
return 1;
else if (ch == 'T')
return 2;
else
return 3;
} void init () {
memset(cnt, 0, sizeof(cnt));
for (int i = 0; i <= M; i++) {
for (int j = 0; j < N; j++)
f[j][i] = i;
} char s[20];
int x, y, r, c, w;
for (int i = 1; i <= Q; i++) {
scanf("%s", s);
if (s[0] != 'R') {
scanf("%d%d%d%d", &x, &y, &r, &c);
op[i].set(style(s[0]), x, y, c, r);
} else {
scanf("%d%d%d%d%d", &x, &y, &r, &w, &c);
op[i].set(style(s[0]), x, y, c, r, w);
}
}
} inline int get_R (int r, int x, int i, int sign) {
if (sign == 0)
return (int)sqrt(1.0 * r * r - 1.0 * (x - i) * (x - i));
else if (sign == 1)
return r - abs(x - i);
else if (sign == 2)
return r - (i - x);
return 0;
} int count (int x, int y, int r, int star, int end, int sign) {
int ret = 0;
for (int i = star; i <= end; i++) {
int R = get_R(r, x, i, sign); int mv = max(y - R, 0);
while (mv = getfar(f[i], mv), abs(mv - y) <= R && mv < M) {
ret++;
f[i][mv] = mv+1;
}
}
return ret;
} int count_rec (int x, int y, int r, int l) {
int ret = 0;
for (int i = x; i <= x + r - 1 && i < N; i++) {
int mv = y;
while (mv = getfar(f[i], mv), abs(mv - y) < l && mv < M) {
ret++;
f[i][mv] = mv+1;
}
}
return ret;
} void solve () { for (int i = Q; i; i--) {
int& col = cnt[op[i].c];
if (op[i].sign == 3)
col += count_rec(op[i].x, op[i].y, op[i].r, op[i].w);
else
col += count(op[i].x, op[i].y, op[i].r, op[i].star, op[i].end, op[i].sign);
} for (int i = 1; i <= 9; i++)
printf("%d%c", cnt[i], i == 9 ? '\n' : ' ');
} int main () {
while (scanf("%d%d%d", &N, &M, &Q) == 3) {
init();
solve();
}
return 0;
}

uva 1493 - Draw a Mess(并查集)的更多相关文章

  1. UVA 1493 Draw a Mess(并查集+set)

    这题我一直觉得使用了set这个大杀器就可以很快的过了,但是网上居然有更好的解法,orz... 题意:给你一个最大200行50000列的墙,初始化上面没有颜色,接着在上面可能涂四种类型的形状(填充):  ...

  2. UVA1493 - Draw a Mess(并查集)

    UVA1493 - Draw a Mess(并查集) 题目链接 题目大意:一个N * M 的矩阵,每次你在上面将某个范围上色,不论上面有什么颜色都会被最新的颜色覆盖,颜色是1-9,初始的颜色是0.最后 ...

  3. Draw a Mess (并查集)

    It's graduated season, every students should leave something on the wall, so....they draw a lot of g ...

  4. UVA 572 油田连通块-并查集解决

    题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... # ...

  5. UVA 12232 - Exclusive-OR(带权并查集)

    UVA 12232 - Exclusive-OR 题目链接 题意:有n个数字.一開始值都不知道,每次给定一个操作,I a v表示确认a值为v,I a b v,表示确认a^b = v,Q k a1 a2 ...

  6. UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环

    X-Plosives A secret service developed a new kind ofexplosive that attain its volatile property only ...

  7. UVA 11987 Almost Union-Find (并查集+删边)

    开始给你n个集合,m种操作,初始集合:{1}, {2}, {3}, … , {n} 操作有三种: 1 xx1 yy1 : 合并xx1与yy1两个集合 2 xx1 yy1 :将xx1元素分离出来合到yy ...

  8. UVA - 1160(简单建模+并查集)

    A secret service developed a new kind of explosive that attain its volatile property only when a spe ...

  9. UVa 1455 Kingdom 线段树 并查集

    题意: 平面上有\(n\)个点,有一种操作和一种查询: \(road \, A \, B\):在\(a\),\(b\)两点之间加一条边 \(line C\):询问直线\(y=C\)经过的连通分量的个数 ...

随机推荐

  1. photoshop 安装

    Photoshop 下载: http://www.duote.com/soft/54352.html 下载完后解压选择..\Adobe CS6\Set-up.exe ,点击  Set-up.exe   ...

  2. HDU [P5015] 233 Matrix

    矩阵快速幂 按列递推 #include <iostream> #include <cstdio> #include <cstdlib> #include <a ...

  3. c++学习重点分析

     C++是一种语言,仅仅是它的语法.特性.标准类库就已经是一门非常高深的课程,所以在开始学习的时候,必须先要打好基础.要知道当我们在学习它的时候重点应该注意什么. 一.#include “filena ...

  4. [LeetCode] Longest Substring Without Repeating Characters最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  5. Python 数据类型-2

    序列 包括:字符串 列表 元组 索引操作和切片操作 索引操作:可以从序列中抓取一个特定的项目 切片操作: 获取序列的一个切片,即一部分序列 序列的通用方法: len() 求序列的长度 + 连接2个序列 ...

  6. pip源配置

    1.使用配置文件配置文件[global]trusted-host=mirrors.aliyun.comindex-url=http://mirrors.aliyun.com/pypi/simple/ ...

  7. C#将图片进行马赛克处理

    /// <summary> /// 马赛克处理 /// </summary> /// <param name="bitmap"></par ...

  8. 关于Integer的parseInt(String s, int radix)方法的使用

    我们平时用到Integer.parseInt("123");其实默认是调用了int i =Integer.parseInt("123",10); 其中10代表的 ...

  9. Selenium 2.0自动化测试

    http://blog.sina.com.cn/s/blog_b6142fb401017oo6.html http://www.cnblogs.com/halia/p/3562132.html?utm ...

  10. Codeforces 323C Two permutations

    题目描述 You are given two permutations pp and qq , consisting of nn elements, and mm queries of the for ...