zoukankan      html  css  js  c++  java
  • HDU 1199 Color the Ball

    Color the Ball

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 5631    Accepted Submission(s): 1395


    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

    线段树区间覆盖的问题,问题是数字范围太大,所以要离散化。当然也可以不用离散化作,即动态开点。

    这道题目的测试数据比较弱,所以我用了动态开点,要是数据强一点,可能会内存超限,

    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <algorithm>
    #include <math.h>
    
    using namespace std;
    typedef long long int LL;
    const int maxn=2*1e3;
    LL mmax[maxn*35];
    LL lmax[maxn*35];
    LL rmax[maxn*35];
    int c[maxn*35];
    int l[maxn*35];
    int r[maxn*35];
    LL ll[maxn*35];
    LL rr[maxn*35];
    int n;
    int p;
    int newnode()
    {
        l[p]=r[p]=-1;
        lmax[p]=rmax[p]=mmax[p]=0;
        ll[p]=rr[p]=-1;
        return p++;
    }
    void pushdown(int node,LL L,LL R)
    {
        if(!(L==R))
        {
              if(l[node]==-1) l[node]=newnode();
            if(r[node]==-1) r[node]=newnode();
        }
        if(c[node]!=-1)
        {
            if(c[node])
            {
                lmax[node]=rmax[node]=mmax[node]=R-L+1;
                ll[node]=L,rr[node]=R;
            }
            else
            {
                lmax[node]=rmax[node]=mmax[node]=0;
                ll[node]=rr[node]=-1;
            }
    
            c[l[node]]=c[r[node]]=c[node];
            c[node]=-1;
        }
    }
    void pushup(int node,LL L,LL R)
    {
        LL mid=(L+R)>>1;
        
        pushdown(l[node],L,mid);
        pushdown(r[node],mid+1,R);
        if(lmax[l[node]]==(mid-L+1))
            lmax[node]=lmax[l[node]]+lmax[r[node]];
        else
            lmax[node]=lmax[l[node]];
        if(rmax[r[node]]==(R-mid))
            rmax[node]=rmax[r[node]]+rmax[l[node]];
        else
            rmax[node]=rmax[r[node]];
        mmax[node]=max(lmax[r[node]]+rmax[l[node]],max(mmax[l[node]],mmax[r[node]]));
        if(mmax[node]==mmax[l[node]])
            ll[node]=ll[l[node]],rr[node]=rr[l[node]];
        else if(mmax[node]==lmax[r[node]]+rmax[l[node]])
            ll[node]=mid-rmax[l[node]]+1,rr[node]=mid+lmax[r[node]];
        else
            ll[node]=ll[r[node]],rr[node]=rr[r[node]];
    
    }
    
    void update(int node,LL begin,LL end,LL L,LL R,int tag)
    {
    
        if(L<=begin&&end<=R)
        {
            c[node]=tag;
            if(l[node]==-1) l[node]=newnode();
            if(r[node]==-1) r[node]=newnode();
            pushdown(node,begin,end);
            return;
        }
        if(l[node]==-1) l[node]=newnode();
        if(r[node]==-1) r[node]=newnode();
        pushdown(node,begin,end);
        int mid=(begin+end)>>1;
    
        if(L<=mid) update(l[node],begin,mid,L,R,tag);
        if(R>mid) update(r[node],mid+1,end,L,R,tag);
        pushup(node,begin,end);
    }
    int main()
    {
    
        while(scanf("%d",&n)!=EOF)
        {
            p=0;
            int root=newnode();
            LL a,b;
            char cc;
            LL len=(1<<31)-1;
            int tag;
            update(root,1,len,1,len,0);
            memset(c,-1,sizeof(c));
          for(int i=1;i<=n;i++)
            {
                scanf("%lld%lld %c",&a,&b,&cc);
                if(cc=='w') tag=1;
                else tag=0;
                update(root,1,len,a,b,tag);
            }
            if(ll[root]==-1)
                printf("Oh, my god
    ");
            else
                printf("%lld %lld
    ",ll[root],rr[root]);
        }
        return 0;
    }


  • 相关阅读:
    纯js实现字符串formate方法
    C#实现json压缩和格式化
    简单的前端校验框架实现
    快速拷贝文件
    0012 移除元素
    0011 删除链表的倒数第N个节点
    0010 最长公共前缀
    0009 合并两个有序链表
    0008 合并K个排序链表
    0007 回文数
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228578.html
Copyright © 2011-2022 走看看