POJ 2777 Count Color (线段树)

来源:互联网 发布:mac cp 文件夹 编辑:程序博客网 时间:2024/05/21 00:54

传送门


题面:

Count Color
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 41256 Accepted: 12474

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

题目大意:

    给定长度为L的线段,有两种操作,一、将一段区间染色,二、统计一段区间的颜色种类。


解题:

   看操作可以很明显地看出是线段树。看颜色数只有30种,可以用int表示状态,每一位表示颜色有无。两种颜色合并,可以采用位或操作,最后统计1的个数即可。

坑点:

1.a可能大于b

2.起始颜色是1

3.多样例


代码:

#include <iostream>#include <cstdio>using namespace std;struct SegTree{int l,r,val,lazy;}ST[400005];//建树操作 void build(int i,int le,int ri){  ST[i].l=le;  ST[i].r=ri;  ST[i].val=1;  ST[i].lazy=0;  if(le==ri)return;  int mid=(ST[i].l+ST[i].r)>>1;  build(i<<1,le,mid);  build(i<<1|1,mid+1,ri);}//下放懒标记 void push_down(int i){ST[i<<1].val=ST[i<<1|1].val=ST[i].val;ST[i<<1].lazy=ST[i<<1|1].lazy=ST[i].lazy;ST[i].lazy=0;}//向上更新 void push_up(int i){ST[i].val=ST[i<<1].val|ST[i<<1|1].val;}//更新操作 void update(int i,int le,int ri,int v){if(le==ST[i].l&&ri==ST[i].r){ST[i].val=v;ST[i].lazy=v;return;}int mid=(ST[i].l+ST[i].r)>>1;if(ST[i].lazy)   push_down(i);if(ri<=mid)  update(i<<1,le,ri,v);    else if(le>mid)      update(i<<1|1,le,ri,v);    else    {  update(i<<1,le,mid,v);      update(i<<1|1,mid+1,ri,v);    }    push_up(i);}//查询操作 int query(int i,int le,int ri){    int mid=(ST[i].l+ST[i].r)>>1;if(ST[i].l==le&&ST[i].r==ri)  return ST[i].val;      if(ST[i].lazy)      push_down(i);    if(le>mid)      return query(i<<1|1,le,ri);    else if(ri<=mid)      return query(i<<1,le,ri);else  return query(i<<1,le,mid)|query(i<<1|1,mid+1,ri); }int main(){int L,T,O,a,b,v,ans,cnt;char oper;while(~scanf("%d%d%d",&L,&T,&O)){build(1,1,L);for(int i=0;i<O;i++){scanf(" %c%d%d",&oper,&a,&b);//确保b>a if(a>b){ a^=b; b^=a; a^=b;}if(oper=='C'){scanf("%d",&v);//使对应位为1 update(1,a,b,1<<(v-1));}else{//统计1的个数 cnt=0;ans=query(1,a,b);while(ans){if(ans&1) cnt++;    ans>>=1;}printf("%d\n",cnt);}}}}



0 0
原创粉丝点击