zoukankan      html  css  js  c++  java
  • ZOJ 2301 Color the Ball 线段树(区间更新+离散化)

    Color the Ball

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    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未知,因此离散化好处理,线段树的区间更新

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <queue>
    #include <cstdlib>
    #include <iomanip>
    #include <cmath>
    #include <ctime>
    #include <map>
    #include <set>
    using namespace std;
    #define lowbit(x) (x&(-x))
    #define max(x,y) (x>y?x:y)
    #define min(x,y) (x<y?x:y)
    #define MAX 100000000000000000
    #define MOD 1000000007
    #define pi acos(-1.0)
    #define ei exp(1)
    #define PI 3.141592653589793238462
    #define INF 0x3f3f3f3f3f
    #define mem(a) (memset(a,0,sizeof(a)))
    typedef long long ll;
    const int maxn=50005;
    struct node
    {
        int l,r,co;
    }tree[maxn*4];
    int n,cnt,tot;
    int num[maxn],ha[maxn],color[maxn],co[maxn],li[maxn],ri[maxn];
    int find(int x)
    {
        int l=1,r=cnt;
        while(l<=r)
        {
            int mid=(l+r)>>1;
            if(ha[mid]==x) return mid;
            else if(ha[mid]<x) l=mid+1;
            else r=mid-1;
        }
        return -1;
    }
    void build(int l,int r,int root)
    {
        tree[root].l=l,tree[root].r=r,tree[root].co=1;
        if(l==r) return ;
        int mid=(l+r)>>1;
        build(l,mid,root<<1);
        build(mid+1,r,root<<1|1);
    }
    void update(int l,int r,int root,int val)
    {
        if(tree[root].l==l && tree[root].r==r)
        {
            tree[root].co=val;
            return;
        }
        if(tree[root].co==val) return;
        if(tree[root].co!=-1)
        {
            tree[root<<1].co=tree[root<<1|1].co=tree[root].co;
            tree[root].co=-1;
        }
        if(r<=tree[root<<1].r) update(l,r,root<<1,val);
        else if(l>=tree[root<<1|1].l)update(l,r,root<<1|1,val);
        else update(l,tree[root<<1].r,root<<1,val),update(tree[root<<1|1].l,r,root<<1|1,val);
    }
    void query(int l,int r,int root)
    {
        if(tree[root].co!=-1)
        {
            for(int i=tree[root].l;i<=tree[root].r;i++)
                color[i]=tree[root].co;
            return;
        }
        if(r<=tree[root<<1].r) query(l,r,root<<1);
        else if(l>=tree[root<<1|1].l) query(l,r,root<<1|1);
        else query(l,tree[root<<1].r,root<<1),query(tree[root<<1|1].l,r,root<<1|1);
    
    }
    int main(int argc,char *argv[])
    {
        char s[5];
        while(scanf("%d",&n)!=EOF)
        {
            tot=0;
            memset(color,1,sizeof(color));
            for(int i=0;i<n;i++)
            {
                scanf("%d%d%s",&li[i],&ri[i],s);
                co[i]=s[0]=='b'?1:0;
                num[tot++]=li[i];
                num[tot++]=ri[i];
            }
            sort(num,num+tot);
            int t=tot;
            for(int i=1;i<t;i++)
            {
                if(num[i]-num[i-1]>1) num[tot++]=num[i-1]+1;
                if(num[i]-num[i-1]>2) num[tot++]=num[i]-1;
            }
            sort(num,num+tot);
            cnt=0;
            ha[++cnt]=num[0];
            for(int i=1;i<tot;i++)
                if(num[i-1]!=num[i]) ha[++cnt]=num[i];
            build(1,maxn,1);
            for(int i=0;i<n;i++)
            {
                int a=find(li[i]);
                int b=find(ri[i]);
                update(a,b,1,co[i]);
            }
            query(1,maxn,1);
            int s,e,ans=0,i=1,j;
            while(i<=cnt)
            {
                if(color[i]==0)
                {
                    j=i;
                    while(color[j]==0 && j<=cnt) j++;
                    int t=ha[j-1]-ha[i]+1;
                    if(t>ans)
                    {
                        ans=t;
                        s=ha[i];
                        e=ha[j-1];
                    }
                    i=j;
                }
                else i++;
            }
            if(!ans) printf("Oh,my god
    ");
            else printf("%d %d
    ",s,e);
        }
        return 0;
    }

  • 相关阅读:
    第五周项目4-静态成员应用
    第五周项目3-用多文件组织多个类的程序
    python的wraps函数
    python函数加括号于不加括号的区别
    socket.error: [Errno 10048]
    tensorflow的MNIST教程
    cidr地址段
    mongodb数据库的存储问题
    搭建巡风扫描器
    mongodb的数据库,集合,数据可介绍。
  • 原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7253582.html
Copyright © 2011-2022 走看看