方法参见: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. ibator自动代码生成

    首先,强烈推荐一篇文章,介绍的特详细 http://www.iteye.com/topic/821983 1. 插件安装 http://blog.csdn.net/rchm8519/article/d ...

  2. 阅读list

    最近感觉效率不高,其实有很多事情要做的,读书的速度也慢下来了,要抓紧时间的了. 继续读deep learning 一书的part II. 读完jifeng dai的几篇文章,去年欠下的债务啊.其中包括 ...

  3. react(一):组件的生命周期

    最近兄弟团队让我去帮忙优化两个页面,前端用的react全家桶,后端用的python,上一次写react代码都过去一年了,顺着以前的的学习思路,再捋顺一下react的要点 组件的生命周期就是Reac的工 ...

  4. vue学习之路 - 0.背景

    1 单页面应用程序 Single Page Application (SPA) 从字面意义来看就是一个网站就一个页面,如: coding 网易云音乐 极致的用户体验,就像nativeapp一样 优点: ...

  5. 关于discuz 不能全文搜索的问题

    这个问题客服反馈很多次了,以为discuz 默认搜索只能搜标题,除非配置了sphinx全文搜索引擎. 但是之前比较老的员工说以前能用的,也就是discuz老版本. 今天突然想到是不是discuz纵横搜 ...

  6. 注释java中某个方法过时

    添加一个注解即可 @Deprecated

  7. 交换机基础配置之单交换机划分vlan

    我们以以上拓扑图为例 pc0的IP地址为:192.168.1.1 pc1的ip地址为:192.168.1.2 两台主机在同一网段,相互ping是能ping通的 我们的目的是在单交换机上划分两个vlan ...

  8. python__基础 : sys模块: sys.argv与sys.path

    sys模块中的 argv 保存的是当你运行一个py文件的时候给他传递进去的参数,如: import sys a = sys.argv print(a) # 当在命令行中调用这个py文件: > p ...

  9. Java源码解析——Java IO包

    一.基础知识: 1. Java IO一般包含两个部分:1)java.io包中阻塞型IO:2)java.nio包中的非阻塞型IO,通常称为New IO.这里只考虑到java.io包中堵塞型IO: 2. ...

  10. MySQL优化总结-查询总条数

    1.COUNT(*)和COUNT(COL) COUNT(*)通常是对主键进行索引扫描,而COUNT(COL)就不一定了,另外前者是统计表中的所有符合的纪录总数,而后者是计算表中所有符合的COL的纪录数 ...