CUGB专题训练之数据结构:B - Count Color 线段树区间更新

B - Count Color
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status

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

这题做得真是有点……晕……区间更新没做过,所以处理颜色的时候不太会……本来想的是每段都记录哪种颜色,但是这个不会,所以直接2种颜色以上的就不把这些颜色都记录了,查询的时候再查询它的子树得了,本来以为这会时间更加多,但是没想到也挺快的,数据弱了!如果数组比较强的话,我这种方法可能会超时的……

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define sca(a) scanf("%d",&a)
#define pri(a) printf("%d\n",a)
#define f(i,a,n) for(i=a;i>1; //建树直接把中点算好,更新与查询就不用再算了
    if(l==r) return;
    build(i<<1,l,tree[i].mid);
    build(i<<1|1,tree[i].mid+1,r);
}
void insert(int i,int l,int r,int lazy)
{
    if(tree[i].l==l&&tree[i].r==r)
    {
        tree[i].lazy=lazy;
        return ;
    }
    if(tree[i].lazy==lazy) return ;
    if(tree[i].lazy)
    {
        tree[i<<1].lazy=tree[i<<1|1].lazy=tree[i].lazy;
        tree[i].lazy=0;
    }
    if(l>tree[i].mid) insert(i<<1|1,l,r,lazy);
    else if(r<=tree[i].mid) insert(i<<1,l,r,lazy);
    else
    {
        insert(i<<1,l,tree[i].mid,lazy);
        insert(i<<1|1,tree[i].mid+1,r,lazy);
    }
}
void query(int i,int l,int r)
{
    if(tree[i].lazy)
    {
        vis[tree[i].lazy]=1;  //哪种颜色出现,重复时不变
        return ;
    }
    if(l>tree[i].mid) query(i<<1|1,l,r);
    else if(r<=tree[i].mid) query(i<<1,l,r);
    else
    {
        query(i<<1,l,tree[i].mid);
        query(i<<1|1,tree[i].mid+1,r);
    }
}
int main()
{
    int l,t,q;
    scanf("%d%d%d",&l,&t,&q);
    build(1,1,l);
    while(q--)
    {
        scanf("%s%d%d",s,&a,&b);
        if(s[0]=='C')
        {
            scanf("%d",&c);
            insert(1,a,b,c);
        }
        else
        {
            int sum=0; mem(vis,0);
            query(1,a,b);
            for(int i=1;i<=t;i++)
                if(vis[i]) sum++; //出现颜色+1
            pri(sum);
        }
    }
    return 0;
}


你可能感兴趣的:(CUGB专题训练之数据结构:B - Count Color 线段树区间更新)