• Count Color


    Count Color
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 43773   Accepted: 13249

    Description

    Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem.

    There is a very long board with length L centimeter, L is a positive integer, so we can evenly divide the board into L segments, and they are labeled by 1, 2, ... L from left to right, each is 1 centimeter long. Now we have to color the board - one segment with only one color. We can do following two operations on the board:

    1. "C A B C" Color the board from segment A to segment B with color C.
    2. "P A B" Output the number of different colors painted between segment A and segment B (including).

    In our daily life, we have very few words to describe a color (red, green, blue, yellow…), so you may assume that the total number of different colors T is very small. To make it simple, we express the names of colors as color 1, color 2, ... color T. At the beginning, the board was painted in color 1. Now the rest of problem is left to your.

    Input

    First line of input contains L (1 <= L <= 100000), T (1 <= T <= 30) and O (1 <= O <= 100000). Here O denotes the number of operations. Following O lines, each contains "C A B C" or "P A B" (here A, B, C are integers, and A may be larger than B) as an operation defined previously.

    Output

    Ouput results of the output operation in order, each line contains a number.

    Sample Input

    2 2 4
    C 1 1 2
    P 1 2
    C 2 2 2
    P 1 2
    

    Sample Output

    2
    1
    

    Source

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #define N 100005
    #define lson i*2,l,m
    #define rson i*2+1,m+1,r
    using namespace std;
    bool visit[31];
    int color[N*4];
    int cur;
    void build(int i,int l,int r)
    {
        color[i]=1;
        if(l==r)    return;
        int m=(l+r)/2;
        build(lson);
        build(rson);
    }
    void pushdown(int i)
    {
        if(color[i]>0)
            color[i*2]=color[i*2+1]=color[i];
    }
    void pushup(int i)
    {
        if(color[i*2]==-1||color[i*2+1]==-1)
            color[i]=-1;
        else if(color[i*2]==color[i*2+1])
            color[i]=color[i*2];
        else
            color[i]=-1;
    }
    void update(int ql,int qr,int val,int i,int l,int r)
    {
        //cout<<"l="<<l<<" r="<<r<<endl;
        if(ql<=l&&r<=qr)
        {
            //cout<<"l="<<l<<" r="<<r<<endl;
            color[i]=val;
            return ;
        }
        pushdown(i);
        int m=(l+r)/2;
        if(ql<=m) update(ql,qr,val,lson);
        if(m<qr) update(ql,qr,val,rson);
        pushup(i);
    }
    void query(int ql,int qr,int i,int l,int r)
    {
        //cout<<"l="<<l<<" r="<<r<<endl;
        //cout<<"i="<<i<<endl;
        if(color[i]>0)///这个区间是相同的颜色
        {
            //cout<<l<<"到"<<r<<" 颜色"<<color[i]<<endl;
            if(visit[color[i]]==false)
                cur++;
            visit[color[i]]=true;
            return ;
        }
        int m=(l+r)/2;
        if(ql<=m) query(ql,qr,lson);
        if(m<qr) query(ql,qr,rson);
    }
    int main()
    {
        //freopen("C:\Users\acer\Desktop\in.txt","r",stdin);
        int l,t,o;
        char op;
        while(scanf("%d%d%d",&l,&t,&o)!=EOF)
        {
            //cout<<l<<" "<<t<<" "<<o<<endl;
            build(1,1,l);
            getchar();
            while(o--)
            {
                scanf("%c",&op);
                memset(visit,false,sizeof visit);
                int a,b,c;
                if(op=='C')
                {
                    scanf("%d%d%d",&a,&b,&c);
                    update(a,b,c,1,1,l);
                    //cout<<"ok"<<endl;
                }
                else
                {
                    cur=0;
                    scanf("%d%d",&a,&b);
                    query(a,b,1,1,l);
                    printf("%d
    ",cur);
                    //cout<<"ok"<<endl;
                }
                getchar();
            }
        }
    
        return 0;
    }
  • 相关阅读:
    apk逆向工具总结
    DAY...
    DAY1-Flask项目
    Django笔记
    DAY8-Python学习笔记
    DAY7-Python学习笔记
    DAY6-Python学习笔记
    DAY5-Python学习笔记
    DAY4-Python学习笔记
    DAY3-Python学习笔记
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6017429.html
Copyright © 2020-2023  润新知