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.

Sample Input

2 2 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2

Sample Output

2
1

题目大意:有30种颜色,Q种操作,可以将区间上的块染成相应的颜色,每种颜色用1--30表示,c,p分别表示两种操作

1--30这是比较难处理的,怎么去表示这30种颜色,而且还能相加,那就要用到二进制来表示

#include <iostream>
#include<algorithm>
#include<bitset>
#include<string>
using namespace std;
const int M=100000+5;
#define lson        id<<1 , l, m//这里的宏定义一定要和下面的所用的变量相同
#define rson        id<<1|1 ,m+1,r
int c[M<<2];
int lay[M<<2];
void pu(int x)
{
    c[x]=c[x<<1]|c[x<<1|1];
}
void pd(int id)
{
    if(lay[id])
    {
        c[id<<1]=c[id<<1|1]=lay[id];
        lay[id<<1]=lay[id<<1|1]=lay[id];lay[id]=0;
    }
}
void build (int id,int l,int r)
{
    if(l==r)
    {
        c[id]=1;return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    pu(id);
}
void updata(int id,int l,int r,int L,int R,int v)
{
    if(L<=l&&R>=r)
    {
        c[id]=v;lay[id]=v;return;
    }
    int m=(l+r)>>1;
    pd(id);
    if(L<=m)
        updata(lson,L,R,v);
    if(R>m) updata(rson,L,R,v);
    pu(id);
}
int qu(int id,int l,int r,int L,int R)
{
    if(L<=l&&R>=r)
        return c[id];
        pd(id);
    int m=(l+r)>>1;int ans=0;
    if(L<=m) ans|= qu(lson,L,R);
    if(R>m) ans|= qu(rson,L,R);
    return ans;
}
int main(){
    int l,t,o;
    ios::sync_with_stdio(0);cin.tie(0);
    cin>>l>>t>>o;
    build(1,1,l);
    string s;
    while(o--)
    {
        int x,y,v;
        cin>>s>>x>>y;
        if(x>y) swap(x,y);//一定要注意这个地方!!!表示w了多次><
        if(s[0]=='C')
        {
            cin>>v;updata(1,1,l,x,y,1<<(v-1));///每种颜色就让其表示在第几位为1,这样最多30
        }//位,然后将其相与运算就能得到多少个1
        else if(s[0]=='P'){
            bitset<32>ss(qu(1,1,l,x,y));
            cout<<ss.count()<<endl;//判断二进制种有多少个1;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/c___c18/article/details/81736077
今日推荐