• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Just Fools.
♕Heart-broken is just a fool.
   首页    新随笔    联系   管理    订阅  订阅

POJ 2777 Count Color

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


线段树区间更新
代码如下:
  1 #include<cstdio>
  2 #include<iostream>
  3 #include<cmath>
  4 #include<cstring>
  5 using namespace std;
  6 struct wakaka{
  7     int l,r,num;
  8 }tree[5000000];
  9 bool b[31],bo;
 10 int n,m,x,ll,rr,v,ans,t;
 11 char ch;
 12 int maxx(int a,int b){
 13     if (a>b)
 14     return a;
 15     else
 16     return b;
 17 }
 18 void build(int l,int r,int i){
 19     tree[i].l=l;
 20     tree[i].r=r;
 21     tree[i].num=1;
 22     if (l!=r){
 23         int mid=(l+r)>>1;
 24         build(l,mid,i*2);
 25         build(mid+1,r,i*2+1);
 26     }
 27 }
 28 void change(int i,int l,int r,int v){
 29     
 30     if(tree[i].num==v)
 31     return ;
 32     int ll=tree[i].l;
 33     int rr=tree[i].r;
 34     if (ll==l&&rr==r){
 35         tree[i].num=v;
 36         return ;
 37     }
 38     if (tree[i].num!=-1)
 39         {
 40             tree[i*2].num=tree[i*2+1].num=tree[i].num;
 41             tree[i].num=-1;
 42         }
 43     
 44     int mid=(ll+rr)>>1;
 45     if (l>mid)
 46     change(2*i+1,l,r,v);
 47     else if (mid>=r)
 48     change(2*i,l,r,v);
 49     else {
 50         change(2*i,l,mid,v);
 51         change(2*i+1,mid+1,r,v);
 52     }
 53 }
 54 void search1(int i,int l,int r)
 55 {
 56     if (tree[i].num!=-1)
 57     {
 58         b[tree[i].num]=1;
 59         return ;
 60     }
 61     int mid=tree[i].l+tree[i].r;
 62     mid/=2;
 63     if (l>mid)
 64     search1(i*2+1,l,r);
 65     else if (r<=mid)
 66     search1(i*2,l,r);
 67     else{
 68         search1(i*2,l,mid);
 69         search1(i*2+1,mid+1,r);
 70     }
 71 }
 72 
 73 int main(){
 74     bo=0;
 75     scanf("%d %d %d",&n,&t,&m);
 76     build(1,n,1);
 77     for (int i=1;i<=t;++i){
 78         b[i]=0;
 79     }
 80     for (int i=1;i<=m;++i){
 81         scanf("%c",&ch);
 82         while(ch!='C'&&ch!='P'){
 83             scanf("%c",&ch);
 84         }
 85         if (ch=='C'){
 86             scanf("%d %d %d",&ll,&rr,&x);
 87             change(1,min(ll,rr),max(ll,rr),x);    
 88      }
 89      else {
 90          ans=0;
 91          scanf("%d %d",&ll,&rr);
 92          search1(1,min(ll,rr),max(ll,rr));
 93          for (int j=1;j<=t;++j)
 94          {
 95              if (b[j]==1)
 96              ans++;
 97              b[j]=0;
 98          }
 99          printf("%d\n",ans);
100      }
101     }
102    
103     return 0;
104 }

 

posted @ 2016-03-03 15:04  ♕Heart-broken  阅读(196)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3