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 ...
随机推荐
- 解决调用Office组件的问题
在修改一个之前工作的好好的工具的时候出了如下错误: 无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型“Microsoft.Office.Interop.Excel ...
- 将base64编码转换图片
/// <summary> /// 二进制Base64编码转图片 /// </summary> /// <param name="bytes"> ...
- 堆优化dijkstra
单源最短路径 题目链接:https://www.luogu.org/problemnew/show/P4779 直到做了这个题才发现我之前写的堆优化dijkstra一直是错的.. 这个堆优化其实很容易 ...
- 【操作系统作业-lab4】 linux 多线程编程和调度器
linux多线程编程 参考:https://blog.csdn.net/weibo1230123/article/details/81410241 https://blog.csdn.net/skyr ...
- Nginx无法加载.woff .eot .svg .ttf等解决办法
在Nginx的配置文件,加上以下代码即可修复该问题 location ~ \.(eot|otf|ttf|woff|svg)$ { add_header Access-Control-Allow-Ori ...
- linux下载利器之curl和wget的区别
linux下载利器-------curl和wget的区别 curl和wget基础功能有诸多重叠,如下载等. 在高级用途上的curl由于可自定义各种请求参数所以长于模拟web请求,用于测试网页交互(浏览 ...
- Lucene实战
导包
- 【转载】谈MongoDB的应用场景
引用:http://blog.csdn.net/adparking/article/details/38727911 MongoDB的应用场景在网上搜索了下,很少介绍关于传统的信息化应用中如何使用Mo ...
- MLT教程:从BXL文件导入Altium Designer原理图封装和PCB封装
在TI官网的封装文件中提供弄BXL文件可以导出Altium Designer的封装库和原理图库. 这个界面往下面拉会看到: 然后可以下载各种封装的bxl文件了.下面视频说明bxl文件如何导出成功. 如 ...
- POJ:3579-Median(二分+尺取寻找中位数)
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9201 Accepted: 3209 Description Gi ...