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 ≤ 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 ≤ r 2 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, 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.
题解:想了好久,感觉要用到并查集,然后有点无从下手,然后参考了网上的博客,用暴力去给行涂色,再利用并查集的操作来维护列即可,但是G++通过不了
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
const double pi=3.14;
double eps=0.000001;
int fa[100005];
int vis[100005];
int find(int x)
{
if (fa[x]==x)
return x;
else return fa[x]=find(fa[x]);
}
struct node
{
char op[12];
int x,y,z,d;
int e;
node() {}
};
node tm[100005];
int ans[10];
int main()
{
int n,m,k;
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
memset(ans,0,sizeof ans);
for (int i=1; i<=k; i++)
{
scanf("%s%d%d%d%d",tm[i].op,&tm[i].x,&tm[i].y,&tm[i].z,&tm[i].d);
if (tm[i].op[0]=='R') scanf("%d",&tm[i].e);
}
for (int j=0; j<n; j++)
{
for (int i=0; i<=m; i++) fa[i]=i,vis[i]=0;
for (int i=k; i>=1; i--)
{
int l,r,col=tm[i].d;
if (tm[i].op[0]=='C')
{
int up=tm[i].x+tm[i].z;
int down=tm[i].x-tm[i].z;
if (!(j>=down&&j<=up ))continue;
int tmp=tm[i].z*tm[i].z-(tm[i].x-j)*(tm[i].x-j);
tmp=sqrt(tmp);
l=tm[i].y-tmp;
r=tm[i].y+tmp;
}
if (tm[i].op[0]=='D')
{
int up=tm[i].x+tm[i].z;
int down=tm[i].x-tm[i].z;
if (!(j>=down&&j<=up ))continue;
l=tm[i].z-abs(j-tm[i].x);
r=tm[i].y+l;
l=tm[i].y-l;
}
if (tm[i].op[0]=='R')
{
col=tm[i].e;
int up=tm[i].x+tm[i].z-1;
int down=tm[i].x;
if (!(j>=down&&j<=up ))continue;
l=tm[i].y;
r=tm[i].y+tm[i].d-1;
}
if (tm[i].op[0]=='T')
{
int up=tm[i].x+(tm[i].z+1)/2-1;
int down=tm[i].x;
if (!(j>=down&&j<=up ))continue;
int tmp=(tm[i].z-1)/2+(tm[i].x-j);
l=tm[i].y-tmp;
r=tm[i].y+tmp;
}
l=max (l,0);
r=min(r,m-1);
int fx=find(l);
for (int i=r; i>=l;)
{
int fy=find(i);
if (!vis[fy]) ans[col]++;
vis[fy]=1;
if (fx!=fy) fa[fy]=fx;
i=fy-1;
}
}
}
for (int i=1; i<=9; i++)
{
if (i>1) printf(" ");
printf("%d",ans[i]);
}
printf("\n");
}
return 0;
}
Draw a Mess (并查集)的更多相关文章
- UVA1493 - Draw a Mess(并查集)
UVA1493 - Draw a Mess(并查集) 题目链接 题目大意:一个N * M 的矩阵,每次你在上面将某个范围上色,不论上面有什么颜色都会被最新的颜色覆盖,颜色是1-9,初始的颜色是0.最后 ...
- uva 1493 - Draw a Mess(并查集)
题目链接:uva 1493 - Draw a Mess 题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域. 解题思路:用并查集维护每一个位置相应下一 ...
- UVA 1493 Draw a Mess(并查集+set)
这题我一直觉得使用了set这个大杀器就可以很快的过了,但是网上居然有更好的解法,orz... 题意:给你一个最大200行50000列的墙,初始化上面没有颜色,接着在上面可能涂四种类型的形状(填充): ...
- 并查集(涂色问题) HDOJ 4056 Draw a Mess
题目传送门 题意:给出一个200 * 50000的像素点矩阵,执行50000次操作,每次把一个矩形/圆形/菱形/三角形内的像素点涂成指定颜色,问最后每种颜色的数量. 分析:乍一看,很像用线段树成段更新 ...
- 【HDOJ】4056 Draw a Mess
这题用线段树就MLE.思路是逆向思维,然后每染色一段就利用并查集将该段移除,均摊复杂度为O(n*m). /* 4056 */ #include <iostream> #include &l ...
- POJ 2912 - Rochambeau - [暴力枚举+带权并查集]
题目链接:http://poj.org/problem?id=2912 Time Limit: 5000MS Memory Limit: 65536K Description N children a ...
- CodeForces Roads not only in Berland(并查集)
H - Roads not only in Berland Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- POJ2912 Rochambeau [扩展域并查集]
题目传送门 Rochambeau Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4463 Accepted: 1545 ...
- POJ2912:Rochambeau(带权并查集)
Rochambeau Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5208 Accepted: 1778 题目链接:h ...
随机推荐
- P1330 封锁阳光大学(二分图染色)
题目描述 曹是一只爱刷街的老曹,暑假期间,他每天都欢快地在阳光大学的校园里刷街.河蟹看到欢快的曹,感到不爽.河蟹决定封锁阳光大学,不让曹刷街. 阳光大学的校园是一张由N个点构成的无向图,N个点之间由M ...
- 洛谷【P1175】表达式的转换
浅谈栈:https://www.cnblogs.com/AKMer/p/10278222.html 题目传送门:https://www.luogu.org/problemnew/show/P1175 ...
- java.util Properties使用记录
转:http://www.2cto.com/px/201006/47834.html 在java.util 包下面有一个类 Properties,该类主要用于读取以项目的配置文件(以.properti ...
- JavaScript之使用JavaScript模仿oop编程
第一, 首先,使用关键字function定义一个类 function Shape1(ax,ay) {//此时将function看成声明类的标志 ; ; var init=function () {// ...
- WCF服务用户名密码访问
有2种方式, 第一直接在程序中指定用户名密码,配置调用 private void BtnSearch_Click(object sender, EventArgs e) { try { var cli ...
- VS 关于无法打开项目文件,此安装不支持该项目类型的问题
用VS打开后有时会出现类似: 无法打开项目文件,此安装不支持该项目类型 的错误,这个错误一般都是由于用低版本VS打开高版本项目文件造成的 其中包括: 1.用VS2003 打开包括VS2005以上版本项 ...
- 06_android虚拟机介绍 05_sdkManager介绍
如果你不对SDK Manager进行设置,它默认会去谷歌官网下载最新的内容.由于官网被墙了,要么你FQ要么去设置代理.通过代理去下载相关的内容. 每一个android版本都对应着一个API的版本号.如 ...
- 使用JFileChooser保存文件
--------------------siwuxie095 工程名:TestFileChooser 包名:com.siwuxie095 ...
- C++中的对象的赋值和复制
对象的赋值 如果对一个类定义了两个或多个对象,则这些同类的对象之间可以互相赋值,或者说,一个对象的值可以赋给另一个同类的对象.这里所指的对象的值是指对象中所有数据成员的值. 对象之间的赋值也是通过赋值 ...
- Spring 框架学习整理
JDBC操作数据库的基本入门中存在什么问题? * 导致驱动注册两次是个问题,但不是严重的. * 严重的问题:是当前类和mysql的驱动类有很强的依赖关系. * 当我们没有驱动类的时候 ...