[HDU 1199] Color the Ball

Color the Ball

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

Total Submission(s): 4529    Accepted Submission(s): 1114 

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

一样的题目:ZOJ 2301[数据较强]
受不了恶心的离散化、、、、、

查询是一次查询、暴力就是了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 20010

struct line
{
    int l,r,c;
}p[N];

int tot;
int x[N];
int vis[N];
int lazy[N<<2];

void pushdown(int rt)
{
    if(lazy[rt]!=-1)
    {
        lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
        lazy[rt]=-1;
    }
}
void build(int l,int r,int rt)
{
    lazy[rt]=-1;
    if(l==r) return;
    int m=(l+r)>>1;
    build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
}
void update(int l,int r,int rt,int L,int R,int c)
{
    if(l==L && R==r)
    {
        lazy[rt]=c;
        return;
    }
    pushdown(rt);
    int m=(l+r)>>1;
    if(R<=m) update(l,m,rt<<1,L,R,c);
    else if(L>m) update(m+1,r,rt<<1|1,L,R,c);
    else
    {
        update(l,m,rt<<1,L,m,c);
        update(m+1,r,rt<<1|1,m+1,R,c);
    }
}
void query(int l,int r,int rt)
{
    if(lazy[rt]==1)
    {
        for(int i=l;i<=r;i++)
            vis[i]=1;
        return;
    }
    if(l>=r) return;
    pushdown(rt);
    int m=(l+r)>>1;
    query(l,m,rt<<1);
    query(m+1,r,rt<<1|1);
}
int main()
{
    int i,j,n;
    while(scanf("%d",&n)!=EOF)
    {
        if(!n)
        {
            cout<<"Oh, my god\n";
            continue;
        }
        tot=0;
        memset(vis,0,sizeof(vis));
        for(i=0;i<n;i++)
        {
            char ch;
            scanf("%d%d %c",&p[i].l,&p[i].r,&ch);
            if(p[i].l>p[i].r) swap(p[i].l,p[i].r);
            p[i].c=ch=='b'?0:1;
            x[tot++]=p[i].l;
            x[tot++]=p[i].r;
        }

        /* 离散化 */
        sort(x,x+tot);
        tot=unique(x,x+tot)-x;
        for(i=tot-1;i>=0;i--)
        {
            if(x[i]-x[i-1]>1) x[tot++]=x[i-1]+1;
            if(x[i]-x[i-1]>2) x[tot++]=x[i]-1;
        }
        sort(x,x+tot);
        build(0,tot-1,1);
        for(i=0;i<n;i++)
        {
            int l=lower_bound(x,x+tot,p[i].l)-x;
            int r=lower_bound(x,x+tot,p[i].r)-x;
            update(0,tot-1,1,l,r,p[i].c);
        }
        query(0,tot-1,1);
        int ans=-1,s=-1,e=-1;
        for(i=0;i<tot;i++)
        {
            if(vis[i])
            {
                j=i;
                while(vis[j+1]) j++;
                if(x[j]-x[i]+1>ans)
                {
                    ans=x[j]-x[i]+1;
                    s=x[i];
                    e=x[j];
                }
            }
        }
        if(s!=-1)
            cout<<s<<' '<<e<<endl;
        else
            cout<<"Oh, my god\n";
    }
    return 0;
}

 

posted @ 2014-12-26 12:34  哈特13  阅读(331)  评论(0编辑  收藏  举报