POJ 2777 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

题解

 使用位运算进行状态压缩,在非叶子节点处记录的是区间内的颜色状态。

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int seg[100005 << 2];
int tag[100005 << 2];

void push_down(int k){
    if(tag[k] != 0){
        tag[k << 1] = tag[k << 1 | 1] = tag[k];
        seg[k << 1] = seg[k << 1 | 1] = 1 << tag[k];
        tag[k] = 0;
    }
}

void push_up(int k){
    seg[k] = seg[k << 1] | seg[k << 1 | 1];
}

void modify(int L, int R, int l, int r, int k, int val){
    if(l >= L && r <= R){
        tag[k] = val;
        seg[k] = 1 << val;
        return;
    }
    push_down(k);
    int mid = (l + r) >> 1;
    if(mid >= L) modify(L, R, l, mid, k << 1, val);
    if(mid < R) modify(L, R, mid + 1, r, k << 1 | 1, val);
    push_up(k);
}

int query(int L, int R, int l, int r, int k){
    if(l >= L && r <= R){
        return seg[k];
    }
    push_down(k);
    int ans = 0;
    int mid = (l + r) >> 1;
    if(mid >= L) ans = ans | query(L, R, l, mid, k << 1);
    if(mid < R) ans = ans | query(L, R, mid + 1, r, k << 1 | 1);
    return ans;
}

void build(int l, int r, int k){
    if(l == r){
        seg[k] = 1 << 1;
        return;
    }
    int mid = (l + r) >> 1;
    build(l, mid, k << 1);
    build(mid + 1, r, k << 1 | 1);
    push_up(k);
}
int main(){
    int l, t, o;
    scanf("%d%d%d", &l, &t, &o);
    memset(tag, 0, sizeof(tag));
    build(1, l, 1);
    char ch[10]; int a, b, c;
    while(o--){
        //getchar();
        scanf("%s", &ch[0]);
        //cin >> ch;
        if(ch[0] == 'C'){
            scanf("%d%d%d", &a, &b, &c);
            if(a > b) swap(a, b);
            //cin >> a >> b >> c;
            modify(a, b, 1, l, 1, c);
        }
        else{
            scanf("%d%d", &a, &b);
            //cin >> a >> b;
            if(a > b) swap(a, b);
            int state = query(a, b, 1, l, 1);
            //cout << state << endl;
            int cnt = 0;
            while(state){
                if(state & 1)
                    cnt++;
                state >>= 1;
            }
            //cout << cnt << endl;
            printf("%d\n", cnt);
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Cl0ud_z/p/11326381.html
今日推荐