Count Color

Time Limit: 1000MS Memory Limit: 65536K

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:

  • “C A B C” Color the board from segment A to segment B with color C.
  • “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


解题心得:

  1. 一看就是线段树,但是还加了状压,每一段区间用一个二进制来表示其涂抹情况,假如二进制的第一位为1代表第一种颜色涂抹了,然后父节点等于两个子节点的状态或起来,询问的区间用0去线段树中或,然后数1的个数就行了。区间更新的时候lazy标记一下,但是要注意的是颜色是直接覆盖。
  2. 读题眼贱了一下,没看到给的left端和right端可能是交换的,然后一直Runtime Error,怀疑人生啊。

#include<stdio.h>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn = 1e6+100;
struct Bitree
{
int l,r;
int co;
} bitree[maxn<<2];
int lazy[maxn<<2];
int n,a,b,c,o,t,ans; void updata(int rt)//向上更新
{
bitree[rt].co = (bitree[rt<<1].co)|(bitree[rt<<1|1].co);//字节点或起来
} void build_tree(int rt,int l,int r)//先初始化一棵树
{
bitree[rt].l = l;
bitree[rt].r = r;
bitree[rt].co |= 2;
if(r == l)
return ;
int mid = (l + r)>>1;
build_tree(rt<<1,l,mid);
build_tree(rt<<1|1,mid+1,r);
updata(rt);
} void pushdown(int rt)//向下更新,注意lazy标记的转移方式就可以了
{
if(bitree[rt].l == bitree[rt].r || !lazy[rt])
return ;
bitree[rt<<1].co = bitree[rt<<1|1].co = 0;
bitree[rt<<1].co |= (1<<lazy[rt]);
bitree[rt<<1|1].co |= (1<<lazy[rt]);
lazy[rt<<1] = lazy[rt<<1|1] = lazy[rt];
lazy[rt] = 0;
} void make_lazy(int rt,int l,int r,int L,int R)//区间更新,lazy标记
{
pushdown(rt);
if(l == L && r == R)
{
lazy[rt] = c;
bitree[rt].co = 0;
bitree[rt].co |= (1<<c);
return ;
}
int mid = (L + R) >> 1;
if(mid >= r)
make_lazy(rt<<1,l,r,L,mid);
else if(mid < l)
make_lazy(rt<<1|1,l,r,mid+1,R);
else
{
make_lazy(rt<<1,l,mid,L,mid);
make_lazy(rt<<1|1,mid+1,r,mid+1,R);
}
updata(rt);
} void query(int rt,int l,int r,int L,int R)
{
pushdown(rt);
if(L == l && R == r)
{
ans |= bitree[rt].co;//查询的时候用ans去将线段树中的状态或出来
return ;
}
int mid = (L + R) >> 1;
if(mid >= r)
query(rt<<1,l,r,L,mid);
else if(mid < l)
query(rt<<1|1,l,r,mid+1,R);
else
{
query(rt<<1,l,mid,L,mid);
query(rt<<1|1,mid+1,r,mid+1,R);
}
updata(rt);
} int SUM(int x)
{
int sum = 0;
while(x)
{
if(x&1)
sum++;
x >>= 1;
}
return sum;
} int main()
{
while(cin>>n>>t>>o)
{
memset(lazy,0,sizeof(lazy));
memset(bitree,0,sizeof(bitree));
build_tree(1,1,n);
while(o--)
{
char s[10];
scanf("%s",s);
if(s[0] == 'C')
{
scanf("%d%d%d",&a,&b,&c);
if(a>b)//注意交换啊,坑死了
swap(a,b);
make_lazy(1,a,b,1,n);
}
else if(s[0] == 'P')
{
ans = 0;
scanf("%d%d",&a,&b);
if(a>b)
swap(a,b);
query(1,a,b,1,n);
ans = SUM(ans);//数最终有多少个1的个数
printf("%d\n",ans);
}
}
}
return 0;
}

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 - 线段树 - 位运算优化

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

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

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

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

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

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

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

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

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

  8. poj2777Count Color——线段树+状压

    题目:http://poj.org/problem?id=2777 状压每个颜色的选择情况,取答案时 | 一番: 注意题目中的区间端点可能大小相反,在读入时换一下位置: 注意pushdown()中要l ...

  9. POJ 2777 Count Color(段树)

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

  10. poj 2777 Count Color

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

随机推荐

  1. python 6 循环

    循环 要计算1+2+3,我们可以直接写表达式: >>> 1 + 2 + 3 6 要计算1+2+3+...+10,勉强也能写出来. 但是,要计算1+2+3+...+10000,直接写表 ...

  2. 《深入理解java虚拟机》笔记(8)类的加载机制

    一.类加载机制 类加载器将类的.class文件中的二进制数据读入到内存中,将其放在方法区内,然后在堆区创建一个java.lang.Class对象,用来封装类在方法区内的数据结构.类的加载的最终产品是位 ...

  3. springmvc httprequest 使用@Autowired注解

    springmvc httprequest 使用@Autowired注解我一直有个疑问,就是注解后每次的httprequest 是不是都一样的了,然后会不会引发多线程问题? 代码如下: import ...

  4. 结合源码看nginx-1.4.0之nginx内存管理详解

    目录 0. 摘要 1. nginx内存结构设计 2. nginx内存数据结构 3. nginx内存管理原理 4. 一个简单的内存模型 5. 小结 6. 参考资料 0. 摘要 内存管理,是指软件运行时对 ...

  5. IO(字节流、字符流)

      第1章 字节流 在前面的学习过程中,我们一直都是在操作文件或者文件夹,并没有给文件中写任何数据.现在我们就要开始给文件中写数据,或者读取文件中的数据. 1.1 字节输出流OutputStream ...

  6. <Android 应用 之路> 天气预报(三)

    昨天介绍了基本的载入界面,今天介绍下天气信息显示界面的代码 基本ListView显示 搜索框,查询城市 上一篇文章中,载入界面通过showWeatherInfo()方法跳转到天气信息显示界面 priv ...

  7. 【转】Java Cipher类 DES算法(加密与解密)

    Java Cipher类 DES算法(加密与解密) 1.加密解密类 import java.security.*; import javax.crypto.*; import java.io.*; / ...

  8. Lodash.js常用拷贝

    lodash.js 降低 array.number.objects.string 等等的使用难度从而让 JavaScript 变得更简单.非常适用于:遍历 array.object 和 string: ...

  9. This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms while caching

    今天运行自己的网站时报了这样一个错误,很是纳闷,这个网站运行了这么久,怎么报这个错呢,原来是做缓存的时候用到了基于windows平台的加密算法.解决方法如下: 删除注册表下的这个节点即可.删除HKEY ...

  10. python爬虫之路——Python的re模块及其方法

    介绍常用的三种方法:search(),sub(),findall() search():匹配并提取第一个符合规律的内容,然后返回一个正则表达式的对象 #提取字符串中的第一个数字 import re a ...