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 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 <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)

const int inf=0x7fffffff;   //无限大
char op[3];
struct interval
{
    int start,endn;
    bool operator < (const interval& b)const{
        if(start!=b.start)
            return start<b.start;
        else
            return endn<b.endn;
    }
};
interval val[20001];
void white(int a,int b,int& cnt)
{
    val[cnt].start=a;
    val[cnt].endn=b;
    cnt++;
}
void black(int a,int b,int& cnt)
{
    int tmp=cnt;
    for(int i=0;i<cnt;i++)
    {
        if(val[i].start<a)
        {
            if(val[i].endn>=a)
            {
                if(val[i].endn<=b)
                {
                    val[i].endn=a-1;
                }
                else
                {
                    val[tmp].start=b+1;
                    val[tmp].endn=val[i].endn;
                    tmp++;
                    val[i].endn=a-1;
                }
            }
        }
        else if(val[i].start<=b)
        {
            if(val[i].endn<=b)
            {
                val[i].start=0;
                val[i].endn=-1;
            }
            else
            {
                val[i].start=b+1;
            }
        }
    }
    cnt=tmp;
}
int solve(int cnt,int& index)
{
    sort(val,val+cnt);
    int maxn=val[0].endn-val[0].start+1;
    for(int i=1;i<cnt;i++)
    {
        if(val[i].start!=0)
        {
            if(val[i].start<=val[i-1].endn+1)
            {
                if(val[i-1].endn<=val[i].endn)
                {
                    val[i].start=val[i-1].start;
                }
                else
                {
                    val[i].start=val[i-1].start;
                    val[i].endn=val[i-1].endn;
                }
            }
            if(val[i].endn-val[i].start+1>maxn)
            {
                maxn=val[i].endn-val[i].start+1;
                index=i;
            }
        }
    }
    return maxn;
}
int main()
{
    int n,index,a,b,c;
    while(cin>>n)
    {
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%s",&a,&b,op);
            if(a>b)
            {
                swap(a,b);
            }
            if(op[0]==‘w‘)
                white(a,b,cnt);
            else
                black(a,b,cnt);
        }
        index=0;
        if(solve(cnt,index))
        {
            cout<<val[index].start<<" "<<val[index].endn<<endl;
        }
        else
            cout<<"Oh, my god"<<endl;
    }
    return 0;
}
时间: 2024-08-27 10:50:52

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 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(线段树:区间更新)

http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意: N个气球,每次[a,b]之间的气球涂一次色,统计每个气球涂色的次数. 思路: 这道题目用树状数组和线段树都可以,拿这道题来入门一下线段树的区间更新. 1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 6 const int maxn = 1000

HDU - 1556 Color the ball(线段树和树状数组)

题意:n个气球,n个操作(每次给区间[a,b]的气球涂颜色),求最后每个气球被涂颜色的次数. n可以取到100000,所以如果想for,for的话,就 爆!爆!爆! 1.线段树做法: 一道简单的线段树,做了好久都没做出来,果然自己还是太菜了... 创建一棵线段树,把里面的涂色次数对应的值都初始化为0. 更新的时候,找到那个要涂色的区间,涂色次数+1. 询问的时候从上往下找到那个区间,每次把值都递归出来. 1 #define LC(a) ((a<<1)+1) 2 #define RC(a) ((

HDOJ1556 Color the ball 【线段树】+【树状数组】+【标记法】

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

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

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

hdoj 1556 Color the ball【线段树区间更新】

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

hdu 1556 Color the ball (扫描线+树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14237    Accepted Submission(s): 7120 Problem Description N个气球排成一排,从左到右依次编号为1,