• Count Color(poj 2777)


    题意:

    给一个固定长度为L的画板

    有两个操作:

    C A B C:区间AB内涂上颜色C。

    P A B:查询区间AB内颜色种类数。

    分析:显然是要用线段树来操作的,设定一个sum[]来维护一个区间内的颜色,若为-1,则说明不是叶子节点,还应在往底下找;

           同时在查询时维护一个vis[]来记录在这个区间内哪个颜色被使用过了。

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #define lson l,m,now*2
    #define rson m+1,r,now*2+1
    #define M 400010
    #define N 35
    using namespace std;
    int vis[N],sum[M];//当sum[i]=-1是,说明不是底下至少有2种颜色,否则sum[i]是i的颜色 
    void build(int l,int r,int now)
    {
        sum[now]=1;
        if(l==r)return;
        int m=(l+r)/2;
        build(lson);
        build(rson);
    }
    void push_down(int now)
    {
        sum[now*2]=sum[now];
        sum[now*2+1]=sum[now];
        sum[now]=-1;
    }
    void change(int x,int y,int v,int l,int r,int now)
    {
        if(l>=x&&r<=y)
        {
            sum[now]=v;
            return;
        }
        if(sum[now]==v)return;
        if(sum[now]!=-1)push_down(now);
        int m=(l+r)/2;
        if(x<=m)change(x,y,v,lson);
        if(y>m)change(x,y,v,rson);
    }
    void query(int x,int y,int l,int r,int now)
    {
        if(sum[now]!=-1)
        {
            vis[sum[now]]=1;
            return;
        }
        int m=(l+r)/2;
        if(x<=m)query(x,y,lson);
        if(y>m)query(x,y,rson);
    }
    int main()
    {
        int n,m,p;
        scanf("%d%d%d",&n,&m,&p);
        build(1,n,1);
        for(int i=1;i<=p;i++)
        {
            char c;
            cin>>c;
            if(c=='C')
            {
                int a,b,v;
                scanf("%d%d%d",&a,&b,&v);
                if(a>b)swap(a,b);
                change(a,b,v,1,n,1);
            }
            else
            {
                memset(vis,0,sizeof(vis));
                int a,b,tot=0;
                scanf("%d%d",&a,&b);
                if(a>b)swap(a,b);
                query(a,b,1,n,1);
                for(int i=1;i<=30;i++)
                  if(vis[i])tot++;
                printf("%d
    ",tot);
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    检测对象类型的两种方式,constructor属性和instanceof
    Javascript中的事件
    工厂模式、寄生构造函数模式、稳妥构造函数模式比较
    ECMAScript中的原型继承
    Javascript中new的作用
    js组合继承
    【原型模式】--重写原型对象prototype的影响
    动态原型模式
    js类型检测
    Javascript中的继承与复用
  • 原文地址:https://www.cnblogs.com/harden/p/5624932.html
Copyright © 2020-2023  润新知