前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDU 1199 Color the Ball

HDU 1199 Color the Ball

作者头像
ShenduCC
发布2018-04-27 11:00:40
8060
发布2018-04-27 11:00:40
举报
文章被收录于专栏:算法修养算法修养算法修养

Color the Ball

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 5631    Accepted Submission(s): 1395

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线段树区间覆盖的问题,问题是数字范围太大,所以要离散化。当然也可以不用离散化作,即动态开点。这道题目的测试数据比较弱,所以我用了动态开点,要是数据强一点,可能会内存超限,#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>

using namespace std;
typedef long long int LL;
const int maxn=2*1e3;
LL mmax[maxn*35];
LL lmax[maxn*35];
LL rmax[maxn*35];
int c[maxn*35];
int l[maxn*35];
int r[maxn*35];
LL ll[maxn*35];
LL rr[maxn*35];
int n;
int p;
int newnode()
{
    l[p]=r[p]=-1;
    lmax[p]=rmax[p]=mmax[p]=0;
    ll[p]=rr[p]=-1;
    return p++;
}
void pushdown(int node,LL L,LL R)
{
    if(!(L==R))
    {
          if(l[node]==-1) l[node]=newnode();
        if(r[node]==-1) r[node]=newnode();
    }
    if(c[node]!=-1)
    {
        if(c[node])
        {
            lmax[node]=rmax[node]=mmax[node]=R-L+1;
            ll[node]=L,rr[node]=R;
        }
        else
        {
            lmax[node]=rmax[node]=mmax[node]=0;
            ll[node]=rr[node]=-1;
        }

        c[l[node]]=c[r[node]]=c[node];
        c[node]=-1;
    }
}
void pushup(int node,LL L,LL R)
{
    LL mid=(L+R)>>1;
    
    pushdown(l[node],L,mid);
    pushdown(r[node],mid+1,R);
    if(lmax[l[node]]==(mid-L+1))
        lmax[node]=lmax[l[node]]+lmax[r[node]];
    else
        lmax[node]=lmax[l[node]];
    if(rmax[r[node]]==(R-mid))
        rmax[node]=rmax[r[node]]+rmax[l[node]];
    else
        rmax[node]=rmax[r[node]];
    mmax[node]=max(lmax[r[node]]+rmax[l[node]],max(mmax[l[node]],mmax[r[node]]));
    if(mmax[node]==mmax[l[node]])
        ll[node]=ll[l[node]],rr[node]=rr[l[node]];
    else if(mmax[node]==lmax[r[node]]+rmax[l[node]])
        ll[node]=mid-rmax[l[node]]+1,rr[node]=mid+lmax[r[node]];
    else
        ll[node]=ll[r[node]],rr[node]=rr[r[node]];

}

void update(int node,LL begin,LL end,LL L,LL R,int tag)
{

    if(L<=begin&&end<=R)
    {
        c[node]=tag;
        if(l[node]==-1) l[node]=newnode();
        if(r[node]==-1) r[node]=newnode();
        pushdown(node,begin,end);
        return;
    }
    if(l[node]==-1) l[node]=newnode();
    if(r[node]==-1) r[node]=newnode();
    pushdown(node,begin,end);
    int mid=(begin+end)>>1;

    if(L<=mid) update(l[node],begin,mid,L,R,tag);
    if(R>mid) update(r[node],mid+1,end,L,R,tag);
    pushup(node,begin,end);
}
int main()
{

    while(scanf("%d",&n)!=EOF)
    {
        p=0;
        int root=newnode();
        LL a,b;
        char cc;
        LL len=(1<<31)-1;
        int tag;
        update(root,1,len,1,len,0);
        memset(c,-1,sizeof(c));
      for(int i=1;i<=n;i++)
        {
            scanf("%lld%lld %c",&a,&b,&cc);
            if(cc=='w') tag=1;
            else tag=0;
            update(root,1,len,a,b,tag);
        }
        if(ll[root]==-1)
            printf("Oh, my god\n");
        else
            printf("%lld %lld\n",ll[root],rr[root]);
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-10-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Color the Ball
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档