POJ 2777 Count color 统计颜色 线段树

Count Color
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 50214   Accepted: 15139

Description

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

Source

POJ Monthly--2006.03.26,dodo

拿到这题思路涌现 简单的运用下|操作 然后就能求解啦 代码应该挺清晰的(这人真自恋

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
int n,m;
const int MAX_N = 100010;
int s[MAX_N<<2],col[MAX_N<<2];
void up(int p){
   s[p] = (s[p*2]|s[p*2+1]);
}
void build(int p,int l,int r){
    if(l==r){
        col[p] = 1;
        s[p] = 1;
        return;
    }
    int mid = (l+r)>>1;
    build (p*2,l,mid);
    build(p*2+1,mid+1,r);
    up(p);
}
void down(int p,int l,int r){
    if(col[p]){
        col[p*2] = col[p*2+1] = col[p];
        s[p*2]=col[p];
        s[p*2+1]=col[p];
        col[p] = 0;
    }
}
void change(int p,int l,int r,int x,int y,int c){
    if(x<=l&&r<=y){
        col[p] = c;
        s[p]= c;
        return;
    }
    down(p,l,r);
    int mid = (l+r)>>1;
    if(x<=mid) change(p*2,l,mid,x,y,c);
    if(y>mid) change(p*2+1,mid+1,r,x,y,c);
    up(p);
}
int query(int p,int l,int r,int x,int y){
    if(x<=l&&r<=y){
       return s[p];
    }
    int mid = (l+r)>>1,res1 = 0,res2=0,res;
    down(p,l,r);
    if(x<=mid) res1|=query(p*2,l,mid,x,y);
    if(y>mid) res2|=query(p*2+1,mid+1,r,x,y);
    res = res1|res2;
    return res;
}
int main(){
    int t;
    scanf("%d%d%d",&n,&m,&t);
    build(1,1,n);
    while(t--){
        char str[20];
        scanf("%s",str);
        string str_ = str;
        if(str_=="C"){
            int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        if(a>b){
            a=a+b;
            b=a-b;
            a=a-b;
        }
        change(1,1,n,a,b,(1<<(c-1)));
        }
        else if(str_=="P"){
            int a,b;
            scanf("%d%d",&a,&b);
            if(a>b){
            a=a+b;
            b=a-b;
            a=a-b;
        }
            int ans = 0;
            int pre_s = query(1,1,n,a,b);
            //dbg(pre_s);
            while(pre_s){
                if(pre_s&1==1) ans++;
                pre_s/=2;
                //dbg(pre_s);
            }
            printf("%d\n",ans);
        }
       /*for(int i=1;i<=n;++i){
            printf("%---d\n",query(1,1,n,i,i));
        }*/

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/heucodesong/article/details/80679699