POJ 2777Count Color

Posted labelray

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2777Count Color相关的知识,希望对你有一定的参考价值。

【POJ 2777】Count Color

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
#include <iostream>

#include <cstdio>

using namespace std;

struct SegmentTree{

    int l,r,col;

    bool cov;

}t[400040];

int L,T,O,A,B,C;

void build(int l,int r,int p){

    t[p].l=l;

    t[p].r=r;

    t[p].col=(1<<1);

    t[p].cov=true;

    if(l==r)    return;

    build(l,(l+r)/2,p*2);

    build((l+r)/2+1,r,p*2+1);

}

void add(int l,int r,int col,int p){

    if(l<=t[p].l&&r>=t[p].r){

        t[p].cov=true;

        t[p].col=(1<<col);

        return;

    }

    if(t[p].cov){

        t[p*2].cov=true;

        t[p*2+1].cov=true;

        t[p*2].col=t[p].col;

        t[p*2+1].col=t[p].col;

        t[p].cov=false;

    }

    if(l<=(t[p].l+t[p].r)/2)

        add(l,r,col,p*2);

    if(r>(t[p].l+t[p].r)/2)

        add(l,r,col,p*2+1);

    t[p].col=t[p*2].col|t[p*2+1].col;

}

int query(int l,int r,int p){

    if(l==t[p].l&&r==t[p].r)

        return t[p].col;

    if(t[p].cov){

        t[p*2].cov=true;

        t[p*2+1].cov=true;

        t[p*2].col=t[p].col;

        t[p*2+1].col=t[p].col;

        t[p].cov=false;

    }

    if(l>(t[p].l+t[p].r)/2)

        return query(l,r,p*2+1);

    else if(r<=(t[p].l+t[p].r)/2)

        return query(l,r,p*2);

    else    

        return query(l,(t[p].l+t[p].r)/2,p*2)|query((t[p].l+t[p].r)/2+1,r,p*2+1);

}

int fj(int k){

    int ans=0;

    while(k){

        if(k%2)

            ans++;

        k=k/2;

    }

    return ans;

}

int main(){

    char ch;

    scanf("%d %d %d",&L,&T,&O);

    build(1,L,1);

    for(int i=1;i<=O;i++){

        scanf("\n%ch",&ch);

        if(ch==C){

            scanf("%d %d %d",&A,&B,&C);

            if(A>B)    swap(A,B);

            add(A,B,C,1);

        }else{

            scanf("%d %d",&A,&B);

            if(A>B)    swap(A,B);

            int ans=fj(query(A,B,1));

            cout<<ans<<endl;

        }

    }

    return 0;

}

 

以上是关于POJ 2777Count Color的主要内容,如果未能解决你的问题,请参考以下文章

POJ2777-Count Color 线段树

POJ2777--Count Color

poj2777 Count Color

poj 2777 Count Color

poj 2777 count color 线段树

POJ2777 Count Color线段树