加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

hdu1199(离散化线段树)

发布时间:2020-12-15 01:56:51 所属栏目:Java 来源:网络整理
导读:Color the Ball Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6689????Accepted Submission(s): 1655 ? Problem Description There are infinite balls in a line (numbered 1 2 3 ....),and

Color the Ball

Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6689????Accepted Submission(s): 1655

?

Problem 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
?
?
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1199
?
题目大意:区间初始化颜色为黑色,给定N个区间颜色,求白色最长区间的L,R。
?
题目解析:离散化线段树,熟练之后补解析
?
AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

const int maxn=2e5+5;
int cnt=0;

int l[maxn*2],node[maxn*4];
struct node
{
    int l,r,color,lazy;//color=1白色 
}init[maxn],tree[4*maxn],d[maxn];

void reset_node()
{
    int i=1,j=1;
    node[1]=l[1];
    for(i=2;i<=cnt;i++)
    {
        if(node[j]==l[i]) continue;
        if(node[j]+1<l[i])
        {
            node[++j]=node[j-1]+1;
        }
        if(node[j]<l[i]-1)
        {
            node[++j]=l[i]-1;
        }
        node[++j]=l[i];
    }
    cnt=j;
}

void btree(int l,int r,int k)
{
    tree[k].color=0;
    tree[k].lazy=1;
    tree[k].l=l;tree[k].r=r;
    if(l==r) return ;
    int mid=(l+r)/2;
    btree(l,mid,k<<1);
    btree(mid+1,k<<1|1);
}

int ccnt=0;
void date_tree(int l,int color,int k)
{
    //printf("%d ",ccnt++);
    int mid=(tree[k].l+tree[k].r)/2;
    if(tree[k].l>=l&&tree[k].r<=r)
    {
        tree[k].color=color;
        tree[k].lazy=1;
        return ;
    }
    if(tree[k].lazy&&tree[k].color==color)
        return ;
    if(tree[k].lazy)
    {
        tree[k<<1].color=tree[k<<1|1].color=tree[k].color;
        tree[k<<1].lazy=tree[k<<1|1].lazy=1;
    }
    if(l>mid)
    {
        date_tree(l,k<<1|1);
    }
    else if(r<=mid)
    {
        date_tree(l,k<<1);
    }
    else
    {
        date_tree(l,k<<1);
        date_tree(l,k<<1|1);
    }
    tree[k].lazy=0;
}

int in_node(int k)
{
    int l=1,r=cnt,mid;
    while(l<=r)
    {
        mid=(l+r)/2;
        if(k==node[mid]) break;
        if(k>node[mid]) l=mid+1;
        if(k<node[mid]) r=mid-1;
    }
    return mid;
}

void push_down(int k,int l,int r)
{
    int mid=(l+r)/2;
    if(tree[k].l==tree[k].r) return;
    if(tree[k].lazy)
    {
        tree[k<<1].color=tree[k<<1|1].color=tree[k].color;
        tree[k<<1].lazy=tree[k<<1|1].lazy=1;
    }
    push_down(k<<1,tree[k<<1].l,tree[k<<1].r);
    push_down(k<<1|1,tree[k<<1|1].l,tree[k<<1|1].r);
}

int dn=0;
void get_node(int l,int k)
{
    if(tree[k].lazy)
        d[++dn]=tree[k];
    if(l==r) return ;
    int mid=(l+r)/2;
    get_node(l,k<<1);
    get_node(mid+1,k<<1|1);
}

int main()
{
    int n;
    cnt=0;
    char s[5];
    while(scanf("%d",&n)!=EOF)
    {
        //printf("1n");
        cnt=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%s",&init[i].l,&init[i].r,s);
            l[++cnt]=init[i].l;l[++cnt]=init[i].r;
            if(s[0]==w)
            {
                init[i].color=1;
            }
            else init[i].color=0;
        }
        sort(l+1,l+1+cnt);
        reset_node();
        //printf("%d...n",cnt);
        btree(1,cnt,1);
        //printf("2n");
        int ll,rr,cc;
        for(int i=1;i<=n;i++)
        {
            ll=in_node(init[i].l);
            rr=in_node(init[i].r);
            cc=init[i].color;
            date_tree(ll,cc,1);
        }
        //printf("3n");
        push_down(1,1,cnt);
        //printf("4n");
        dn=0;
        get_node(1,1);
        int L,R;
        int len=-1;
        //printf("5n");
        for(int i=1;i<=dn;i++)
        {
            if(d[i].color)
            {
                int j=i;
                while(d[i+1].color!=0&&i+1<=dn) i++;
                if(i==dn&&node[d[i].r]-node[d[j].l]+1>len)
                {
                    L=node[d[j].l];
                    R=node[d[i].r];
                    len=R-L+1;
                }
                if(i<dn&&node[d[i].l]-node[d[j].l]>len)
                {
                    L=node[d[j].l];
                    R=node[d[i].l];
                    len=R-L;
                }
            }
        }
        //for(int i=1;i<=11;i++)
        //printf("%dn",tree[i].color);
        //printf("6n");
        if(len!=-1)
            printf("%d %dn",L,R);
        else
            printf("Oh,my godn");
    }
    return 0;
}
View Code

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读