zoj 2301 || hdu 1199 Color the Ball

80 篇文章 0 订阅

= = 本来刚A掉URAL类似的,搜题解看看人家咋做的,然后看到有人提起HDU1199 = =。。。看起来似乎一样,就改了改。

结果还是需要认真读题!!!

这个染色是闭合区间的,而我上个代码是开区间的,这个很成问题。还有,如果没有染色的话,我的那种建树它会报错的!!结果HDU数据弱,ZOJ一直SF。。。

后来改了下,我之前的代码输入0的时候,它会报错的,内存神马的,在建树里加了一句,if( l >= r ) return ;这样就好了。 = = 。。。教训啊。。


#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 = 4010;
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()
{
    memset(col,0,sizeof(col));
    memset(nn,0,sizeof(nn));
    memset(node,0,sizeof(node));
}

void Build(int t,int l,int r)
{
	if( l >= r ) return ;
    node[t].l = l; node[t].r = r;
    node[t].col = 0;
    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( l >= r ) return ;
    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( l >= r ) return ;
    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;
    x[cnt] = 0; x[cnt] = x[cnt-1];
    for(int i=0; i<cnt; i++)
    {
        if( col[i] != 1 ) continue;
        ts = x[i];
        while( col[i] == 1 )
            i++;
        if( i > cnt ) break;
        te = x[i];
        if( te - ts > e - s )
        {
            e = te;
            s = ts;
        }
    }
    if( s == e )
        printf("Oh, my god\n");
    else
        printf("%d %d\n",s,e-1);
}

int main()
{
    int n;
    char s[5];
    while( ~scanf("%d",&n) )
    {
        init();
        int cnt = 0;
        for(int i=0; i<n; i++)
        {
            scanf("%d%d%s",&nn[i].x,&nn[i].y,s);
            nn[i].y++;
            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);
    }
return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值