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 ...
随机推荐
- ssd论文解读
https://www.sohu.com/a/168738025_717210 https://www.cnblogs.com/lillylin/p/6207292.html https://blog ...
- nvl()函数和nvl2()函数
如果你某个字段为空,但是你想让这个字段显示0,可以使用nvl(字段名,0),当然这个0也可以换成其他东西,如:1,2,3…… 一 NVL(表达式1,表达式2) 如果表达式1为空值,NVL返回值为表达式 ...
- egg- 配置
1. model module.exports = app => { const { INTEGER, STRING, TEXT } = app.Sequelize; const User = ...
- LEA指令与MOV指令区别
Tips: LEA指令与MOV指令的区别: ① MOV指令是 数据 传送指令-------传送数据 LEA指令是 有效地址 传送指令-------取偏移地址 ② MOV OPRD1 ...
- 彻底弄懂session,cookie,token
session,cookie和token究竟是什么 简述 我在写之前看了很多篇session,cookie的文章,有的人说先有了cookie,后有了session.也有人说先有session,后有co ...
- thinkphp5,单图,多图,上传
/** * 上传单图 */ function upload($path, $filename) { $file = request()->file($filename); $info = $fi ...
- JZOJ| 5910. DuLiu
Description LF是毒瘤出题人中AK IOI2019,不屑于参加NOI的唯一的人.他对人说话,总是满口垃圾题目者也,教人半懂不懂的.因为他姓李,别人便从QQ群上的“毒瘤李F ...
- php COM
查看php.ini中是否已经开启了com.allow_dcom = true 从php/ext/里面查找一下有没有这个php_com_dotnet.dll这个文件 如果没有网上下载个,一般都会有的吧应 ...
- 汉罗塔问题——Python
汉罗塔问题就是一个循环的过程:* (有两种情况) 如果被移动盘只有一个盘子,可以直接移动到目的盘 但是被移动盘有多个盘子,就先需要将上面的n-1个盘子通过目的盘移动到辅助盘,然后将被移动盘最下面一个盘 ...
- 硬件中断--DEBUG系列
问题描述: 在线调试时,全速运行,程序进入硬件中断,查看堆栈窗口,发现是从A函数进去的.但是A函数应该没有问题的: 再次重复,发现是从B函数进去的,但是B函数之前运行起来也没有问题的,而且没有传入参数 ...