POJ 2777 Count Color(线段树+二进制位运算)

来源:互联网 发布:windows什么系统好用 编辑:程序博客网 时间:2024/06/06 17:22
Count Color
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 43938 Accepted: 13310

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
题意:

给一个固定长度为L的画板

有两个操作:

C A B C:区间AB内涂上颜色C。

P A B:查询区间AB内颜色种类数。

思路:

首先显然是要线段树。

每个节点有如下参数:

l,r 表示区间。

int color表示颜色,对于颜色要用位运算。

AC代码:

#include <stdio.h>#include <string.h>#include <algorithm>#include <iostream>using namespace std;const int M=112345;struct node{    int l,r;    int c;    bool flag;  //标记此区间是否为一种颜色覆盖}p[M<<2];void pushup(int root)//位运算求区间颜色种类{    p[root].c=p[root<<1].c|p[root<<1|1].c;}void build(int root,int b,int e){    p[root].l=b;    p[root].r=e;    p[root].c=1;    p[root].flag=1;       if(b==e)return ;    int mid=(b+e)/2;    build(root<<1,b,mid);    build(root<<1|1,mid+1,e);    pushup(root);}void pushdown(int root){    p[root<<1].c=p[root].c;    p[root<<1].flag=1;    p[root<<1|1].c=p[root].c;    p[root<<1|1].flag=1;    p[root].flag=0;}void updata(int root,int b,int e,int c){    if(p[root].l>=b&&p[root].r<=e)    {        p[root].c=c;        p[root].flag=1;        return ;    }    if(p[root].c==c)return ;//剪枝    if(p[root].flag)pushdown(root);     int mid=(p[root].l+p[root].r)/2;    if(mid>=e)        updata(root<<1,b,e,c);    else if(mid<b)        updata(root<<1|1,b,e,c);     else     {         updata(root<<1,b,mid,c);         updata(root<<1|1,mid+1,e,c);     }    pushup(root);}int sum;void query(int root,int b,int e){    if(p[root].l>=b&&p[root].r<=e)    {        sum |=  p[root].c;        return ;    }    if(p[root].flag)    {        sum|=p[root].c;        return ;    }    int mid=(p[root].l+p[root].r)/2;    if(mid>=e)        query(root<<1,b,e);    else if(mid<b)        query(root<<1|1,b,e);    else    {        query(root<<1,b,mid);        query(root<<1|1,mid+1,e);    }}int solve(){    int ans=0;    while(sum)  //求sum中1的个数    {        if(sum&1)            ans++;        sum>>=1;    }    return ans;}void swap(int &a,int &b){    int t;    t=a;    a=b;    b=t;}int main(){    int n,m,q;    while(~scanf("%d%d%d",&n,&m,&q))    {        build(1,1,n);        while(q--)        {            char c[2];            scanf("%s",c);            if(c[0]=='C')            {                int a,b,c;                scanf("%d%d%d",&a,&b,&c);                if(a>b)swap(a,b);                updata(1,a,b,1<<(c-1));            }            else            {                sum=0;                int a,b;                scanf("%d%d",&a,&b);                query(1,a,b);               // printf("%d\n",sum);                printf("%d\n",solve());            }        }    }}



0 0