码迷,mamicode.com
首页 > 其他好文 > 详细

Count Color

时间:2014-09-20 17:03:49      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   div   

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
bubuko.com,布布扣
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 
  5 using namespace std;
  6 
  7 const int N=100010;
  8 
  9 #define L(rt) (rt<<1)
 10 #define R(rt) (rt<<1|1)
 11 
 12 struct Tree{
 13     int l,r;
 14     int col;    //  用一个32位的int型,每一位对应一种颜色,用位运算代替bool col[32]
 15     bool cover; //  表示这个区间都被涂上同一种颜色,线段树效率的体现,否则插入就是0(n)了。
 16 }tree[N<<2];
 17 
 18 void PushUp(int rt){    // 最后递归回来再更改父节点的颜色
 19     tree[rt].col=tree[L(rt)].col | tree[R(rt)].col;
 20 }
 21 
 22 void build(int L,int R,int rt){
 23     tree[rt].l=L;
 24     tree[rt].r=R;
 25     tree[rt].col=1; //  开始时都为涂有颜色1,看题要仔细,要注意状态。
 26     tree[rt].cover=1;
 27     if(tree[rt].l==tree[rt].r)
 28         return ;
 29     int mid=(L+R)>>1;
 30     build(L,mid,L(rt));
 31     build(mid+1,R,R(rt));
 32 }
 33 
 34 void PushDown(int rt){  //  延迟覆盖的操作
 35     tree[L(rt)].col=tree[rt].col;
 36     tree[L(rt)].cover=1;
 37     tree[R(rt)].col=tree[rt].col;
 38     tree[R(rt)].cover=1;
 39     tree[rt].cover=0;
 40 }
 41 
 42 void update(int val,int L,int R,int rt){
 43     if(L<=tree[rt].l && R>=tree[rt].r){
 44         tree[rt].col=val;
 45         tree[rt].cover=1;
 46         return ;
 47     }
 48     if(tree[rt].col==val)  //剪枝
 49         return ;
 50     if(tree[rt].cover)
 51         PushDown(rt);
 52     int mid=(tree[rt].l+tree[rt].r)>>1;
 53     if(R<=mid)
 54         update(val,L,R,L(rt));
 55     else if(L>=mid+1)
 56         update(val,L,R,R(rt));
 57     else{
 58         update(val,L,mid,L(rt));
 59         update(val,mid+1,R,R(rt));
 60     }
 61     PushUp(rt);      // 最后递归回来再更改父节点的颜色
 62 }
 63 
 64 int sum;
 65 
 66 void query(int L,int R,int rt){
 67     if(L<=tree[rt].l && R>=tree[rt].r){
 68         sum |= tree[rt].col;
 69         return ;
 70     }
 71     if(tree[rt].cover){     //  这个区间全部为1种颜色,就没有继续分割区间的必要了
 72         sum |= tree[rt].col;     //  颜色种类相加的位运算代码
 73         return;
 74     }
 75     int mid=(tree[rt].l+tree[rt].r)>>1;
 76     if(R<=mid)
 77         query(L,R,L(rt));
 78     else if(L>=mid+1)
 79         query(L,R,R(rt));
 80     else{
 81         query(L,mid,L(rt));
 82         query(mid+1,R,R(rt));
 83     }
 84 }
 85 
 86 int solve(){
 87     int ans=0;
 88     while(sum){
 89         if(sum&1)
 90             ans++;
 91         sum>>=1;
 92     }
 93     return ans;
 94 }
 95 
 96 void swap(int &a,int &b){
 97     int tmp=a;a=b;b=tmp;
 98 }
 99 
100 int main(){
101 
102     //freopen("input.txt","r",stdin);
103 
104     int n,t,m;
105     while(~scanf("%d%d%d",&n,&t,&m)){
106         build(1,n,1);
107         char op[3];
108         int a,b,c;
109         while(m--){
110             scanf("%s",op);
111             if(op[0]==C){
112                 scanf("%d%d%d",&a,&b,&c);
113                 if(a>b)
114                     swap(a,b);
115                 update(1<<(c-1),a,b,1); // int型的右起第c位变为1,即2的c-1次方。
116             }else{
117                 scanf("%d%d",&a,&b);
118                 if(a>b)
119                     swap(a,b);
120                 sum=0;
121                 query(a,b,1);
122                 printf("%d\n",solve());
123             }
124         }
125     }
126     return 0;
127 }
View Code

 

Count Color

标签:des   style   blog   http   color   io   os   ar   div   

原文地址:http://www.cnblogs.com/767355675hutaishi/p/3983336.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!