HDU 1199-Color the Ball

来源:互联网 发布:手机淘宝无线端链接 编辑:程序博客网 时间:2024/05/16 12:39

染色问题,很明显的线段树~~

不同的是长度没有规定,要先存储修改范围,再取最大值,从而确定范围。

依然是线段树的模版。。。有点lazy思想

悲剧的是我hdu过了,zoj2301一样的题却一直提示segment fault...改了半天也没改好,放弃了

媛姐的代码貌似很强大,不过我没看懂。。。o(╯□╰)o

代码如下:

#include<iostream>#include<cstdio>#include<cstring>#define MAXN 200010#define L(x) (x << 1)#define R(x) (x << 1 | 1)using namespace std;int N, maxy, len, leni, lenj;typedef struct{int l, r;int col;}Node;Node node[MAXN * 4];int color[MAXN];typedef struct{int x, y;bool col;}Data;Data data[MAXN];char s[2];void init(){maxy = 0;len = 0;memset(node, 0, sizeof(node));memset(color, 0, sizeof(color));}void Build(int t, int l, int r){if(l >= r)return;node[t].l = l;node[t].r = r;node[t].col = -1;if(node[t].r - node[t].l == 1){node[t].col = 0;return;}int mid = (node[t].l + node[t].r) >> 1;Build(L(t), l, mid);Build(R(t), mid, r);}void Updata(int t, int l, int r, int d){if(l >= r)return;if(node[t].l >= l && node[t].r <= r){node[t].col = d;return;}if(node[t].r - node[t].l == 1)return;if(node[t].col != -1){node[L(t)].col = node[R(t)].col = node[t].col;node[t].col = -1;}int mid = (node[t].l + node[t].r) >> 1;if(l >= mid)Updata(R(t), l, r, d);else if(r <= mid)Updata(L(t), l, r, d);else{Updata(L(t), l, mid, d);Updata(R(t), mid, r, d);}}void Query(int t){if(node[t].col >= 0){for(int i = node[t].l; i < node[t].r; i++){color[i] = node[t].col;}return;}Query(L(t));Query(R(t));}int main(){int i, j;while(scanf("%d", &N) != EOF){init();for(i = 0; i < N; i++){scanf("%d%d%s", &data[i].x, &data[i].y, s);if(data[i].y > maxy)maxy = data[i].y;if(s[0] == 'w')data[i].col = true;else data[i].col = false;}Build(1, 0, maxy);for(i = 0; i < N; i++){Updata(1, data[i].x-1, data[i].y, data[i].col);}Query(1);for(i = 0; i < maxy; i++){if(color[i]){for(j = 1;;j++){if(color[i+j])continue;else{if(len < j){len = j;leni = i+1;lenj = i+j;}i += j;break;}}}}if(len)printf("%d %d\n", leni, lenj);elseprintf("Oh, my god\n");}return 0;}

媛姐的代码:http://blog.csdn.net/zxy_snow/article/details/6639878


原创粉丝点击