zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    <<浪潮之巅>>阅读笔记三
    <<浪潮之巅>>阅读笔记二
    <<浪潮之巅>>阅读笔记一
    《需求工程——软件建模与分析》阅读笔记三
    个人总结
    学习进度条(第十六周)
    人月神话阅读笔记03
    第十五周学习进度条
    人月神话阅读笔记02
    操作
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5142565.html
Copyright © 2011-2022 走看看