HDU1199-Color the Ball

Color the Ball

                                                                        Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                                                                                                Total Submission(s): 6032    Accepted Submission(s): 1507


Problem Description
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".
 

Sample Input
  
  
3 1 4 w 8 11 w 3 5 b
 

Sample Output
  
  
8 11
 

Author
ZHOU, Kai
 

Source
 

Recommend
Ignatius.L
 



题意:有若干个球以及n次操作,每次操作把区间 [ a , b ] 的球涂成一种颜色,w 表示涂成白色,b 表示涂成黑色。初始的时候所有的球都是黑色的。求出n次操作以后最长连续白色的区间,如果有多个,输出最左边的那个

解题思路:线段树+离散化(注意离散化时需要将一个点的左右两个点也加进去)


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int lr[4 * 12007], rl[4 * 12007], ma[4 * 12007], lazy[4 * 12007];
int xx[12007], n, cnt;
int L[2005], R[2005];
char ch[2005][3];
map<int, int>mp;

void Merge(int k, int l, int r)
{
	lr[k] = lr[k << 1], rl[k] = rl[k << 1 | 1];
	ma[k] = max(ma[k << 1], ma[k << 1 | 1]);
	ma[k] = max(ma[k], rl[k << 1] + lr[k << 1 | 1]);
	int mid = (l + r) / 2;
	int L = xx[mid] - xx[l - 1];
	int R = xx[r] - xx[mid];
	if (lr[k << 1] == L) lr[k] += lr[k << 1 | 1];
	if (rl[k << 1 | 1] == R) rl[k] += rl[k << 1];
}

void Push(int k, int l, int r)
{
	lazy[k << 1] = lazy[k << 1 | 1] = lazy[k];
	int mid = (l + r) / 2;
	int L = xx[mid] - xx[l - 1];
	int R = xx[r] - xx[mid];
	ma[k << 1] = lr[k << 1] = rl[k << 1] = L*lazy[k];
	ma[k << 1 | 1] = lr[k << 1 | 1] = rl[k << 1 | 1] = R*lazy[k];
	lazy[k] = -1;
}

void update(int k, int l, int r, int ll, int rr, int flag)
{
	if (ll <= l && rr >= r)
	{
		lazy[k] = flag;
		ma[k] = lr[k] = rl[k] = flag * (xx[r] - xx[l - 1]);
		return;
	}
	if (lazy[k] != -1) Push(k, l, r);
	int mid = (l + r) / 2;
	if (ll <= mid) update(k << 1, l, mid, ll, rr, flag);
	if (rr > mid) update(k << 1 | 1, mid + 1, r, ll, rr, flag);
	Merge(k, l, r);
}

int query(int k, int l, int r)
{
	int mid = (l + r) / 2;
	if (lazy[k] != -1) Push(k, l, r);
	if (ma[k << 1] == ma[1]) return query(k << 1, l, mid);
	else if (rl[k << 1] + lr[k << 1 | 1] == ma[1]) return xx[mid] - rl[k << 1] + 1;
	else return query(k << 1 | 1, mid + 1, r);
}

int main()
{
	while (~scanf("%d", &n))
	{
		mp.clear();
		cnt = 1;
		for (int i = 1; i <= n; i++)
		{
			scanf("%d%d%s", &L[i], &R[i], ch[i]);
			xx[cnt++] = L[i] - 1, xx[cnt++] = L[i], xx[cnt++] = L[i] + 1;
			xx[cnt++] = R[i] - 1, xx[cnt++] = R[i], xx[cnt++] = R[i] + 1;
		}
		sort(xx + 1, xx + cnt);
		cnt = unique(xx + 1, xx + cnt) - xx;
		memset(ma, 0, sizeof ma);
		memset(lr, 0, sizeof lr);
		memset(rl, 0, sizeof rl);
		memset(lazy, -1, sizeof lazy);
		for (int i = 1; i < cnt; i++) mp[xx[i]] = i;
		for (int i = 1; i <= n; i++)
		{
			int a = mp[L[i]], b = mp[R[i]];
			if (ch[i][0] == 'w') update(1, 1, cnt - 1, a, b, 1);
			else update(1, 1, cnt - 1, a, b, 0);
		}
		if (ma[1] <= 0) puts("Oh, my god");
		else
		{
			int l = query(1, 1, cnt - 1);
			printf("%d %d\n", l, l+ma[1]-1);
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值