poj 2777 Count Color(线段树区间更新+位运算)

来源:互联网 发布:js url encode php 编辑:程序博客网 时间:2024/06/18 12:24

问题描述

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.

输入

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.

输出

Ouput results of the output operation in order, each line contains a number.

样例输入

2 2 4C 1 1 2P 1 2C 2 2 2P 1 2

样例输出

21


因为颜色的个数不是很大,所以可以用位运算来存储状态。

#include <iostream>#include <stdio.h>#include <algorithm>#include <cstring>using namespace std;const int maxx=100005;int ans;struct node{    int l,r,c;    int f;}no[maxx<<2];void pushdown(int rt){    no[rt<<1].f=no[rt].f;    no[rt<<1|1].f=no[rt].f;    no[rt<<1|1].c=no[rt].c;    no[rt<<1].c=no[rt].c;    no[rt].f=0;}void build(int l,int r,int rt){    int mid=(l+r)>>1;    no[rt].l=l;    no[rt].r=r;    no[rt].c=1;    no[rt].f=0;    if(l==r)    {        return ;    }    build(l,mid,rt<<1);    build(mid+1,r,rt<<1|1);}void updata(int l,int r,int k,int rt){    int mid=(no[rt].l+no[rt].r)>>1;    if(no[rt].l==l&&no[rt].r==r)    {        no[rt].f=1;        no[rt].c=1<<(k-1);        return ;    }    if(no[rt].f>0)pushdown(rt);    if(mid>=r)        updata(l,r,k,rt<<1);    else if(mid<l)        updata(l,r,k,rt<<1|1);    else    {        updata(l,mid,k,rt<<1);        updata(mid+1,r,k,rt<<1|1);    }    no[rt].c=no[rt<<1].c|no[rt<<1|1].c;}void query1(int l,int r,int rt){    if(no[rt].f>0||(no[rt].l==l&&no[rt].r==r))    {        //cout<<l<<" "<<r<<" "<<no[rt<<1].c<<" "<<no[rt<<1|1].c<<endl;        ans|=no[rt].c;        return ;    }//if(no[rt].f>0)pushdown(rt);    int mid=(no[rt].l+no[rt].r)>>1;    if(mid>=r)        query1(l,r,rt<<1);    else if(mid<l)        query1(l,r,rt<<1|1);    else    {        query1(l,mid,rt<<1);        query1(mid+1,r,rt<<1|1);    }}int main(){    int n,t,o;    scanf("%d %d %d",&n,&t,&o);    build(1,n,1);    for(int i=1;i<=o;i++)    {        char a;int l,r,k;       cin>>a;       scanf("%d %d",&l,&r);        if(a=='C')        {            scanf("%d",&k);            if(l>r)            {                int te=l;                l=r;                r=te;            }            updata(l,r,k,1);        }        if(a=='P')        {            if(l>r)            {                int te=l;                l=r;                r=te;            }            ans=0;            query1(l,r,1);            int ans1=0;            for(int i=1;i<=30;i++)            {                if(1<<(i-1)&ans)                    ans1++;            }            printf("%d\n",ans1);        }    }}


0 0