poj2777 Count Color

原创 2016年05月30日 20:37:23

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.

题解:

令线段树节点定义为: l,r,value,flag分被表示区间左右端点,该区间的涂色为value,flag为标记量,true表示该区间涂色为最新的涂色,否则表示该区间的子区间有更新的涂色。由于初始时均涂色为1,故value的值均为1,flag均为true(即都是最新的涂色)。本题的难点在于更新和查找。

代码:

with tree[p] do  
    begin  
      if (l=b) and (r=e) then  
        begin  
          color:=1 shl (c-1);  
          cover:=true;  
          exit;  
        end;  
      if cover then  
        begin  
          cover:=false;  
          tree[p*2].cover:=true;  
          tree[p*2].color:=color;  
          tree[p*2+1].cover:=true;  
          tree[p*2+1].color:=color;  
        end;  
      m:=(l+r) div 2;  
      if e<=m then ins(p*2,b,e,c) else  
        if b>m then ins(p*2+1,b,e,c) else  
          begin  
            ins(p*2,b,m,c);  
            ins(p*2+1,m+1,e,c);  
          end;  
      color:=tree[p*2].color or tree[p*2+1].color;  
    end;  

版权声明:要转载的“汪”一声,价钱面谈。

POJ 2777 Count Color 【线段树 区间更新 按位或运算】

Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40183   Accepted: 121...
  • wyjwyl
  • wyjwyl
  • 2015年11月11日 22:13
  • 261

poj 2777 Count Color 【线段树lazy区间染色 + 查询区间颜色数目 + 状态压缩】

Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39950   Accepted: 120...

poj 2777 Count Color(线段树)

传送门:点击打开链接 题目大意: 给定一个区间,出

poj 2777 Count Color 线段树成段更新区间统计

Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31008   Accepted: 929...

POJ 2777 Count Color(线段树 )

Language: Default Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Su...

POJ 2777 Count Color (二进制或 线段树)

由于T不超过30,可以用一个数表示当前区间的已有颜色,异或操作来pushup每一个区间 #include #include #include #include #include #include ...

POJ2777 Count Color【线段树】

很显然,这是一道线段树的题,但难点就是怎么保存区间的颜色数,想了很久,曾经考虑过二进制,可是我不太懂二进制,所以就不想了,其实不用二进制也可以,可是脑子笨,不会用数组来保存颜色数,到现在还是不太会,,...

poj 2777 Count Color(线段树区区+染色问题)

题目链接:  poj 2777 Count Color 题目大意:  给出一块长度为n的板,区间范围[1,n],和m种染料                     k次操作,C  a  b  c ...

POJ 2777 Count Color 线段树无位运算

POJ 2777 Count Color 线段树无位运算
  • NNDXNM
  • NNDXNM
  • 2015年04月07日 22:14
  • 284

poj 2777 Count Color

第一道线段树 简单线段树 只要修改其插入操作即可 #include #include #define maxn 700000 using namespace std; int n,t...
  • pbj1203
  • pbj1203
  • 2011年03月14日 22:03
  • 313
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj2777 Count Color
举报原因:
原因补充:

(最多只允许输入30个字)