题目链接:http://poj.org/problem?id=2777

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

题意:

给一个固定长度为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(线段树+位运算)的更多相关文章

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

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

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

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

  3. poj 2777 Count Color(线段树)

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

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

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

  5. Count Color(线段树+位运算 POJ2777)

    Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39917 Accepted: 12037 Descrip ...

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

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

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

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

  8. [poj2777] Count Color (线段树 + 位运算) (水题)

    发现自己越来越傻逼了.一道傻逼题搞了一晚上一直超时,凭啥子就我不能过??? 然后发现cin没关stdio同步... Description Chosen Problem Solving and Pro ...

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

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

  10. POJ 2777 Count Color(段树)

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

随机推荐

  1. JFinal 的源代码超具体的分析DB+ActiveRecord

    我记得有人告诉我."面试一下spring源代码.看ioc.aop源代码"那为什么要看这些开源框架的源代码呢,事实上非常多人都是"应急式"的去读.就像读一篇文章一 ...

  2. Java多播通讯框架 JGroups(转)

    JGroups是一个可靠的群组通讯Java工具包.它基于IP组播(IP multicast),但在可靠性,组成员管理上对它作了扩展. JGroups的可靠性体现在: 1,对所有接收者的消息的无丢失传输 ...

  3. 当向后台插入或读取JSON数据遇见回车时

    今天在项目中发现.当插入或读取JSON数据时遇见回车符.返回JSON数据格式时会报错(firebug里体现为乱码),百度了一下发现JSON不支持字符串里存在回车! 解决的方法: 在向接口插入带json ...

  4. HDOJ 1753 明朝A+B

     http://acm.hdu.edu.cn/showproblem.php? pid=1753 大明A+B Time Limit: 3000/1000 MS (Java/Others)    M ...

  5. bzoj 2437 [Noi2011]兔子和鸡蛋 [二分图匹配]

    叙述性说明 这些日子.兔子和蛋像一个新的棋盘游戏. 这场比赛是在 n 行 m 在船上进行列. 前,棋盘上有一 个格子是空的,其他的格子中都放置了一枚棋子,棋子或者是黑色,或者是白色. 每一局游戏总是兔 ...

  6. AngularJS 实现按需异步加载

    习惯了 seajs 的异步加载方式,也想着 angular 也能同样使用异步加载,但是事实不随人愿. angularjs 和 requirejs 一样,使用的是预先加载的方式组织模块(这和 seajs ...

  7. 在Mac OS上配置Android开发环境

    1)安装配置NDK 1.1 下载NDK并解压缩 下载路径 https://developer.android.com/tools/sdk/ndk/index.html 在terminal运行: chm ...

  8. Ajax实践之用户是否存在

    关于Ajax在之前的学习中,已经对它的基础知识有了初步的了解.仅仅是欠实践. 那么接下来就让实践来检验一下真理吧! 基础见:http://blog.csdn.net/liu_yujie2011com/ ...

  9. 最受欢迎telnet

    点击开关在模拟器,它相当于实PC经由控制线连接真实开关 由于我使用telnet远程登录到交换机的话.因为telnet工作在TCP/IP模型的应用层,现在让应用层通信的话,我首先要保证网络层通信,通信, ...

  10. REST|RESTful初步认识

    工作中要用到jersey来实现restful风格的webservice.对于webservice另一定的认知(能够觉得是一种服务,远程调用的组件),可是对于restful笔者根本就木有了解过,rest ...