poj 2777 Count Color(线段树区间更新+技巧)

来源:互联网 发布:日本对外贸易数据2015 编辑:程序博客网 时间:2024/06/11 23:29

Count Color
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 46667 Accepted: 14137

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 4C 1 1 2P 1 2C 2 2 2P 1 2

Sample Output

21

Source

POJ Monthly--2006.03.26,dodo


分析:这题和普通的线段树区间更新一样,只不过在存父亲节点的时候要用一个小技巧。用按位或来存,当然每个子节点对应的也不能是存1-30这样的整数,要存(1<<0)--(1<<29)这样的数,因为数字不是太大,所以这样的数Int就可以存下。最后算每个数二进制有多少个1就是有多少不同的颜色。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N=101000;int tr[4*N];int add[4*N];void PushUp(int i){    tr[i]=tr[2*i]|tr[2*i+1];}void PushDown(int i){    if(add[i])    {        add[i<<1]=add[i<<1|1]=add[i];        tr[i<<1]=tr[i<<1|1]=1<<(add[i]-1);        add[i]=0;    }}void build(int i,int l,int r){    tr[i]=1;    add[i]=0;    if(l==r)    {        return;    }    int mid=(l+r)/2;    build(2*i,l,mid);    build(2*i+1,mid+1,r);    PushUp(i);}void update(int i,int l,int r,int x,int y,int c){    if(x<=l&&r<=y)    {        tr[i]=(1<<(c-1));        add[i]=c;        return;    }    PushDown(i);    int mid=(l+r)/2;    if(x<=mid) update(2*i,l,mid,x,y,c);    if(y>mid) update(2*i+1,mid+1,r,x,y,c);    PushUp(i);}int query(int i,int l,int r,int x,int y){    if(x<=l&&r<=y)    {        return tr[i];    }    PushDown(i);    int ans=0;    int mid=(l+r)/2;    if(x<=mid) ans|=query(2*i,l,mid,x,y);    if(y>mid) ans|=query(2*i+1,mid+1,r,x,y);    return ans;}int cal(int num){    int ans=0;    while(num)    {        if(num%2==1)            ans++;        num/=2;    }    return ans;}int main(){    int l,t,o,x,y,c;    while(~scanf("%d%d%d",&l,&t,&o))    {        build(1,1,l);        for(int i=1;i<=o;i++)        {            char a[5];            scanf("%s",a);            if(a[0]=='C')            {                scanf("%d%d%d",&x,&y,&c);                if(x>y) swap(x,y);                update(1,1,l,x,y,c);            }            else            {                scanf("%d%d",&x,&y);                if(x>y) swap(x,y);                int num=query(1,1,l,x,y);                int ans=cal(num);                printf("%d\n",ans);            }        }    }    return 0;}


阅读全文
0 0