Count Color(线段树)

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

题意:L个球,T种颜色,O次操作,初始每个球颜色为1, C为修改a,b区间的颜色为c, P表示询问a,b区间的颜色种数。

题解:线段数,tree[node]表示颜色的状态,0表示不存在,1表示存在

//#include"bits/stdc++.h"
//#include
//#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
#define lson l, mid, node << 1
#define rson mid + 1, r, node << 1 | 1
const int INF  =  0x3f3f3f3f;
const int O    =  1e6;
const int mod  =  10007;
const int maxn =  1e5+5;
const double PI  =  acos(-1.0);
const double E   =  2.718281828459;

const int _ = 1;
const int __ = 2;
const int ___ = 3;

int tree[maxn<<2];
int lazy[maxn<<2];

int pushup(int sta1, int sta2) { return sta1 | sta2; }

void pushdown(int l ,int r, int node){
    if(lazy[node]){
        tree[node<<1] = tree[node<<1|1] = lazy[node];
        lazy[node<<1] = lazy[node<<1|1] = lazy[node];
        lazy[node] = 0;
    }
}

void build(int l, int r, int node){
    if(l == r) {
        tree[node] = 2;
        lazy[node] = 0;
        return;
    }
    int mid = (l + r) >> 1;
    build(lson); build(rson);
    tree[node] = pushup(tree[node<<1] , tree[node<<1|1]);
}

int ql, qr, v;

void update(int l, int r, int node){
    if(ql <=l && qr >= r) {
        tree[node] = 1 << v;
        lazy[node] = 1 << v;
        return ;
    }
    pushdown(l ,r, node);
    int mid = (l + r) >> 1;
    if(qr > mid) update(rson);
    if(ql <= mid) update(lson);
    tree[node] = pushup(tree[node<<1], tree[node<<1|1]);
}

int query(int l, int r, int node){
    if(ql <= l && qr >= r) return tree[node];
    pushdown(l, r, node);
    int ans = 0;
    int mid = (l + r) >> 1;
    if(qr > mid) ans |= query(rson);
    if(ql <= mid) ans |= query(lson);
    return ans;
}

int main(){
    int l, t, q;
    while(~scanf("%d%d%d", &l, &t, &q)){
        build(1, l, 1);
        while(q --) {
            char c; scanf(" %c", &c);
            if(c == 'C') {
                scanf("%d%d%d", &ql, &qr, &v);
                if(ql > qr) swap (ql, qr);
                update(1, l, 1);
            }
            else if(c == 'P'){
                scanf("%d%d", &ql ,&qr);
                if(ql > qr) swap (ql, qr);
                int sta = query(1, l, 1);
                int ans = 0;
                while(sta) {
                    if(sta & 1) ans ++;
                    sta >>= 1;
                }
                printf("%d\n", ans);
            }
        }
    }
    return 0;
}

 

你可能感兴趣的:(简单线段树)