POJ 2777 Count Color(线段树+位运算)
题目链接:http://poj.org/problem?id=2777
Description
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
defined previously.
Output
Sample Input
2 2 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2
Sample Output
2
1
Source
题意:
给一个固定长度为L的画板
有两个操作:
C A B C:区间A--B内涂上颜色C。
P A B:查询区间AB内颜色种类数。
PS:
此题和HDU:5023是类似的!
附题解:http://blog.csdn.net/u012860063/article/details/39434665
代码例如以下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; #define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define LL int const int maxn = 110017;
LL add[maxn<<2];
LL sum[maxn<<2];
void PushUp(int rt)
{
//把当前结点的信息更新到父结点
sum[rt] = sum[rt<<1] | sum[rt<<1|1];//总共的颜色
}
void PushDown(int rt,int m)
{
if(add[rt])
{
add[rt<<1] = add[rt];
add[rt<<1|1] = add[rt];
sum[rt<<1] = add[rt];
sum[rt<<1|1] = add[rt];
add[rt] = 0;//将标记向儿子节点移动后,父节点的延迟标记去掉
//传递后,当前节点标记域清空
}
}
void build(int l,int r,int rt)
{
add[rt] = 0;//初始化为全部结点未被标记
if (l == r)
{
sum[rt] = 1;//初始颜色为1
return ;
}
int mid = (l + r) >> 1;
build(lson);
build(rson);
PushUp(rt);
}
void update(int L,int R,int c,int l,int r,int rt)
{
if (L <= l && r <= R)
{
add[rt] =1<<(c-1);//位运算左移表示有某种颜色
sum[rt] =1<<(c-1);
return ;
}
PushDown(rt , r - l + 1);//----延迟标志域向下传递
int mid = (l + r) >> 1;
if (L <= mid)
update(L , R , c , lson);//更新左儿子
if (mid < R)
update(L , R , c , rson);//更新右儿子
PushUp(rt);
}
LL query(int L,int R,int l,int r,int rt)
{
if (L <= l && r <= R)
{
return sum[rt];
}
//要取rt子节点的值时,也要先把rt的延迟标记向下移动
PushDown(rt , r - l + 1);
int mid = (l + r) >> 1;
LL ret = 0;
if (L <= mid)
ret |= query(L , R , lson);
if (mid < R)
ret |= query(L , R , rson);
return ret;
}
int main()
{
int L, T, O;
int a, b, c;
while(~scanf("%d%d%d",&L,&T,&O))
{
build(1, L, 1);//建树
while(O--)//Q为询问次数
{
char op[2];
scanf("%s",op);
if(op[0] == 'P')
{
scanf("%d%d",&a,&b);
if(a > b)
{
int t = a;
a = b;
b = t;
}
LL tt=query(a, b, 1, L, 1);
int ans = 0;
while(tt)
{
if(tt&1)
{
ans++;
}
tt>>=1;
}
printf("%d\n",ans);
}
else
{
scanf("%d%d%d",&a,&b,&c);
if(a > b)
{
int t = a;
a = b;
b = t;
}
update(a, b, c, 1, L, 1);
}
}
}
return 0;
}
POJ 2777 Count Color(线段树+位运算)的更多相关文章
- poj 2777 Count Color - 线段树 - 位运算优化
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42472 Accepted: 12850 Description Cho ...
- poj 2777 Count Color(线段树区区+染色问题)
题目链接: poj 2777 Count Color 题目大意: 给出一块长度为n的板,区间范围[1,n],和m种染料 k次操作,C a b c 把区间[a,b]涂为c色,P a b 查 ...
- poj 2777 Count Color(线段树)
题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- poj 2777 Count Color(线段树、状态压缩、位运算)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38921 Accepted: 11696 Des ...
- Count Color(线段树+位运算 POJ2777)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39917 Accepted: 12037 Descrip ...
- POJ 2777 Count Color(线段树之成段更新)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- [poj2777] Count Color (线段树 + 位运算) (水题)
发现自己越来越傻逼了.一道傻逼题搞了一晚上一直超时,凭啥子就我不能过??? 然后发现cin没关stdio同步... Description Chosen Problem Solving and Pro ...
- POJ P2777 Count Color——线段树状态压缩
Description Chosen Problem Solving and Program design as an optional course, you are required to sol ...
- POJ 2777 Count Color(段树)
职务地址:id=2777">POJ 2777 我去.. 延迟标记写错了.标记到了叶子节点上.. . . 这根本就没延迟嘛.. .怪不得一直TLE... 这题就是利用二进制来标记颜色的种 ...
随机推荐
- org.eclipse.birt.report.data.oda.jdbc.JDBCException: Missing properties in Connection.open(Propertie
首先查看project的web.xml档"BIRT_RESOURCE_PATH"属性的设置.此属性设置的是"用户资源存放路径.这些资源包含 library 文件,imag ...
- 懵懵懂懂初识J2EE
一.定义 Java2平台包含:标准版.企业版.微缩版.当中J2SE是Java2的标准版,主要用 于桌面应用软件的编程:J2ME是微缩版,主要应用于嵌入式系统开发:还有这次学习的J2EE是Java2的企 ...
- java_访问权限修饰符
java的访问权限修饰符有四种,根据权限由大到小的顺序为:public,protected,包访问权限(默认,没有修饰符),private. 根据修饰的东西不同,详细介绍如下: 1.修饰类的话分为两种 ...
- hdu2571命
称号: Problem Description 穿过幽谷意味着离大魔王lemon已经无限接近了! 可谁能想到,yifenfei在斩杀了一些虾兵蟹将后.却再次面临命运大迷宫的考验.这是魔王lemon设下 ...
- python 3.4.0 简单的print 'hello world',出错--SyntaxError: invalid syntax
问题描写叙述: win7下安装的python 3.4.0版本号, 在命令行里写入简单的输出语句: print 'hello world' 然后enter,结果返回结果为: SyntaxError: i ...
- ECLIPSE IDEA 调音 1
为自己所用IDE进行jvm优 首先进行日志输出配置 Eclipse 改动eclipse.ini IDEA 改动 idea.exe.vmoptions 添加打印日志的配置參数 -XX:+Print ...
- ASP.NET MVC5 插件机制中插件的简单实现
Autofac 依赖注入 ASP.NET MVC5 插件机制中插件的简单实现 一.前言 由于项目业务复杂,创建了多个插件并把他们放在了不同的项目中,项目使用AutoFac做的IOC:但是主项目可以注入 ...
- ubuntu14.04通过将语音,耳机没有声音
真正使用linux每天都有新的发现啊,截至今天上午有一台电脑,听几首歌.后插上耳机中没有声音.我认为这是一个糟糕的耳机,把插在手机耳机,我发现了一个语音耳机,很奇怪,我喜欢计算机接口是不是有问题,是在 ...
- malloc,free简单的实现
有关标准库首先简要malloc其原理: 标准库内部通过一个双向链表.管理在堆中动态分配的内存. malloc函数分配内存时会附加若干(一般是12个)字节,存放控制信息. 该信息 ...
- 怪异php 语法, 求解!
查找php馍用来推断是否串串返回值和方法 strpos很奇怪. 请看下面的语句: echo "A1: ".(strpos("csd","c" ...