ural 1019. Line Painting

http://acm.timus.ru/problem.aspx?space=1&num=1019

呃,搜线段树题呢,瞟到URAL这个了。很水啊,还是染色问题,但是没好好看题,The segment of numerical axis from 0 to 10^9 is painted into white color.这个需要把0到10^9都先染成白色的= = 。。。我木有注意 = =。。昨晚回寝室后精神也不太好,各种BUG。刚才稍微调了下就过了= =。。。

数字很大,所以采用离散化。(如果是比赛的话,我肯定这题不用线段树!!!直接离散化就能搞定的 = =)。。

注意加边界,其他没什么了。还有统计最长线段的时候,扫描下,如果一直是一个颜色,就一直扫下去~

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <climits>
#define MID(x,y) ((x+y)>>1)
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )

using namespace std;

const int MAX = 10010;
int x[MAX];
struct Tnode{int l,r;int col;};
struct NODE{int x,y;bool col;};
NODE nn[MAX];
Tnode node[MAX*4];
int col[MAX];
void init()
{
	for(int i=0; i<MAX; i++)
		col[i] = 1;
	memset(nn,0,sizeof(nn));
	memset(node,0,sizeof(node));
}

void Build(int t,int l,int r)
{
	node[t].l = l; node[t].r = r;
	node[t].col = 1;
	if( l == r - 1 ) return ;
	int mid = MID(l,r);
	Build(L(t),l,mid);
	Build(R(t),mid,r);
}

void Updata(int t,int l,int r,int col)
{
	if( node[t].l == l && node[t].r == r )
	{
		node[t].col = col;
		return ;
	}
	if( node[t].col >= 0 && node[t].col != col )
	{
		node[R(t)].col = node[L(t)].col = node[t].col;
		node[t].col = -1;
	}
	int mid = MID(node[t].l,node[t].r);
	if( l >= mid )
		Updata(R(t),l,r,col);
	else
		if( r <= mid )
			Updata(L(t),l,r,col);
		else
		{
			Updata(L(t),l,mid,col);
			Updata(R(t),mid,r,col);
		}
}

void Query(int t,int l,int r)
{
	if( node[t].col >= 0 )
	{
		for(int i=node[t].l; i<node[t].r; i++)
			col[i] = node[t].col;
		return ;
	}
	int mid = MID(node[t].l,node[t].r);
	if( l >= mid )
		Query(R(t),l,r);
	else
		if( r <= mid )
			Query(L(t),l,r);
		else
		{
			Query(L(t),l,mid);
			Query(R(t),mid,r);
		}
}
void solve(int *x,int cnt,int n)
{
	Build(1,0,cnt);
	for(int i=0; i<n; i++)
	{
		int xx = lower_bound(x,x+cnt,nn[i].x) - x;
		int yy = lower_bound(x,x+cnt,nn[i].y) - x;
		Updata(1,xx,yy,nn[i].col);	
	}
	Query(1,0,cnt);
	int s = 0,e = 0,te,ts;
	int mmax = 0;
	col[cnt] = 0;
	x[cnt] = 1000000000;
	for(int i=0; i<cnt; i++)
	{
		ts = x[i];
		while( col[i] == 1 )
			i++;
		te = x[i];
		if( te - ts > e - s )
		{
			e = te;
			s = ts;
		}
	}
	printf("%d %d\n",s,e);
}

int main()
{
	int n;
	char s[5];
	while( ~scanf("%d",&n) )
	{
		int cnt = 0;
		init();
		nn[0].x = 0; nn[0].y = 1000000000;
		nn[0].col = 1;
		x[cnt++] = nn[0].x; x[cnt++] = nn[0].y;
		for(int i=1; i<=n; i++)
		{
			scanf("%d%d%s",&nn[i].x,&nn[i].y,s);
			x[cnt++] = nn[i].x;
			x[cnt++] = nn[i].y;
			if( s[0] == 'w' )
				nn[i].col = 1;
		}
		sort(x,x+cnt);
		cnt = unique(x,x+cnt) - x;
		solve(x,cnt,n+1);
	}
return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值