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;
    }
  • 相关阅读:
    低级错误之Hbm中类型不一致错误
    低级错误之Oracle客户端添加数据
    低级错误之页面中action的请求地址与action中写的requsetmapping不一致
    低级错误之删除存在关联的表报错
    低级错误之方法自调陷入死循环
    Notepad++的一些常用的快捷键
    [css]inline-block
    框架开发之Java注解的妙用
    面试必备【含答案】Java面试题系列(二
    Kotlin:数组、字符串模板
  • 原文地址:https://www.cnblogs.com/jackge/p/3014343.html
Copyright © 2011-2022 走看看