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.

简而言之,一条一维的线,反复地给某一连续段涂色,后涂的色覆盖先涂的色,要求一边涂一边随时接受问询,回答某一段有多少种颜色。

很明显的线段树题。

几个key point:一、由于颜色数量比较少,利用了2进制来存储数据。cnt【x】的二进制表达中的第i位为1代表这段板上有第i种颜色。

二、lazy数组,当一整块涂色的时候先不急着向下更新子节点,把lazy置成1表示子区间未更新。当需要知道的时候再pushdown一层,还需要的话再继续pushdown。很懒吧233.

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 using namespace std;
  5 #define lson l,m,rt<<1
  6 #define rson m+1,r,rt<<1|1
  7 const int MAXN=100010;
  8 int cnt[MAXN<<2];
  9 int lazy[MAXN<<2];
 10
 11 void pushup(int rt)
 12 {
 13     cnt[rt]=cnt[rt<<1]|cnt[rt<<1|1];
 14  //   printf("cnt=%d %d %d %d\n",cnt[rt],cnt[rt<<1],cnt[rt<<1|1],rt<<1|1);
 15 }
 16 void pushdown(int rt)
 17 {
 18     if(lazy[rt])
 19     {
 20         cnt[rt<<1]=cnt[rt];
 21         cnt[rt<<1|1]=cnt[rt];
 22         lazy[rt<<1]=1;
 23         lazy[rt<<1|1]=1;
 24         lazy[rt]=0;
 25     }
 26 }
 27 int query(int a,int b,int l,int r,int rt)
 28 {
 29    // printf("a\n");
 30     //printf("query %d %d %d %d %d\n",a,b,l,r,rt);
 31     if(a<=l&&b>=r) return cnt[rt];
 32     pushdown(rt);
 33     int t1=0,t2=0;
 34     int m=(r+l)>>1;
 35     if(a<=m) t1=query(a,b,lson);
 36     if(b>m) t2=query(a,b,rson);
 37  //   printf("query %d %d %d %d %d t1=%d t2=%d\n",a,b,l,r,rt,t1,t2);
 38     return t1|t2;
 39 }
 40
 41 void update(int a,int b,int c,int l,int r,int rt)
 42 {
 43    // printf("update %d %d %d %d %d %d\n",a,b,c,l,r,rt);
 44     if(a<=l&&b>=r)
 45     {
 46         cnt[rt]=1<<(c-1);
 47         lazy[rt]=1;
 48       //  printf("cnt=%d l=%d r=%d c=%d\n",cnt[rt],l,r,c);
 49         return;
 50     }
 51     pushdown(rt);
 52     int m=(l+r)>>1;
 53     if(a<=m) update(a,b,c,lson);
 54     if(b>m) update(a,b,c,rson);
 55     pushup(rt);
 56 }
 57 int main()
 58 {
 59   //  freopen("in.txt","r",stdin);
 60   //  freopen("out.txt","w",stdout);
 61     int len,numtype,oper,a,b,c,tmp,t,ans;
 62     char type;
 63     scanf("%d%d%d",&len,&numtype,&oper);
 64    for(int i=0;i<MAXN<<2;i++)
 65    {
 66         cnt[i]=1;
 67    }
 68     memset(lazy,0,sizeof(lazy));
 69     for(int i=1;i<=oper;i++)
 70     {
 71         char type[2];
 72         scanf("%s",type);
 73        // printf("%c\n",type[0]);
 74         if(type[0]==‘C‘)
 75         {
 76             scanf("%d%d%d",&a,&b,&c);
 77             if(a>b)
 78             {
 79                 t=a;a=b;b=t;
 80             }
 81             update(a,b,c,1,len,1);
 82         }
 83         else
 84         {
 85             scanf("%d%d",&a,&b);
 86             if(a>b)
 87             {
 88                 t=a;a=b;b=t;
 89             }
 90             tmp=query(a,b,1,len,1);
 91             for(ans=0;tmp>=1;tmp>>=1)
 92             {
 93           //  printf("tmp=%d\n",tmp);
 94                 if(tmp&1) ans++;
 95             }
 96             printf("%d\n",ans);
 97         }
 98     }
 99     return 0;
100 }

时间: 05-28

poj 2777 count color 线段树的相关文章

POJ 2777 Count Color (线段树成段更新+二进制思维)

题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的颜色有几种. 很明显的线段树成段更新,但是查询却不好弄.经过提醒,发现颜色的种类最多不超过30种,所以我们用二进制的思维解决这个问题,颜色1可以用二进制的1表示,同理,颜色2用二进制的10表示,3用100,....假设有一个区间有颜色2和颜色3,那么区间的值为二进制的110(十进制为6).那我们就把

POJ P2777 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 d

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 d

poj 2777 Count Color(线段树区间修改)

题目链接:http://poj.org/problem?id=2777 题目意思:就是问你在询问的区间里有几种不同的颜色 思路:这题和一般的区间修改差不多,但是唯一不同的就是我们要怎么计算有种颜色,所以这时候我们就需要把延时标记赋予不同的意义,当某段区间有多种颜色时就赋值为-1,当为一种颜色时就把它赋值为这个颜色的号数.这儿我们要怎么统计询问区间不同的颜色数叻,为了不重复计算同一种颜色,那么我们就需要用一个数组来标记计算过的颜色,当我们下次遇到时就不需要再次计算了.... 代码核心处就在计数那儿

POJ训练计划2777_Count Color(线段树/成段更新/区间染色)

解题报告 题意: 对线段染色,询问线段区间的颜色种数. 思路: 本来直接在线段树上染色,lz标记颜色.每次查询的话访问线段树,求出颜色种数.结果超时了,最坏的情况下,染色可以染到叶子节点. 换成存下区间的颜色种数,这样每次查询就不用找到叶子节点了,用按位或来处理颜色种数. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace

POJ 2777 Count Color

C - Count Color Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2777 Appoint description:  System Crawler  (2015-07-22) Description Chosen Problem Solving and Program design as an optional cours

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

题目链接:http://poj.org/problem?id=2777 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

POJ 3321 DFS序+线段树

单点修改树中某个节点,查询子树的性质.DFS序 子树序列一定在父节点的DFS序列之内,所以可以用线段树维护. 1: /* 2: DFS序 +线段树 3: */ 4:   5: #include <cstdio> 6: #include <cstring> 7: #include <cctype> 8: #include <algorithm> 9: #include <vector> 10: #include <iostream> 1

POJ 1195 2维线段树(树套树实现) 树状数组

1: #include <stdio.h> 2: #include <string.h> 3: #include <stdlib.h> 4: #include <algorithm> 5: #include <iostream> 6: using namespace std; 7:   8: #define LL(a) a<<1 9: #define RR(a) a<<1|1 10: const int MaxL = 10