(POJ2777)Count Color <经典:线段树区间修改 + 或运算记颜色数>

来源:互联网 发布:计算机算法书籍推荐 编辑:程序博客网 时间:2024/05/21 23:00

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
Source

POJ Monthly–2006.03.26,dodo

题意:
有一个长度为n(1~100000)个单位的画板,有t=种颜料。现在叫你完成m组操作,操作分两类:
C a b c : 将区间【a,b】单位图成c色
P a b : 问区间【a,b】单位之间有多少种不同的颜色
注意: a 可能大于 b

分析:
典型的线段树区间修改的问题。不过难点在于如何记录某个区间的颜色的数目,并且便于更新。
关于记录种类数目或几个中选几个的问题,我们可以用或运算来处理】
用一个int col 来表示某段区间的颜色的数目 col有32位,每一位表示一种颜色,当某一位为1代表有,0代表无
cover代表区间是否整体被某颜色覆盖(修改)了

AC代码:

#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int N=100010;#define L(rt) (rt<<1)#define R(rt) (rt<<1|1)struct Tree{    int l,r;    int col;    //  用一个32位的int型,每一位对应一种颜色,用位运算代替bool col[32]    bool cover; //  表示这个区间都被涂上同一种颜色,线段树效率的体现,否则插入就是0(n)了。}tree[N<<2];void PushUp(int rt){    // 最后递归回来再更改父节点的颜色    tree[rt].col=tree[L(rt)].col | tree[R(rt)].col;}void build(int L,int R,int rt){    tree[rt].l=L;    tree[rt].r=R;    tree[rt].col=1; //  开始时都为涂有颜色1,看题要仔细,要注意状态。    tree[rt].cover=1;    if(tree[rt].l==tree[rt].r)        return ;    int mid=(L+R)>>1;    build(L,mid,L(rt));    build(mid+1,R,R(rt));}void PushDown(int rt){  //  延迟覆盖的操作    tree[L(rt)].col=tree[rt].col;    tree[L(rt)].cover=1;    tree[R(rt)].col=tree[rt].col;    tree[R(rt)].cover=1;    tree[rt].cover=0;}void update(int val,int L,int R,int rt){    if(L<=tree[rt].l && R>=tree[rt].r){        tree[rt].col=val;        tree[rt].cover=1;        return ;    }    if(tree[rt].col==val)  //剪枝        return ;    if(tree[rt].cover)        PushDown(rt);    int mid=(tree[rt].l+tree[rt].r)>>1;    if(R<=mid)        update(val,L,R,L(rt));    else if(L>=mid+1)        update(val,L,R,R(rt));    else{        update(val,L,mid,L(rt));        update(val,mid+1,R,R(rt));    }    PushUp(rt);      // 最后递归回来再更改父节点的颜色}int sum;void query(int L,int R,int rt){    if(L<=tree[rt].l && R>=tree[rt].r){        sum |= tree[rt].col;        return ;    }    if(tree[rt].cover){     //  这个区间全部为1种颜色,就没有继续分割区间的必要了        sum |= tree[rt].col;     //  颜色种类相加的位运算代码        return;    }    int mid=(tree[rt].l+tree[rt].r)>>1;    if(R<=mid)        query(L,R,L(rt));    else if(L>=mid+1)        query(L,R,R(rt));    else{        query(L,mid,L(rt));        query(mid+1,R,R(rt));    }}int solve(){    int ans=0;    while(sum){        if(sum&1)            ans++;        sum>>=1;    }    return ans;}void swap(int &a,int &b){    int tmp=a;a=b;b=tmp;}int main(){    //freopen("input.txt","r",stdin);    int n,t,m;    while(~scanf("%d%d%d",&n,&t,&m)){        build(1,n,1);        char op[3];        int a,b,c;        while(m--){            scanf("%s",op);            if(op[0]=='C'){                scanf("%d%d%d",&a,&b,&c);                if(a>b)                    swap(a,b);                update(1<<(c-1),a,b,1); // int型的右起第c位变为1,即2的c-1次方。            }else{                scanf("%d%d",&a,&b);                if(a>b)                    swap(a,b);                sum=0;                query(a,b,1);                printf("%d\n",solve());            }        }    }    return 0;}
1 0