POJ 2777 Count Color(线段树染色,二进制优化)

时间:2022-12-06 01:18:57
Count Color
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 42940   Accepted: 13011

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 2777

大致题意就是区间染色覆盖,区间查询出现的颜色种类。跟以往的普通线段树倒是有点不一样,就是如何进行颜色的记录问题,看了一下别人的做法,就是用2的幂数来代表颜色,由于题目中颜色小于等于30种,小于log2(INT_MAX),可以存的下,那怎么表示呢?看下面的表格

1 1
2 10
3 100
4 1000
5 10000
6 100000
.. .....
.. .....
.. .....
30 100000..(29个0)

由以上表格可以看出,某种颜色用2的幂来表示相当于二进制的1和(颜色标号-1)个0,比如6用2的幂表示就是1和5个0。

那为什么要用这个表示呢?因为后面还要用到按位或“|”运算,即重复的二进制中的1也只算一个,只要出现过,就能算计去,出现两次,也只会按位或得到一个1,比如一棵树有5(表示为10000)和2(表示为10),按位或后就得到10010,其中的1的个数就是颜色个数,不信再试试,再和颜色2按位或(表示为10),还是10010,这显然是符合同色只算一次的情况的,然后就稍微改写一下向上更新、向下更新函数,就差不多了,计算出最后的query结果中1的个数也就是答案了。用了bitset的简化求二进制1个数的写法挺方便

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<bitset>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=100010;
struct seg
{
int l,mid,r;
int color;
int add;
}T[N<<2];
void pushup(int k)
{
T[k].color=T[LC(k)].color|T[RC(k)].color;
}
void pushdown(int k)
{
if(T[k].add)
{
T[LC(k)].color=T[k].add;
T[RC(k)].color=T[k].add;
T[LC(k)].add=T[k].add;
T[RC(k)].add=T[k].add;
T[k].add=0;
}
}
void build(int k,int l,int r)
{
T[k].l=l;
T[k].r=r;
T[k].mid=MID(l,r);
T[k].add=0;
T[k].color=0;
if(l==r)
{
T[k].color=1;
return ;
}
build(LC(k),l,T[k].mid);
build(RC(k),T[k].mid+1,r);
pushup(k);
}
void update(int k,int l,int r,int color)
{
if(r<T[k].l||l>T[k].r)
return ;
if(l<=T[k].l&&r>=T[k].r)
{
int bina_val=1<<(color-1);
T[k].color=T[k].add=bina_val;
return ;
}
pushdown(k);
if(l<=T[k].mid)
update(LC(k),l,r,color);
if(r>T[k].mid)
update(RC(k),l,r,color);
pushup(k);
}
int query(int k,int l,int r)
{
if(l<=T[k].l&&T[k].r<=r)
return T[k].color;
else
{
pushdown(k);
if(r<=T[k].mid)
return query(LC(k),l,r);
else if(l>T[k].mid)
return query(RC(k),l,r);
else
return query(LC(k),l,T[k].mid)|query(RC(k),T[k].mid+1,r);
}
}
int main(void)
{
int L,t,O;
int i,j,l,r,c;
char ops[3];
while (~scanf("%d%d%d",&L,&t,&O))
{
build(1,1,L);
for (i=0; i<O; ++i)
{
scanf("%s",ops);
if(ops[0]=='C')
{
scanf("%d%d%d",&l,&r,&c);
if(l>r)
swap(l,r);
update(1,l,r,c);
}
else
{
scanf("%d%d",&l,&r);
if(l>r)
swap(l,r);
printf("%d\n",bitset<32>(query(1,l,r)).count());
}
}
}
return 0;
}

POJ 2777 Count Color(线段树染色,二进制优化)的更多相关文章

  1. poj 2777 Count Color - 线段树 - 位运算优化

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42472   Accepted: 12850 Description Cho ...

  2. poj 2777 Count Color&lpar;线段树区区&plus;染色问题&rpar;

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

  3. poj 2777 Count Color&lpar;线段树&rpar;

    题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  4. POJ 2777 Count Color &lpar;线段树成段更新&plus;二进制思维&rpar;

    题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

  5. poj 2777 Count Color&lpar;线段树、状态压缩、位运算&rpar;

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38921   Accepted: 11696 Des ...

  6. POJ 2777 Count Color&lpar;线段树之成段更新&rpar;

    Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...

  7. POJ P2777 Count Color——线段树状态压缩

    Description Chosen Problem Solving and Program design as an optional course, you are required to sol ...

  8. POJ 2777 Count Color&lpar;段树)

    职务地址:id=2777">POJ 2777 我去.. 延迟标记写错了.标记到了叶子节点上.. . . 这根本就没延迟嘛.. .怪不得一直TLE... 这题就是利用二进制来标记颜色的种 ...

  9. poj 2777 Count Color

    题目连接 http://poj.org/problem?id=2777 Count Color Description Chosen Problem Solving and Program desig ...

随机推荐

  1. web预设模块化

  2. XML 概述 (可扩展标记语言)

    XML:eXtensible Markup Language  可扩展标记语言 概念:可扩展:xml中所有的标签都是自定义的.没有预定义的.        功能:            存储数据   ...

  3. 做WEB开发的时候,前端与后端我们应该要注意哪些细节,哪些容易出现的漏洞?

    写这篇文章的时候,我和团队正在处理项目漏洞问题,发现这些都是细节但又容易在项目实现的过程中忽视的部分,鉴于此,我想总结下来,方便以后出现类似问题能及时得到解决. 1.任意文件上传漏洞.   描述:允许 ...

  4. android中的4种点击事件

    四种点击事件     (1)采用内部类的方式去实现OnClickListener      (2)匿名内部类     (3)当前类imp OnClickListener      (4)onclick ...

  5. Svn服务启动的两种方式

    一.svn服务器启动 › cmd命令行启动:vsvnserve -d –r 文档仓库路径 -d 后台执行 › -r 版本库的根目录 二.›Windows服务自动启动     利用xp.2000 以上的 ...

  6. Spring MVC Ajax 复杂参数的批量传递

    要解决的问题: 如何组织客户端参数? Ajax 方法的配置属性如何定义才能传递复杂参数? 基于 SpringMVC 的服务端该如何接收? MyBatis 怎么处理批量更新? 客户端脚本 viewMes ...

  7. 将TinyXml快速入门的接口面向对象化(转载)

    作者:朱金灿 来源:http://www.cnblogs.com/clever101 在TinyXml快速入门的系列文章中(详情见本博客),我只是将tinyxml类库解析xml文件的类封装为API接口 ...

  8. Django 学生管理系统

    1. 一对一 班级  模态增加 编辑 def classes(request): data = sqlheper.get_list("select cid,title from class& ...

  9. Python的开发之路

    一.python入门 二.基本数据类型 三.输入与输出 四.基本运算符 五 .流程控制之if判断 六.流程控制之while循环 七.流程控制之for循环 八.数据类型与内置方法 九.文件的处理 十.字 ...

  10. BZOJ3714 PA2014 Kuglarz 最小生成树

    题目传送门 题意:有$N$个盒子,每个盒子中有$0$或$1$个球.现在你可以花费$c_{i,j}$的代价获得$i$到$j$的盒子中球的总数的奇偶性,求最少需要多少代价才能知道哪些盒子中有球.$N \l ...