hdu 1199 Color the Ball(离散化线段树)

There are infinite balls in a line (numbered 1 2 3 ....), and initially all of them are paint black. Now Jim use a brush paint the balls, every time give two integers a b and follow by a char 'w' or 'b', 'w' denotes the ball from a to b are painted white, 'b' denotes that be painted black. You are ask to find the longest white ball sequence.
 

Input

First line is an integer N (<=2000), the times Jim paint, next N line contain a b c, c can be 'w' and 'b'.

There are multiple cases, process to the end of file.

Output

Two integers the left end of the longest white ball sequence and the right end of longest white ball sequence (If more than one output the small number one). All the input are less than 2^31-1. If no such sequence exists, output "Oh, my god".

emmmm。。。。看大佬代码敲的。。。由于大佬有版权声明。。。就不给大佬的链接了。。。。

#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#define LL long long
using namespace std;

const int MAX = 5000;

struct date
{
	int l, r;
	int c, b;	
}Sum[MAX * 10], init[MAX], d[MAX * 10]; //Sum用来建线段树,init用来存输入数据,d用来获得最后的所有叶子节点,数组不能值开4倍,因为最大会有n * 2 * 3 * 4个数,但开10倍也过了。。
int k = 0;
int lshArr[MAX * 3]; //存离散化
int num[MAX];

void Lsh(){ //离散化,要把每个值相邻的值也加入数组
	int j = 1;
	lshArr[1] = num[1];
	for(int i = 2; i <= k; i++){
		if(num[i] == lshArr[j]){
			continue;
		}
		if(lshArr[j] + 1 < num[i]){
			lshArr[++j] = lshArr[j - 1] + 1;
		}

		if(lshArr[j] + 1 < num[i]){
			lshArr[++j] = num[i] - 1;
		}
		lshArr[++j] = num[i];
	}
	k = j;
}

void Build(int l, int r, int rt){
	Sum[rt].l = l;
	Sum[rt].r = r;
	Sum[rt].b = 1;
	Sum[rt].c = 0;
	if(l == r){
		return ;
	}

	int m = (l + r) >> 1;
	Build(l, m, rt << 1);
	Build(m + 1, r, rt << 1 | 1);
}

void PushDown(int rt){
	Sum[rt << 1].b = Sum[rt << 1 | 1].b = 1;
	Sum[rt << 1].c = Sum[rt << 1 | 1].c = Sum[rt].c;
}

void UpdateRange(int L, int R, int c, int l, int r, int rt){
	if(L <= l && r <= R){
		Sum[rt].c = c;
		Sum[rt].b = 1;
		return ;
	}

	if(Sum[rt].b && Sum[rt].c == c){
		return ;
	}

	if(Sum[rt].b){
		PushDown(rt);
	}
	int m = (r + l) >> 1;

	if(R <= m){
		UpdateRange(L, R, c, l, m, rt << 1);
	} else if(L > m){
		UpdateRange(L, R, c, m + 1, r, rt << 1 | 1);
	} else{
		UpdateRange(L, R, c, l, m, rt << 1);
		UpdateRange(L, R, c, m + 1, r, rt << 1 | 1);
	}
	
	Sum[rt].b = 0; // 重点!!!!!!!!!!!!!
}

void PushAllDown(int l, int r, int rt){	
	if(l == r){
		return ;
	}
	if(Sum[rt].b){
		PushDown(rt);
	}
	int m = (l + r) >> 1;
	PushAllDown(l, m, rt << 1);
	PushAllDown(m + 1, r, rt << 1 | 1);
}

int cnt = 0;
void findlshArr(int l, int r, int rt){
	if(l == r){
		d[cnt++] = Sum[rt];
		return ;
	}

	int m = (l + r) >> 1;
	findlshArr(l, m, rt << 1);
	findlshArr(m + 1, r, rt << 1 | 1);
}

int main(int argc, char const *argv[])
{
	/*freopen("in.txt","r",stdin); //重定向一定要关掉!!!!!!!
	freopen("out.txt","w",stdout);*/
	int n;
	while(~scanf("%d", &n)){
		cnt = 0;
		k = 0;
		for(int i = 1; i <= n; i++){
			int l, r;
			char c;
			scanf("%d%d %c", &l, &r, &c);
			init[i].l = l;
			init[i].r = r;
			if(c == 'w'){
				init[i].c = 1;
			} else{
				init[i].c = 0;
			}
			num[++k] = l;
			num[++k] = r;
		}

		sort(num + 1, num + k + 1);
		Lsh(); 

		Build(1, k, 1);

		for(int i = 1; i <= n; i++){
			int l = lower_bound(lshArr + 1, lshArr + k + 1, init[i].l) - lshArr;
			int r = lower_bound(lshArr + 1, lshArr + k + 1, init[i].r) - lshArr;
			//printf("l = %d r = %d\n", l, r);
			UpdateRange(l, r, init[i].c, 1, k, 1);
		}

		PushAllDown(1, k, 1);
		findlshArr(1, k, 1);
		int len = 0;
		int L, R;
		//printf("cnt = %d\n", cnt);
		/*for (int i = 0; i < cnt; ++i)
		{
			printf("%d \n", d[i].c);
		}*/
		//printf("88\n");
		for(int i = 0; i < cnt; i++){
			if(!d[i].c){
				continue ;
			}
			int j = i;
			while(d[i + 1].c != 0 && i + 1 < cnt){
				i++;
			}
			if(i + 1 == cnt && len < lshArr[d[i].r] - lshArr[d[j].l] + 1){
				//printf("88\n");
				len = lshArr[d[i].r] - lshArr[d[j].l] + 1;
				L = lshArr[d[j].l];
				R = lshArr[d[i].r];
			}

			if(i + 1 < cnt && len < lshArr[d[i].r] - lshArr[d[j].l] + 1){
				len = lshArr[d[i].r] - lshArr[d[j].l] + 1;
				L = lshArr[d[j].l];
				R = lshArr[d[i].r];
			}
		}

		if(len == 0){
			printf("Oh, my god\n");
		} else{
			printf("%d %d\n", L, R);
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43737952/article/details/89224557
今日推荐