Count Color
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 53639   Accepted: 16153

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 题目链接: http://poj.org/problem?id=2777 题目大意: 给L长度的木板涂色,一开始木板颜色相同,有T种颜色可用,O(不是零是 ou )次操作。每次操作有两种情况,一种是改变木板的颜色也就是给木板的某个区间涂色,另一种是询问给定区间的颜色种类数量。 题目分析: 这题比较明显是使用线段树解决问题,但是有一个问题是:超时。那么如何解决这个问题呢,这时候就引进了一个很优秀的操作————懒标记。 懒标记是什么呢?“懒”的意思很明显了,就是说在不涉及某个节点的情况下,选择性的变懒,不去改变。举个例子: 如果改变区间的颜色,那么我们可以不去找到区间内每个叶子然后改变每个叶子的颜色,而是找到在这个区间内的另外的区间(可能是一个区间,也可能是很多个区间,也有可能包括了一个区间和n个叶子,也有可能只有叶子),把找到的区间改变颜色,并把区间懒标记。 懒标记后,如果再次查询到该节点,那么需要做下推操作,即把该节点的颜色,下推给子节点,改变子节点的颜色,并把该节点懒标记清空。 对于颜色的存储,这题还是需要注意的。我们很容易想到存储每个节点区间的颜色种类数,但是这样存储问题就来了,怎么确定子节点中颜色是否相同? 所以有引进一个操作,二进制表示法。 我们可以看到颜色的种类不超过30种,所以可以用8位二进制表示一种颜色,即颜色一为(1),颜色二为(10),颜色三为(100),颜色四为(1000),依次类推。那么,表示当前节点有颜色一和颜色四就为1001。 那么我们存储父节点颜色怎么存储呢? 或操作(|)。
tree[k].color=tree[k<<].color|tree[k<<|].color;

解释一下:或操作,如果颜色相同,那么结果仍保存该颜色;如果子节点一个有该颜色,另一个没有该颜色,操作结果仍然有该颜色;如果两个子节点都没有该颜色,那么结果也没有该颜色。

最后得到的结果只需要判断在二进制下有几个1就可以了。

还要注意一个坑就是 A和B大小不确定。

AC代码如下:
#include<stdio.h>

const int MAX=1e6+;

struct a{
int left,right;
int lazy,ans;
}tree[MAX<<]; void init(int length) //懒标记初始化
{
for(int i=;i<=length;i++)
{
tree[i].lazy=;
}
} void btree(int k,int l,int r) //建树操作
{
tree[k].left=l;tree[k].right=r;
if(l==r)
{
tree[k].ans=;return ;
}
int mid=(l+r)>>;
btree(k<<,l,mid);
btree(k<<|,mid+,r);
tree[k].ans=(tree[k<<].ans|tree[k<<|].ans);
} void push_down(int k) //下推
{
if(tree[k].left==tree[k].right) return ;
tree[k<<].lazy=tree[k].lazy;
tree[k<<|].lazy=tree[k].lazy;
tree[k<<].ans=<<(tree[k].lazy-);
tree[k<<|].ans=<<(tree[k].lazy-);
tree[k].lazy=;
} void datetree(int k,int l,int r,int co) //涂色
{
if(tree[k].left>=l&&tree[k].right<=r)
{
tree[k].ans=<<(co-);
tree[k].lazy=co;
return ;
}
if(tree[k].lazy) push_down(k);
int mid=(tree[k].left+tree[k].right)>>;
if(mid<l)
{
datetree(k<<|,l,r,co);
}
else if(mid>=r)
{
datetree(k<<,l,r,co);
}
else
{
datetree(k<<,l,r,co);
datetree(k<<|,l,r,co);
}
tree[k].ans=(tree[k<<].ans|tree[k<<|].ans);
} int ans;
void query(int k,int l,int r) //询问
{
if(tree[k].left>=l&&tree[k].right<=r)
{
ans=(ans|tree[k].ans);
return ;
}
if(tree[k].lazy) push_down(k);
int mid=(tree[k].left+tree[k].right)>>;
if(mid<l)
{
query(k<<|,l,r);
}
else if(mid>=r)
{
query(k<<,l,r);
}
else
{
query(k<<,l,r);
query(k<<|,l,r);
}
} int main()
{
int L,color,q;
char n[];
scanf("%d%d%d",&L,&color,&q);
init(L);
btree(,,L);
while(q--)
{
scanf("%s",n);
if(n[]=='C')
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a>b){
int t=a;a=b;b=t;
}
datetree(,a,b,c); }
else
{
int a,b;
ans=;
int cnt=;
scanf("%d%d",&a,&b);
if(a>b){
int t=a;a=b;b=t;
}
query(,a,b);
while(ans)
{
if(ans%) cnt++;
ans/=;
}
printf("%d\n",cnt);
}
}
return ;
}

POJ - 2777——Count Color(懒标记线段树二进制)的更多相关文章

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

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

  2. poj 2777 Count Color(线段树)

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

  3. poj 2777 Count Color

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

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

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42940   Accepted: 13011 Des ...

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

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

  6. POJ 2777 Count Color(线段树 + 染色问题)

    传送门:Count Color Description Chosen Problem Solving and Program design as an optional course, you are ...

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

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

  8. POJ 2777 Count Color(线段树之成段更新)

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

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

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

随机推荐

  1. Ubuntu 下将 svg 图片转换为其他格式 (如 png)

    参考 How to Convert SVG Files to other Image Formats on Ubuntu 12.04/11.10 Ubuntu 下将 svg 图片转换为其他格式 (如 ...

  2. C语言 Struct 结构体在 Java 中的体现

    大一整个学期完成了 C 语言的学习,大二就进入了Java 的学习. 和C语言一样,我们都会尝试写一个小小的学生管理系统什么的,学习过 C 语言同学知道,在管理系统中 Struct 结构体是个很好用的东 ...

  3. H5外包团队 技术分享 基于H5+的项目分享

    项目截图:             有H5项目需求欢迎联系我们 我们提供免费的项目评估报价 QQ:372900288 WX:Liuxiang0884

  4. 区间DP 基本题集

    51 Nod 1021 石子归并 模板题,敲就完事了,注意一下这种状态转移方程有个四边形的优化(时间) #include <cstdio> #include <iostream> ...

  5. locate 和 find

    因工作需求,需要查找到系统内是否有 密钥文件 id_rsa 或 id_dsa;想到两个工具find 和 locate find 可以对指定范围内的文件进行实时查找,但是 考虑到这种方法非常耗时. lo ...

  6. 如何为Rails作贡献:例增加rich_text field generators

    如何为Rails作贡献 例增加rich_text field generators 下载https://github.com/rails/rails 打开atom,在 rails/railties/l ...

  7. CNN 分割

    测试的是Cifar10数据集,采用VGG模型的网络参数是[32, 'M', 64, 'M', 128, 128, 'M', 256, 256, 'M', 256, 256],准确度大概在90.6左右 ...

  8. C++ for循环语句

    #include "pch.h" #include<iostream> using namespace std; int main() { int i = 1, sum ...

  9. JUC原子类--01

    JUC原子操作类分为四种类型 1. 基本类型: AtomicInteger, AtomicLong, AtomicBoolean ;2. 数组类型: AtomicIntegerArray, Atomi ...

  10. ES9新内容概括

    本文主要介绍ES2018 新出的一些特性 1.异步迭代 允许 async/await 与 for-of 一起使用,以串行的方式运行异步操作,达到异步函数的迭代效果. async function pr ...