poj 2777 (线段树 + 二进制)

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.

就是简单的线段树的区间更新和区间查询,因为颜色的数量很少,可以用二进制表示数量。

直接上代码

#include<iostream>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#define LL long long
using namespace std;
const int MAX = 1e6 + 50;


LL tree[MAX << 2];
LL lazy[MAX << 2];

void PushUp(int rt){
    tree[rt] = tree[rt << 1] | tree[rt << 1 | 1];
}

void PushDown(int rt){
    lazy[rt << 1] = lazy[rt << 1 | 1] = lazy[rt];
    tree[rt << 1] = lazy[rt];
    tree[rt << 1 | 1] = lazy[rt];
    lazy[rt] = 0;
}

void Build(int l, int r, int rt){
    if(l == r){
        tree[rt] = 1;
        return ;
    }
    int m = (l + r) >> 1;
    Build(l, m, rt << 1);
    Build(m + 1, r, rt << 1 | 1);
    PushUp(rt);
}

int Le, Ri;
LL c; 
void UpadteRange(int l, int r, int rt){
    if(Le <= l && r <= Ri){
        tree[rt] = c;
        lazy[rt] = c;
        return ;
    }

    if(lazy[rt]){
        PushDown(rt);
    }
    int m = (l + r) >> 1;
    if(Le <= m){
        UpadteRange(l, m, rt << 1);
    }
    if(Ri > m){
        UpadteRange(m + 1, r, rt << 1 | 1);
    }
    PushUp(rt);
}

LL Query(int l, int r, int rt){
    if(Le <= l && r <= Ri){
        return tree[rt];
    }
    if(lazy[rt]){
        PushDown(rt);
    }
    int m = (l + r) >> 1;
    LL ans = 0;
    if(Le <= m){
        ans |= Query(l, m, rt << 1);
    }
    if(Ri > m){
        ans |= Query(m + 1, r, rt << 1 | 1);
    }
    return ans;
}
int main(){
    int n, t, o;
    scanf("%d%d%d", &n, &t, &o);
    Build(1, n ,1);
    while(o--){
        char op;
        scanf(" %c%d%d", &op, &Le, &Ri);
        if(Le > Ri){
            swap(Le, Ri);
        }
        if(op == 'C'){
            int color;
            scanf("%d", &color);
            c = 1LL << (color - 1);
            UpadteRange(1, n, 1);
        } else{
            LL res = Query(1, n, 1); //要开LL
            int cnt = 0;
            while(res){
                if(res & 1){
                    cnt++;
                }
                res >>= 1;
            }
            printf("%d\n", cnt);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43737952/article/details/89305950