ZOJ 3544 / HDU 4056 Draw a Mess( 并查集好题 )
方法参见:http://blog.acmol.com/?p=751
从最后一个线段开始倒着处理(因为之后的线段不会被它之前的线段覆盖),把这条线段所覆盖的所有线段编号合并到一个集合里,并以最左边线段编号为父结点。然后,以后的线段每次都是从右端向左端进行以下处理:
1、判断该线段在并查集中的根结点是否被覆盖过(用一个数组标记),如果没有被覆盖,则将该线段所在集合与海报左端点所在集合进行合并(以左端点所在集合为根)。
2、然后开始处理刚处理过的线段父结点左边的那一个线段,处理方法与第1步时一样。
3、直到要处理的线段在左端点的左边时停止循环。
处理时,如果有一个线段未被覆盖,就证明该点的染色没有被之后的染色覆盖掉。
我头一次知道还有这种搞法,涨姿势了。
PS.之前我觉得Diamond和Circle是弄出来应该是一样的形状,就把它俩当一样处理了,事实证明这是不对的……
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm> using namespace std; const int MAXN = ; struct node
{
char op[];
int xc, yc;
int l, w, c;
}; int N, M, Q;
node D[MAXN];
int p[MAXN];
bool vis[MAXN]; int find( int x )
{
return p[x] == x ? x : p[x] = find(p[x]);
} int main()
{
while ( scanf( "%d%d%d", &N, &M, &Q ) == )
{
for ( int i = ; i < Q; ++i )
{
scanf( "%s%d%d", D[i].op, &D[i].xc, &D[i].yc );
if ( D[i].op[] == 'R' )
scanf("%d%d%d", &D[i].l, &D[i].w, &D[i].c );
else
scanf( "%d%d", &D[i].w, &D[i].c );
} int ans[] = { };
for ( int i = ; i < N; ++i )
{
for ( int j = ; j < M; ++j )
{
vis[j] = false;
p[j] = j;
}
for ( int j = Q - ; j >= ; --j )
{
int l, r;
int color = D[j].c;
if ( D[j].op[] == 'R' )
{
if ( i < D[j].xc || i >= D[j].xc + D[j].l ) continue;
l = D[j].yc;
r = D[j].yc + D[j].w - ;
}
else if ( D[j].op[] == 'D' )
{
if ( i < D[j].xc - D[j].w || i > D[j].xc + D[j].w ) continue;
l = D[j].yc - ( D[j].w - abs( i - D[j].xc ) );
r = D[j].yc + ( D[j].w - abs( i - D[j].xc ) );
}
else if ( D[j].op[] == 'C' )
{
if ( ( i - D[j].xc )*( i - D[j].xc ) > D[j].w*D[j].w ) continue;
int ww = (int)( sqrt( ( double)(D[j].w*D[j].w - ( i - D[j].xc )*( i - D[j].xc ) ) ) + 1e- );
l = D[j].yc - ww;
r = D[j].yc + ww;
}
else
{
if ( i < D[j].xc || i > D[j].xc + (D[j].w+)/ - ) continue;
int ww = D[j].w - *( i - D[j].xc );
l = D[j].yc - ww / ;
r = D[j].yc + ww / ;
} l = max( , l );
r = min( M - , r ); int xx = find(l);
int yy;
for ( int k = r; k >= l; k = yy - )
{
yy = find(k);
if ( !vis[yy] ) ++ans[color];
vis[yy] = true;
if ( xx != yy ) p[yy] = xx;
}
}
} for ( int i = ; i <= ; ++i )
{
if ( i != ) putchar(' ');
printf( "%d", ans[i] );
}
puts("");
}
return ;
}
之前线段树也能做,只不过效率和代码长度都比较惊悚。
ZOJ上因为内存给的比较宽松,以前线段树可以AC,但是现在好像把数据加强了,线段树一定TLE……
HDU一定MLE……
附:线段树代码,TLE
#include <cstdio>
#include <cstdlib>
#include <cstring> #define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define lc rt << 1
#define rc rt << 1 | 1 const int MAXN = ; int N, M, Q;
short tree[][ MAXN << ];
char op[];
int ans[]; inline int max( int a, int b )
{
return a > b ? a : b;
} inline int min( int a, int b )
{
return a < b ? a : b;
} inline int abs( int a )
{
return a >= ? a : -a;
} inline void update( short *color, int L, int R, short val, int l, int r, int rt )
{
if ( L <= l && r <= R )
{
color[rt] = val;
return;
}
//if ( l >= r ) return; if ( color[rt] )
{
color[lc] = color[rc] = color[rt];
color[rt] = ;
} int m = ( l + r ) >> ;
if ( L <= m ) update( color, L, R, val, lson );
if ( R > m ) update( color, L, R, val, rson ); //if ( color[lc] == color[rc] ) color[rt] = color[lc];
return;
} inline void query( short *color, int l, int r, int rt )
{
if ( color[rt] != )
{
//printf("[%d %d]: %d\n", l, r, color[rt] );
ans[ color[rt] ] += r - l + ;
return;
}
if ( l >= r ) return;
int m = ( l + r ) >> ;
query( color, lson );
query( color, rson );
return;
} int main()
{
while ( scanf( "%d%d%d", &N, &M, &Q ) == )
{
for ( int i = ; i < N; ++i )
memset( tree[i], , sizeof(short)*(( N << ) + ) ); while ( Q-- )
{
scanf( "%s", op );
if ( op[] == 'D' || op[] == 'C' )
{
int xc, yc, r, c;
scanf("%d%d%d%d", &xc, &yc, &r, &c);
int stX = max( , xc - r );
int edX = min( N - , xc + r );
//printf( "stX=%d edX=%d\n", stX, edX );
if ( r == )
{
update( tree[xc], yc, yc, c, , M - , );
continue;
} for ( int i = stX; i <= edX; ++i )
{
int stY = max( , yc - ( r - abs( i - xc ) ) );
int edY = min( M - , yc + ( r - abs(i - xc) ) );
//printf("**%d %d\n", stY, edY );
update( tree[i], stY, edY, c, , M - , );
}
}
else if ( op[] == 'T' )
{
int xc, yc, w, c;
scanf( "%d%d%d%d", &xc, &yc, &w, &c );
int stx = xc, sty = yc;
int limitX = min( N - , xc + (w+)/ - );
//printf( "T: %d %d\n", xc, limitX );
for ( int i = stx; i <= limitX && w >= ; ++i )
{
update( tree[i], max( sty - w/, ), min( sty + w/, M - ), c, , M - , );
w -= ;
if ( w < ) break;
}
}
else if ( op[] == 'R' )
{
int xc, yc, l, w, c;
scanf( "%d%d%d%d%d", &xc, &yc, &l, &w, &c );
if ( l == || w == ) continue;
int limitX = min( xc + l - , N - );
int limitY = min( yc + w - , M - );
//printf("R: %d %d %d %d\n", xc, limitX, yc, limitY );
for ( int i = xc; i <= limitX; ++i )
update( tree[i], yc, limitY, c, , M - , );
}
} memset( ans, , sizeof(ans) );
for ( int j = ; j < N; ++j )
query( tree[j], , M - , ); bool first = false;
for ( int i = ; i <= ; ++i )
{
if ( first ) putchar(' ');
printf( "%d", ans[i] );
first = true;
}
puts("");
}
return ;
}
ZOJ 3544 / HDU 4056 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种颜色各占多少的区域. 解题思路:用并查集维护每一个位置相应下一 ...
- Draw a Mess (并查集)
It's graduated season, every students should leave something on the wall, so....they draw a lot of g ...
- HDU 1213 - How Many Tables - [并查集模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...
- UVA 1493 Draw a Mess(并查集+set)
这题我一直觉得使用了set这个大杀器就可以很快的过了,但是网上居然有更好的解法,orz... 题意:给你一个最大200行50000列的墙,初始化上面没有颜色,接着在上面可能涂四种类型的形状(填充): ...
- hdu 1213 求连通分量(并查集模板题)
求连通分量 Sample Input2 //T5 3 //n m1 2// u v2 34 5 5 12 5 Sample Output24 # include <iostream> # ...
- 【HDOJ】4056 Draw a Mess
这题用线段树就MLE.思路是逆向思维,然后每染色一段就利用并查集将该段移除,均摊复杂度为O(n*m). /* 4056 */ #include <iostream> #include &l ...
- HDU HDU1558 Segment set(并查集+判断线段相交)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...
- hdu 1257 小希的迷宫 并查集
小希的迷宫 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 D ...
随机推荐
- python spark wingide
- Linux利用i节点删除乱码文件
Linux删除乱码文件 当文件名为乱码的时候,无法通过键盘输入文件名,所以在终端下就不能直接利用rm,mv等命令管理文件了. 但是我们知道每个文件都有一个i节点号,我们可以考虑通过i节点号来管理文件. ...
- 旧文备份:AVR读写EEPROM分析
由于AVR的EEPROM写周期比较长(一般为毫秒级),因此在编程使用过程中要特别注意.对于读EEPROM没什么好说的,读一个字节的数据要耗费4个时钟周期,可以忍受,写就比较麻烦了,虽然放在EEPROM ...
- PEP8 常用规范
PEP8 常用规范 完整的规范移步这里两个传送门 pep8规范 官方文档:https://www.python.org/dev/peps/pep-0008/ PEP8中文翻译:http://www.c ...
- javascript入门笔记8-window对象
History 对象 history对象记录了用户曾经浏览过的页面(URL),并可以实现浏览器前进与后退相似导航的功能. 注意:从窗口被打开的那一刻开始记录,每个浏览器窗口.每个标签页乃至每个框架,都 ...
- MacBookPro 存储空间优化
首先,打开电脑内的"终端"; 其次,逐条录入下面的命令行,执行完成后,再次查看您的储存空间,多少会有一些优化,具体会优化出多少储存空间因您日常使用而定(本人清出了5G 空间,还不错 ...
- 使用phpExcel将数据批量导出
if(isset($_POST['export']) && $_POST['export'] == '导出所选数据') { //此处为多选框已勾选的数据 $export_id=$_PO ...
- 触发ionic弹窗区域外的方法
最近项目需要在页面弹窗的时候需要点击弹窗区域外的地方,其实也就是点击页面HTML就可以关闭弹窗, 首先在controller通过js获取到html的dom节点,然后绑定点击事件,话不多说上代码: ...
- tomcat8080端口占用解决办法
打开控制台,在窗口中输入指令:netstat -ano | findstr 8080 指令的意思是找出占用8080端口的进程pid 上图中表示占用进程pid为23288,然后再次输入指令: ...
- mysql 命令 小结
CREATE DATABASE IF NOT EXISTS yourdbname DEFAULT CHARSET utf8 COLLATE utf8_general_ci;创建中文数据库show gl ...