线段树 [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

Author

ZHOU, Kai

Source

ZOJ Monthly, February 2005

一样的题目: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;
}
时间: 2024-10-31 19:49:00

线段树 [HDU 1199] Color the Ball的相关文章

ZOJ 2301 / HDU 1199 Color the Ball 离散化+线段树区间连续最大和

题意:给你n个球排成一行,初始都为黑色,现在给一些操作(L,R,color),给[L,R]区间内的求染上颜色color,'w'为白,'b'为黑.问最后最长的白色区间的起点和终点的位置. 解法:先离散化,为了防止离散后错误,不仅将L,R离散,还要加入L+1,L-1,R+1,R-1一起离散,这样就绝不会有问题了.然后建线段树,线段树维护四个值: 1.col  区间颜色  0 表示黑  1 表示白  -1表示无标记 2.maxi 区间内最大白区间的长度,由于白色用1表示,所以最大白区间的长度即为区间最

hdu 1199 Color the Ball 离散线段树

C - Color the Ball Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status 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 t

HDU 1199 Color the Ball

Color the Ball Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 119964-bit integer IO format: %I64d      Java class name: Main There are infinite balls in a line (numbered 1 2 3 ....), and initially all of the

HDU 1199 - Color the Ball 离散化

[题意]现在有几个球排成一排,编号从1开始,开始时所有球为黑色,现在有n(<=2000)次操作,每次操作将l[i]至r[i](均在int范围)的球凃成颜色c[i](黑色'b'或白色'w'),然后找到最长的连续白色球,输出左右端点符号 [离散化]因为l[i]和r[i]都在int范围,显然不不可以开一个2^31-1那么大的数组.将l[i]和r[i]+1离散化,再模拟染色即可. 如果你不知道离散化: 将l[i]数组所有数与r[i]+1数组所有数取出来从小到大排序,做一个映射. 如样例 3 1 4 w

HDU 1556 Color the ball 线段树

HDU 1556 Color the ball 线段树模版题,存个模板 1 #include <iostream> 2 #include <cstdio> 3 #include <fstream> 4 #include <algorithm> 5 #include <cmath> 6 #include <deque> 7 #include <vector> 8 #include <queue> 9 #inclu

hdu 1556:Color the ball(线段树,区间更新,经典题)

Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7941    Accepted Submission(s): 4070 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电

【线段树】hdu 1556 Color the ball

[线段树]hdu 1556 Color the ball 题目链接:hdu 1556 Color the ball 题目大意 给你N个气球,不断刷新指定区间的颜色,刷新N次,最后输出每一个气球的刷新次数. 上一篇文章是线段树的点修改.区间查询: 这篇文章是线段树的区间修改.点查询: 说一下思路 线段树的区间修改:利用线段树的区间查询,查询到叶节点segTree[root].sum++,而如果对区间进行多次点修改的话,注定超时 线段树的点查询:以为用之前的区间查询就可以了,实际上还是有改变,因为原

HDU 1556 Color the ball 线段树更新区间查点

点击打开链接 Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9120    Accepted Submission(s): 4665 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽

hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)

Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8984    Accepted Submission(s): 4594 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球