zoukankan      html  css  js  c++  java
  • hdu1199(离散化线段树)

    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
     
     
     
    题目大意:区间初始化颜色为黑色,给定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,r,k<<1|1);
    }
    
    int ccnt=0;
    void date_tree(int l,int r,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,r,color,k<<1|1);
        }
        else if(r<=mid)
        {
            date_tree(l,r,color,k<<1);
        }
        else
        {
            date_tree(l,r,color,k<<1);
            date_tree(l,r,color,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 r,int k)
    {
        if(tree[k].lazy)
            d[++dn]=tree[k];
        if(l==r) return ;
        int mid=(l+r)/2;
        get_node(l,mid,k<<1);
        get_node(mid+1,r,k<<1|1);
    }
    
    int main()
    {
        int n;
        cnt=0;
        char s[5];
        while(scanf("%d",&n)!=EOF)
        {
            //printf("1
    ");
            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...
    ",cnt);
            btree(1,cnt,1);
            //printf("2
    ");
            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,rr,cc,1);
            }
            //printf("3
    ");
            push_down(1,1,cnt);
            //printf("4
    ");
            dn=0;
            get_node(1,cnt,1);
            int L,R;
            int len=-1;
            //printf("5
    ");
            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("%d
    ",tree[i].color);
            //printf("6
    ");
            if(len!=-1)
                printf("%d %d
    ",L,R);
            else
                printf("Oh, my god
    ");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    tableView//collectionView加载时的动画
    关于collectionView和tableView的两种cell的出列方法的区别
    缓存的实现,主要是图片缓存整理
    android context获取目录详解
    Android网络传输中必用的两个加密算法:MD5 和 RSA 及Base64加密总结
    listview 与 button 焦点 在item添加下列属性
    VScode 安装必备
    centos7 安装docker
    1290
    MySQL“Another MySQL daemon already running with the same unix socket” 报错信息处理
  • 原文地址:https://www.cnblogs.com/noback-go/p/11239981.html
Copyright © 2011-2022 走看看