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

这个题也就是一个线段树的修改区间查找区间的问题,

一条线段,一开始全是 1, 然后 C x, y, z, 修改 x 到 y 区间,把颜色换成 z

P x y 查找 x 到 y 区间, 一共有多少种颜色,,

颜色最多有 30 种,所以可以用到位运算。

扫描二维码关注公众号,回复: 2444685 查看本文章

和以前不同的是,这个题 是要用到  位运算 | 。

之前的线段树的题目,把所有的值加起来,这次是把所有的值 或  起来。

比如说  

1

10

100

1000

分别代表,颜色 1  ,颜色 2, 颜色3 颜色 4.

把他们或起来, 就是4 中颜色都有,,

也就是说,1 在哪个位置,就是有哪种颜色。

知道这样的话,就跟之前的线段树就没有什么区别了。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 2e5+10;
struct node{
    int a,b,l,r,c,w;
    int cal_len(){
        return b+a;
    };
}f[N];
int x,y,z,t,n,T;
void swap(int &x, int &y){
    int z;
    z = x; x = y; y = z;
    return;
}
void build(int a, int b, int p){
    f[p].a = a; f[p].b = b;
    if (a + 1 == b){
        f[p].w = 1;
        return;
    }
    int m = f[p].cal_len() / 2;
    t++; f[p].l = t; build(a,m,t);
    t++; f[p].r = t; build(m,b,t);
    f[p].w = f[f[p].l].w | f[f[p].r].w;
    return;
}

void updata(int p){
    if (f[p].c == 0) return;
    f[f[p].l].c = f[f[p].r].c = f[p].c;
    f[f[p].l].w = f[f[p].r].w = f[p].c;
    f[p].w = f[p].c;
    f[p].c = 0;
    return;
}
void Insert(int p){
    int m;
    if (x <= f[p].a &&  y >= f[p].b - 1){
        f[p].w = f[p].c = 1 << (z - 1);
        return;
    }
    updata(p);
    m = f[p].cal_len() / 2;
    if (x < m) Insert(f[p].l);
    if (y >= m) Insert(f[p].r);
    f[p].w = f[f[p].l].w | f[f[p].r].w;
    return;
}
int Qurey(int p){
    int m,ans = 0;
    if (x <= f[p].a && y >= f[p].b - 1){
        return f[p].w;
    }
    updata(p);
    m = f[p].cal_len() / 2;
    if (x < m) ans = ans | Qurey(f[p].l);
    if (y >= m) ans = ans | Qurey(f[p].r);
    return ans;
}
int main() {
    int O;
    char ch;
    scanf("%d%d%d",&n,&T,&O);
    t = 1;
    build(1,n+1,1);
    for (int i = 0; i < O; i++){
        cin>>ch;
        scanf("%d%d",&x,&y);
        if (x > y) swap(x,y);
        if (ch == 'C'){
            scanf("%d",&z);
            Insert(1);
        } else {
            int sum = 0,tot = 0;
            sum = Qurey(1);
            while(sum > 0){
                if (sum & 1 == 1) tot++;
                sum = sum / 2;
            }
            printf("%d\n",tot);

        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/kidsummer/article/details/81120206