ural 1019. Line Painting 线段树 离散化

来源:互联网 发布:在线翻译软件 编辑:程序博客网 时间:2024/06/07 05:06

题目链接:

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_backconst 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