zoj 2301 Color the Ball(区间染色,线段树+离散化)

Color the Ball

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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


题意:一开始全部点为黑色,然后给n个区间染色,染色为白色或黑色,最后统计哪段区间白色最长。

思路:线段树+离散化。


AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
using namespace std;

const int maxn = 50005;
struct node
{
    int l, r, co;
} tree[4 * maxn];
struct node1
{
    int x, y, co;
} line[maxn];
int n, cnt, tot;
int num[maxn], hash[maxn], color[maxn];
int find(int x)
{
    int l = 1, r = cnt;
    while(l <= r)
    {
        int mid = (l + r) >> 1;
        if(hash[mid] == x) return mid;
        else if(hash[mid] < x) l = mid + 1;
        else r = mid - 1;
    }
    return -1;
}
void build(int l, int r, int rt)
{
    tree[rt].l = l, tree[rt].r = r, tree[rt].co = 1;
    if(l == r) return;
    int mid = (l + r) >> 1;
    build(l, mid, L(rt));
    build(mid + 1, r, R(rt));
}
void update(int l, int r, int rt, int val)
{
    if(tree[rt].l == l && tree[rt].r == r)
    {
        tree[rt].co = val;
        return;
    }
    if(tree[rt].co == val) return;      //这句不加会导致Segmentation Fault
    if(tree[rt].co != -1)
    {
        tree[L(rt)].co = tree[R(rt)].co = tree[rt].co;
        tree[rt].co = -1;
    }
    if(r <= tree[L(rt)].r) update(l, r, L(rt), val);
    else if(l >= tree[R(rt)].l) update(l, r, R(rt), val);
    else update(l, tree[L(rt)].r, L(rt), val), update(tree[R(rt)].l, r, R(rt), val);
}
void query(int l, int r, int rt)
{
    if(tree[rt].co != -1)
    {
        for(int i = tree[rt].l; i <= tree[rt].r; i++)
            color[i] = tree[rt].co;
        return;
    }
    if(r <= tree[L(rt)].r) query(l, r, L(rt));
    else if(l >= tree[R(rt)].l) query(l, r, R(rt));
    else query(l, tree[L(rt)].r, L(rt)), query(tree[R(rt)].l, r, R(rt));
}
int main()
{
    char s[5];
    while(~scanf("%d", &n))
    {
        tot = 0;
        memset(color, 1, sizeof(color));
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d%s", &line[i].x, &line[i].y, s);
            line[i].co = s[0] == 'b' ? 1 : 0;
            num[tot++] = line[i].x;
            num[tot++] = line[i].y;
        }
        sort(num, num + tot);
        int t = tot;
        for(int i = 1; i < t; i++)
        {
            if(num[i] - num[i - 1] > 1) num[tot++] = num[i - 1] + 1;
            if(num[i] - num[i - 1] > 2) num[tot++] = num[i] - 1;
        }
        sort(num, num + tot);
        cnt = 0;
        hash[++cnt] = num[0];
        for(int i = 1; i < tot; i++)
            if(num[i] != num[i - 1]) hash[++cnt] = num[i];
        build(1, maxn, 1);
        for(int i = 0; i < n; i++)
        {
            int a = find(line[i].x), b = find(line[i].y);
            update(a, b, 1, line[i].co);
        }
        query(1, maxn, 1);
        int s, e, ans = 0;
        int i = 1, j;
        while(i <= cnt)
        {
            if(color[i] == 0)
            {
                j = i;
                while(color[j] == 0 && j <= cnt) j++;
                int t = hash[j - 1] - hash[i] + 1;
                if(t > ans)
                {
                    ans = t;
                    s = hash[i];
                    e = hash[j - 1];
                }
                i = j;
            }
            else i++;
        }
        if(!ans) printf("Oh, my god\n");
        else printf("%d %d\n", s, e);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值