zoukankan      html  css  js  c++  java
  • HDU Paint The Wall

    Paint The Wall

    Time Limit: 20000/10000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 120    Accepted Submission(s): 22
    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.
     
    SampleInput
    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
     
    SampleOutput
    1
    0
    4
    1
     
     
    成段更新,询问过程如果遇到有颜色的线段就可以返回了,修改过程则是遇到有颜色的线段如果匹配就更改,否则就向下传递.但是这么做会TLE.优化的方法是为每条线段增加两个域,记录区间上面颜色的最大值和最小值.如果查询的颜色不在这个范围内,就直接返回. 但是为什么加了一个简单的优化就可以从10000+MS减少到484MS,是否说明测试数据很极端?
     
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    #define lson l,mid,rt<<1
    #define rson mid+1,r,rt<<1|1
    
    const int maxn=100010;
    
    struct Tree{
        int l,r;    //左端点、右端点 
        int c;      //color 
        int mini,most;
    };
    
    struct Tree a[maxn<<2];
    int v[maxn];
    
    void build(int l,int r,int rt){
        int mid=(l+r)>>1;
        a[rt].l=l;
        a[rt].r=r;
        if(l==r){
            a[rt].c=v[l];
            a[rt].mini=v[l];
            a[rt].most=v[l];
            return ;
        }
        build(lson);
        build(rson);
        if(a[rt<<1].c==a[rt<<1|1].c)
            a[rt].c=a[rt<<1].c;
        else
            a[rt].c=-1;
        a[rt].mini=min(a[rt<<1].mini,a[rt<<1|1].mini);
        a[rt].most=max(a[rt<<1].most,a[rt<<1|1].most);
    }
    
    void Update(int l,int r,int id,int val){
        if(l==a[id].l && r==a[id].r){
            a[id].c=val;
            a[id].mini=val;
            a[id].most=val;
            return ;
        }
        if(a[id].l==a[id].r)
            return ;
        if(a[id].c!=-1){
            a[id<<1].c=a[id<<1|1].c=a[id].c;
            a[id].c=-1;
            a[id<<1].mini=a[id<<1|1].mini=a[id].mini;
            a[id<<1].most=a[id<<1|1].most=a[id].most;
        }
        int mid=(a[id].l+a[id].r)>>1;
        if(mid>=r)
            Update(l,r,id<<1,val);
        else if(mid<l)
            Update(l,r,id<<1|1,val);
        else{
            Update(l,mid,id<<1,val);
            Update(mid+1,r,id<<1|1,val);
        }
        if(a[id<<1].c==a[id<<1|1].c)
            a[id].c=a[id<<1].c;
        a[id].mini=min(a[id<<1].mini,a[id<<1|1].mini);
        a[id].most=max(a[id<<1].most,a[id<<1|1].most);
    }
    
    int ans;
    
    void query(int l,int r,int id,int val){
        if(a[id].c!=-1){
            if(a[id].c==val)
                ans+=r-l+1;
            return ;
        }
        if(a[id].l==a[id].r)
            return ;
        if(a[id].mini>val  || a[id].most<val)    //重要的优化 
            return ;
        int mid=(a[id].l+a[id].r)>>1;
        if(mid>=r)
            query(l,r,id<<1,val);
        else if(mid<l)
            query(l,r,id<<1|1,val);
        else{
            query(l,mid,id<<1,val);
            query(mid+1,r,id<<1|1,val);
        }
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int n,m;
        while(~scanf("%d%d",&n,&m)){
            memset(a,0,sizeof(a));
            for(int i=0;i<n;i++)
                scanf("%d",&v[i]);
            build(0,n-1,1);
            int flag,lt,rt,val;
            for(int i=1;i<=m;i++){
                scanf("%d%d%d%d",&flag,&lt,&rt,&val);
                if(flag==1)
                    Update(lt,rt,1,val);
                else{
                    ans=0;
                    query(lt,rt,1,val);
                    printf("%d\n",ans);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    js 获取和设置css3 属性值的实现方法
    API的自动化测试
    删除html标签或标签属性以及样式
    JS+CSS实现数字滚动
    video元素和audio元素相关事件
    SDT v0.0.1 上线
    safari浏览器fixed后,被软键盘遮盖的问题—【未解决】
    js中DOM事件探究
    使用Web存储API存取本地数据
    剑指offer(Java版)第七题:用两个栈实现一个队列。队列的声明如下,请实现它的两个函数appendTail和deleteHead, 分别完成在队列尾部插入结点和在队列头部删除结点的功能。
  • 原文地址:https://www.cnblogs.com/jackge/p/3014343.html
Copyright © 2011-2022 走看看