• HDU4391(线段树+剪枝)


    Paint The Wall

    Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3114    Accepted Submission(s): 846


     

    Problem Description
    As a amateur artist, Xenocide loves painting the wall. The wall can be considered as a line consisting of n nodes. Each node has its own color.

    Xenocide spends all day in front of the wall. Sometimes, he paints some consecutive nodes so that these nodes have the same color. When he feels tired, he focuses on a particular color and counts the number of nodes that have this color within a given interval.

    Now Xenocide is tired of counting, so he turns to you for help.
     
    Input
    The input consists of several test cases.
    The first line of each test case contains two integer n, m(1<=n, m<=100000) indicating the length of the wall and the number of queries.
    The following line contains N integers which describe the original color of every position.
    Then m lines follow. Each line contains 4 non-negative integers a, l, r, z(1<=a<=2, 0<=l<=r<n ,0<=z<231).
    a = 1 indicates that Xenocide paints nodes between l and r and the resulting color is z.
    a = 2 indicates that Xenocide wants to know how many nodes between l and r have the color z.
     
    Output
    Print the corresponding answer for each queries.
     
    Sample Input
    5 5
    1 2 3 4 0
    2 1 3 3
    1 1 3 1
    2 1 3 3
    2 0 3 1
    2 3 4 1
     
    Sample Output
    1
    0
    4
    1
    剪枝线段树。
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int MAXN=100005;
    struct Node{
        int col,mx,mn;//col==-1 表示该区间为杂色
        int l,r;
    }a[MAXN*4];
    void pushUp(int rt)
    {
        if(a[rt<<1].col==a[(rt<<1)|1].col)
            a[rt].col=a[rt<<1].col;
        else
            a[rt].col=-1;
        a[rt].mn=min(a[rt<<1].mn,a[(rt<<1)|1].mn);
        a[rt].mx=max(a[rt<<1].mx,a[(rt<<1)|1].mx);
    }
    void build(int rt,int l,int r)
    {
        a[rt].l=l;
        a[rt].r=r;
        if(l==r)
        {
            scanf("%d",&a[rt].col);
            a[rt].mn=a[rt].col;
            a[rt].mx=a[rt].col;
            return ;
        }
        int mid=(l+r)>>1;
        build(rt<<1,l,mid);
        build((rt<<1)|1,mid+1,r);
        pushUp(rt);
    }
    void pushDown(int rt)
    {
        if(a[rt].col!=-1)
        {
            a[rt<<1].col=a[(rt<<1)|1].col=a[rt].col;
            a[rt<<1].mn=a[(rt<<1)|1].mn=a[rt].col;
            a[rt<<1].mx=a[(rt<<1)|1].mx=a[rt].col;
        }
    }
    void update(int rt,int l,int r,int v)
    {
        if(a[rt].l==l&&a[rt].r==r)
        {
            a[rt].col=v;
            a[rt].mn=a[rt].mx=v;
            return ;
        }
        pushDown(rt);
        int mid=(a[rt].l+a[rt].r)>>1;
        if(r<=mid)
            update(rt<<1,l,r,v);
        else if(mid<l)
            update((rt<<1)|1,l,r,v);
        else
        {
            update(rt<<1,l,mid,v);
            update((rt<<1)|1,mid+1,r,v);
        }
        pushUp(rt);
    }
    int query(int rt,int l,int r,int v)
    {
        if(v<a[rt].mn||v>a[rt].mx)    return 0;
        if(a[rt].l==l&&a[rt].r==r)
        {
            if(a[rt].col==v)    return r-l+1;
        }
        if(a[rt].l==a[rt].r)
        {
            if(a[rt].col==v)    return 1;
            else    return 0;
        }
        pushDown(rt);
        int mid=(a[rt].l+a[rt].r)>>1;
        if(r<=mid)
            return query(rt<<1,l,r,v);
        else    if(mid<l)
            return query((rt<<1)|1,l,r,v);
        else
            return query(rt<<1,l,mid,v)+query((rt<<1)|1,mid+1,r,v);
    }
    int n,m;
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            build(1,1,n);
            while(m--)
            {
                int op,x,y,z;
                scanf("%d%d%d%d",&op,&x,&y,&z);
                x++,y++;
                if(op==1)
                    update(1,x,y,z);
                else
                    printf("%d
    ",query(1,x,y,z));
            }    
            
        }
        
        return 0;
    }
  • 相关阅读:
    【转载】动态加载wpf控件主题样式资源
    paip.批处理清理java项目冗余jar的方法
    paip.java OutOfMemoryError 解决方法o33
    paip.java win程序迁移linux的最佳实践
    paip.java c# .net php python调用c++ c dll so windows api 总结
    paip.提高效率微信 手机app快速开发平台—微网络撬动大市场
    paip.Log4j配置不起作用的解决
    paip.获取地理位置根据Ip
    paip.java 开发中web server的选择jboss resin tomcat比较..
    paip.提升安全性Des加密 java php python的实现总结
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5142565.html
Copyright © 2020-2023  润新知