方法参见: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( 并查集好题 )的更多相关文章

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

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

  2. uva 1493 - Draw a Mess(并查集)

    题目链接:uva 1493 - Draw a Mess 题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域. 解题思路:用并查集维护每一个位置相应下一 ...

  3. Draw a Mess (并查集)

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

  4. HDU 1213 - How Many Tables - [并查集模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...

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

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

  6. hdu 1213 求连通分量(并查集模板题)

    求连通分量 Sample Input2 //T5 3 //n m1 2// u v2 34 5 5 12 5 Sample Output24 # include <iostream> # ...

  7. 【HDOJ】4056 Draw a Mess

    这题用线段树就MLE.思路是逆向思维,然后每染色一段就利用并查集将该段移除,均摊复杂度为O(n*m). /* 4056 */ #include <iostream> #include &l ...

  8. HDU HDU1558 Segment set(并查集+判断线段相交)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...

  9. hdu 1257 小希的迷宫 并查集

    小希的迷宫 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 D ...

随机推荐

  1. Java 文件切割工具类

    Story: 发送MongoDB 管理软件到公司邮箱,工作使用. 1.由于公司邮箱限制附件大小,大文件无法发送,故做此程序用于切割大文件成多个小文件,然后逐个发送. 2.收到小文件之后,再重新组合成原 ...

  2. python 添加 threadpool

    操作系统: Ubuntu 10.04 python安装依赖的软件包: python 出现 ImportError: No module named ** 我这里出现了: ImportError: No ...

  3. 物流管理系统(数据库+后台+springMVC+Mybatis+layui)

    数据库:mysql create database WBG_logistics; use WBG_logistics; #1管理员表 create table admin( a_id int prim ...

  4. django中的forms组件(权限信息校验,增删改查)

    1.用处 1.用户请求数据验证 2.自动生成错误信息 3.打包用户提交的正确信息 4.如果其中有一个错误了,其他的正确,则保留上次输入的内容 5.自动创建input标签并可以设置样式 6.基于form ...

  5. 微信H5单页面滑动的时候如何避免出界,出现头部和底部的黑底?

    ios系统微信浏览器.safari浏览器中h5页面上拉下滑导致悬浮层脱离窗口的解决方法 ios偶现下拉出现黑底时,界面第一次上拉时拉不动的解决方案: document.querySelector('# ...

  6. java使用优先级队列实现哈夫曼编码

    思路: 构建小根堆 根据小根堆实现哈夫曼树 根据哈夫曼树对数据进行编码 代码实现如下: /** * @Author: DaleyZou * @Description: 使用java实现一个哈夫曼编码的 ...

  7. Zeppelin interperter 模式设置总结图解1

    如有错漏,望请指正,不胜感激. 参考:[zeppelin官网]:https://zeppelin.apache.org/docs/latest/interpreter/spark.html#inter ...

  8. LeetCode979. 在二叉树中分配硬币

    问题:979. 在二叉树中分配硬币 给定一个有 N 个结点的二叉树的根结点 root,树中的每个结点上都对应有 node.val 枚硬币,并且总共有 N 枚硬币. 在一次移动中,我们可以选择两个相邻的 ...

  9. python——字符串的操作判断

    s为字符串 s.isalnum()  所有字符都是数字或者字母,为真返回 Ture,否则返回 False. s.isalpha()   所有字符都是字母,为真返回 Ture,否则返回 False. s ...

  10. 1、spring boot入门

    1.Spring Boot 简介 简化Spring应用开发的一个框架: 整个Spring技术栈的一个大整合: J2EE开发的一站式解决方案: 2.微服务 2014,martin fowler 微服务: ...