poj2777 Count Color 2011-12-20

Count Color

Time Limit: 1000MSMemory Limit: 65536K

Total Submissions: 23937Accepted: 7078

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

Source

POJ Monthly--2006.03.26,dodo

______________________________________________________

题目大意:给一个初始为颜色1的板染色,询问一个区间有多少种颜色。

———————————————————————————

颜色最多只有30种,所以需要用一个30位的二进制数表示每段的状态,用线段树修改和查询,基础的线段树水题。= =

______________________________________________________

  1 Program Stone;
  2
  3 var n,o,m,lc,rc,x,ans:longint;
  4
  5     a:array[1..1 shl 18]of longint;
  6
  7     b:array[1..1 shl 18]of boolean;
  8
  9
 10
 11  procedure update(head,tail,num:longint);    //修改
 12
 13  var i,j,k:longint;
 14
 15   begin
 16
 17    if (head>=lc)and(tail<=rc) then          //如果目前访问的区间被所需修改的区间包括,则整个区间标为x色。x为当前要涂成的颜色
 18
 19      begin
 20
 21       a[num]:=1 shl (x-1);
 22
 23       b[num]:=true;                        //b表示该区间是否都为同一种颜色。
 24
 25       exit;                                //因为整个区间修改,所以可以直接返回。
 26
 27      end;
 28
 29    if b[num] then begin
 30
 31                    a[num*2]:=a[num];a[num*2+1]:=a[num];       //将该区间的状态传递给左右孩子区间。
 32
 33                    b[num*2]:=true;b[num*2+1]:=true;
 34
 35                    b[num]:=false;                             //并修改标记。
 36
 37                   end;
 38
 39    k:=(head+tail)div 2;
 40
 41    if k>=rc then update(head,k,num*2) else
 42
 43    if lc>k  then update(k+1,tail,num*2+1) else
 44
 45     begin
 46
 47       update(head,k,num*2);
 48
 49       update(k+1,tail,num*2+1);
 50
 51     end;                                                    //分别访问孩子区间。
 52
 53    a[num]:=a[num*2]or a[num*2+1];                           //使用or计算当前区间的颜色数。
 54
 55   end;
 56
 57
 58
 59  procedure query(head,tail,num:longint);        //询问
 60
 61  var i,j,k:longint;
 62
 63   begin
 64
 65    if (head>=lc)and(tail<=rc) then
 66
 67      begin
 68
 69       ans:=ans or a[num];                      //计算答案
 70
 71       exit;
 72
 73      end;
 74
 75    if b[num] then begin
 76
 77                    a[num*2]:=a[num];a[num*2+1]:=a[num];
 78
 79                    b[num*2]:=true;b[num*2+1]:=true;
 80
 81                    b[num]:=false;
 82
 83                   end;
 84
 85    k:=(head+tail)div 2;
 86
 87    if k>=rc then query(head,k,num*2) else
 88
 89    if lc>k  then query(k+1,tail,num*2+1) else
 90
 91     begin
 92
 93       query(head,k,num*2);
 94
 95       query(k+1,tail,num*2+1);
 96
 97     end;
 98
 99   end;
100
101
102
103  procedure init;
104
105  var i,j,k:longint;
106
107      c,sp:char;
108
109   begin
110
111    fillchar(b,sizeof(b),false);
112
113    a[1]:=1;b[1]:=true;               //初始化,将整块板标为颜色1。
114
115    readln(n,o,m);
116
117     for i:=1 to m do
118
119      begin
120
121       read(c,sp);
122
123       if c=‘C‘ then begin
124
125                       readln(lc,rc,x);
126
127                       if rc<lc then begin
128
129                                      k:=rc;rc:=lc;lc:=k;
130
131                                     end;
132
133                       update(1,n,1);
134
135                     end
136
137                else begin
138
139                       readln(lc,rc);
140
141                       if rc<lc then begin
142
143                                      k:=rc;rc:=lc;lc:=k;
144
145                                     end;
146
147                       ans:=0;
148
149                       query(1,n,1);
150
151                       k:=0;
152
153                       for j:=0 to o-1 do
154
155                        if (ans shr j) and 1=1 then inc(k);   //查看二进制的数有多少位为1,即总共有多少种颜色。
156
157                       writeln(k);
158
159                     end;
160
161      end;
162
163   end;
164
165 Begin
166
167  assign(input,‘poj2777.in‘);reset(input);
168
169   init;
170
171  close(input);
172
173 end.
时间: 2024-08-25 13:24:35

poj2777 Count Color 2011-12-20的相关文章

POJ2777 Count Color 线段树区间更新

题目描述: 长度为L个单位的画板,有T种不同的颜料,现要求按序做O个操作,操作分两种: 1."C A B C",即将A到B之间的区域涂上颜色C 2."P A B",查询[A,B]区域内出现的颜色种类 出现操作2时,请输出答案 PS:初始状态下画板颜色为1 一开始没有想那么好,用int整型位移来代替颜色,还是使用了最传统的bool color[来记录,可是不知道错在了哪里, #include<iostream> #include<cstdio>

poj2777( Count Color)

题目地址:Count Color 题目大意: 给一个划分为L的线段染色,有两种操作,一种C操作 给定l,r区间染色为val.另一种操作P 查询l,r区间的颜色有多少种. 解题报告: 线段树,区间更新. 这题的lazy 表示该区间颜色种类,如果单色则为“1”,如果多色为”0“.tag 代表该区间的哪一种颜色.如果修改区间的颜色时,判断修改的颜色和该区间的颜色是否相同,相同的话就return,如果直接找到该区间,直接lazy赋值为”1“,tag 赋值为”v“,不用往下递归,因为该区间包含下面的子区间

poj 2777 Count Color(线段树、状态压缩、位运算)

Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38921   Accepted: 11696 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.

12.19&amp;12.20 -基础命令练习二

12.19&12.20 基础命令练习二 第1章 Linux开机启动过程 linux启动过程 1.开启开关 2.bios开机自检 3.mbr引导 4.grub菜单 选择内核 5.加载内核 6.启动init进程  init进程是linux启动的时候运行的第一个进程 7.从/etc/inittab读取运行级别 8.根据/etc/rc.d/rc.sysinit 初始化系统 (设置主机名 ip地址) 9.根据运行级别启动对应的软件(开机自启动软件) 10.运行mingetty 显示登录界面 第2章 PAT

人脸识别常用数据集大全(12/20更新)

人脸识别常用数据集大全(12/20更新) 2018-05-18 16:53:37 meng_shangjy 阅读数 2807 1.PubFig: Public Figures Face Database(哥伦比亚大学公众人物脸部数据库) The PubFig database is a large, real-world face dataset consisting of 58,797 images of 200 people collected from the internet. Unli

12.17 Nginx负载均衡;12.18 ssl原理;12.19 生产ssl密钥对;12.20 Nginx配置ssl

扩展: 针对请求的uri来代理 http://ask.apelearn.com/question/1049 根据访问的目录来区分后端web http://ask.apelearn.com/question/920 12.17 Nginx负载均衡 1. 安装dig命令: [[email protected] ~]# yum install -y bind-utils 2. 用dig获取qq.com的ip地址: [[email protected] ~]# dig qq.com 3. 创建ld.co

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

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