hdu 1199 Color the Ball (离散化 + 线段树)

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 <stdlib.h>
#include <stdio.h>
#include <algorithm>
#define LL long long
using namespace std;

const int MAX = 5000;
struct date
{
    int l, r;
    int c, b; //c代表颜色,1为白,0为黑,若区间内颜色单一,则b为1
} init[MAX], tree[MAX * 10], res[MAX * 10];// init存输入的数组,tree用来建树,res用来存更新后的每个叶子节点

int num[MAX * 2]; //存所有坐标,用于离散化
int lshnum[MAX * 3]; // 存离散化后的坐标

int k = 0;

void Lsh(){ // 离散化数据,例如样例要离散化成1,2,3,4,5,6,7,8,9,10,11
    int j = 1;
    lshnum[1] = num[1];
    for(int i = 2; i <= k; i++){
        if(num[i] == lshnum[j]){
            continue;
        }
        if(lshnum[j] + 1 < num[i]){
            lshnum[++j] = lshnum[j - 1] + 1;
        }

        if(lshnum[j] + 1 < num[i]){
            lshnum[++j] = num[i] - 1;
        }
        lshnum[++j] = num[i];
    }
    k = j; // 更新k的数量
}

void Build(int l, int r, int rt){
    
    tree[rt].l = l; // 初始化
    tree[rt].r = r;
    tree[rt].c = 0;
    tree[rt].b = 1;
    if(l == r){
        return ;
    }

    int m = (l + r) >> 1;
    Build(l, m, rt << 1);
    Build(m + 1, r, rt << 1 | 1);
}

void PushDown(int rt){
    tree[rt << 1].c = tree[rt << 1 | 1].c = tree[rt].c;
    tree[rt << 1].b = tree[rt << 1 | 1].b = 1;
}

int Le, Ri, c;
void UpdateRange(int l, int r, int rt){
    if(Le <= l && r <= Ri){
        //printf("888\n");
        tree[rt].c = c;
        tree[rt].b = 1;
        return ;
    }
    if(tree[rt].b && tree[rt].c == c){ // 重点,若该区间更新后不变,则可以直接结束
        return ;
    }
    if(tree[rt].b)
        PushDown(rt);
    int m = (l + r) >> 1;
    if(Le <= m){
        UpdateRange(l, m, rt << 1);
    }
    if(Ri > m){
        UpdateRange(m + 1, r, rt << 1 | 1);
    }
    tree[rt].b = 0; // 重点,若该区间部分更新,则颜色不单一
}

int cnt = 0;
void FindNode(int l, int r, int rt){ // 记录每个叶子节点
    if(l == r){
        res[cnt++] = tree[rt];
        return ;
    }
    int m = (l + r) >> 1;
    FindNode(l, m, rt << 1);
    FindNode(m + 1, r, rt << 1 | 1);
}

void PushAllDown(int l, int r, int rt){ //下推所有标记
    if(l == r){
        return ;
    }
    if(tree[rt].b){
        PushDown(rt);
    }
    int m = (l + r) >> 1;
    PushAllDown(l, m, rt << 1);
    PushAllDown(m + 1, r, rt << 1 | 1);
}



int main(int argc, char const *argv[]){
    int n;
    while(~scanf("%d", &n)){
        cnt = 0;
        k = 0;
        for(int i = 0; i < n; i++){
            int l, r;
            char c;
            scanf("%d%d %c", &l, &r, &c);
            num[++k] = l;
            num[++k] = r;
            init[i].l = l;
            init[i].r = r;
            if(c == 'w'){ //白色为1
                init[i].c = 1;
            } else{
                init[i].c = 0;
            }
        }

        sort(num + 1, num + 1 + k);

        Lsh();
        Build(1, k, 1);
        for(int i = 0; i < n; i++){
            Le = lower_bound(lshnum + 1, lshnum + k + 1, init[i].l) - lshnum;
            Ri = lower_bound(lshnum + 1, lshnum + k + 1, init[i].r) - lshnum;
            c = init[i].c;
            UpdateRange(1, k, 1); 
        }
        
        PushAllDown(1, k, 1);
        FindNode(1, k, 1);

        int len = 0;
        int l, r;
        for(int i = 0; i < cnt; i++){
            if(!res[i].c){
                continue;
            }
            int j = i;
            
            while(i + 1 < cnt && res[i + 1].c){ // 找最长的白色区间
                i++;
            }
            if(i + 1 <= cnt && len < lshnum[res[i].r] - lshnum[res[j].l] + 1){
                len = lshnum[res[i].r] - lshnum[res[j].l] + 1;
                l = lshnum[res[j].l];
                r = lshnum[res[i].r];
            }
        }
        if(len == 0){
            printf("Oh, my god\n");
        } else{
            printf("%d %d\n", l, r);
        }
    }

    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值