ural 1019. Line Painting 线段树 离散化

题目链接:

http://acm.timus.ru/problem.aspx?space=1&num=1019

题意:

初始0~1e9都染成白色,[l,r]染成w/b, 是开区间。
样例:
4
1 999999997 b
40 300 w
300 634 w
43 47 b
输出 47 634

题解:

类比上一篇博客,不同的是开区间
还是那组样例
2
0 3 b
4 999999999 b
输出 3 4

所以不用将r延伸一个单位
注意边界

数字很大,所以采用离散化。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MS(a) memset(a,0,sizeof(a))
#define MP make_pair
#define PB push_back
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
inline ll read(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//////////////////////////////////////////////////////////////////////////
const int maxn = 20010+10;

int x[maxn],y[maxn],c[maxn],tmp[2*maxn],vis[maxn];
int cnt;
char ch;

struct node{
    int l,r,col,lazy;
}t[maxn<<2];

void build(int rt,int l,int r){
    t[rt].l=l,t[rt].r=r,t[rt].col=1,t[rt].lazy=-1;
    if(l == r-1) return ;
    int mid = (l+r)/2;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid,r);
}

void pushdown(int rt){
    if(t[rt].lazy != -1){
        t[rt<<1].lazy = t[rt<<1|1].lazy = 1;
        t[rt].lazy = -1;
        t[rt<<1].col = t[rt<<1|1].col = t[rt].col;
        t[rt].col = -1;
    }
}

void update(int rt,int l,int r,int col){
    int L = t[rt].l, R = t[rt].r;
    if(l<=L && R<=r){
        t[rt].col = col;
        t[rt].lazy = 1;
        return ;
    }
    pushdown(rt);
    int mid = (L+R)/2;
    if( l >= mid )  
        update(rt<<1|1,l,r,col);
    else  
        if( r <= mid )  
            update(rt<<1,l,r,col); 
        else  
        {  
            update(rt<<1,l,mid,col);
            update(rt<<1|1,mid,r,col);
        }  
}

void query(int rt){
    int L = t[rt].l, R = t[rt].r;
    if(t[rt].lazy == 1){
        for(int i=L; i<R; i++)
            vis[i] = t[rt].col;
        return ;
    }
    if(L == R) return ;
    pushdown(rt);
    query(rt<<1);
    query(rt<<1|1);
}

int main(){
    int n=read();
    x[0] = 0, y[0] = 1e9, c[0] = 1;
    tmp[cnt++]=x[0],tmp[cnt++]=y[0];
    for(int i=1; i<=n; i++){
        scanf("%d%d %c",&x[i],&y[i],&ch);
        tmp[cnt++] = x[i];
        tmp[cnt++] = y[i];
        if(ch == 'w') c[i] = 1;
    }
    sort(tmp,tmp+cnt);
    cnt = unique(tmp,tmp+cnt)-tmp;

    build(1,0,cnt);

    for(int i=0; i<=n; i++){
        int p1,p2;
        p1 = lower_bound(tmp,tmp+cnt,x[i])-tmp;
        p2 = lower_bound(tmp,tmp+cnt,y[i])-tmp;
        update(1,p1,p2,c[i]);
    }   


    memset(vis,1,sizeof(vis));
    query(1);

    vis[cnt] = 0;
    tmp[cnt] = 1e9;
    int s=0,e=0,ts,te;
    for(int i=0; i<cnt; i++){
        int color=vis[i],j=i;
        ts = tmp[i];  
        while(color==1 && vis[j]==1 && j<=cnt) ++j;
        te = tmp[j];
        if(te-ts > e-s){
            s=ts,e=te;
        }
        i = j;
    }  
    printf("%d %d\n",s,e);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值