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
线段树
用一个LL 位表示颜色情况
用一个laz表示当前块是否是同一颜色的
pushdown
如果当前颜色是统一的,那么将这个颜色推给下面,同时将下面的laz标识设置为1, 然后清除该标识 pushup
color是左右子节点color的按位与
如果 左边颜色是统一的! 而且 右边颜色是统一的! 而且左右两边颜色相同!
才讲该节点的laz设置为1
在查询的时候由于我们查询的是color,如果当前区间的laz = 1
表示区间内所有颜色都是统一的,那么我们可以直接返回当前节点的颜色
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
#define MAXN 100009
LL l,t,o;
struct node
{
LL l,r;
LL laz;
LL sum;//用一个数字的每个位表示这个段有多少颜色
}T[MAXN * + ];
void pushdown(LL p)
{
if(T[p].laz)
{
T[p].laz = ;
T[p*].laz = T[p*+].laz = ;
T[p*].sum = T[p* + ].sum = T[p].sum;
}
}
void pushup(LL p)
{
T[p].sum = T[p*].sum | T[p*+].sum;
if(T[p*].laz && T[p*+].laz && T[p*].sum == T[p* + ].sum)
T[p].laz = ;
}
void build(LL x,LL l,LL r)
{
T[x].l = l,T[x].r = r;
if(l == r)
{
return ;
}
LL mid = (l + r)/;
build(x * ,l ,mid);
build(x * + ,mid + ,r);
//pushup(x);
}
void update(LL x,LL l,LL r,LL val)
{
if(T[x].l == l&&T[x].r == r)
{
T[x].sum = ( << (val - ) );
T[x].laz = ;
return ;
}
pushdown(x);
LL mid = ( T[x].l + T[x].r)/;
if(r<=mid)
update(x * , l ,r, val);
else if(l > mid)
update(x * +, l , r, val);
else
{
update(x * ,l, mid,val);
update(x * + ,mid + , r, val);
}
pushup(x);
}
LL query(LL x, LL l, LL r)
{
if(T[x].laz || (T[x].l == l && T[x].r == r))
{
return T[x].sum;
}
// pushdown(x);
LL mid = (T[x].l + T[x].r )/;
if(r<=mid)
return query(x * , l, r);
else if( l > mid)
return query(x * + , l ,r);
else
{
// LL tmp1 = query(x *2, l, mid);
// LL tmp2 = query(x*2 +1, mid + 1, r);
return query(x *, l, mid) | query(x* +, mid + , r);
}
}
int main()
{ char c[];
LL L,R,tmp;
scanf("%lld%lld%lld",&l,&t,&o);
build(,,l);
T[].laz = T[].sum = ;
for(LL i = ;i<o;i++)
{
scanf("%s%lld%lld",c,&L,&R);
if(L > R)
{
LL sd = L;
L = R;
R = sd;
}
if(c[]=='C')
{
scanf("%lld",&tmp);
update(,L,R,tmp);
}
else if( c[] == 'P')
{
LL tmp = query(,L,R);
LL cnt = ;
for(int i = ; i < t; i++)
{
if(tmp&(<<i))
cnt++;
}
printf("%lld\n",cnt);
}
} }

Count Color POJ - 2777 线段树的更多相关文章

  1. POJ 2777——线段树Lazy的重要性

    POJ 2777 Count Color --线段树Lazy的重要性 原题 链接:http://poj.org/problem?id=2777 Count Color Time Limit: 1000 ...

  2. poj 2777(线段树+lazy思想) 小小粉刷匠

    http://poj.org/problem?id=2777 题目大意 涂颜色,输入长度,颜色总数,涂颜色次数,初始颜色都为1,然后当输入为C的时候将x到y涂为颜色z,输入为Q的时候输出x到y的颜色总 ...

  3. POJ 2777(线段树)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42507   Accepted: 12856 Des ...

  4. POJ 2777 线段树基础题

    题意: 给你一个长度为N的线段数,一开始每个树的颜色都是1,然后有2个操作. 第一个操作,将区间[a , b ]的颜色换成c. 第二个操作,输出区间[a , b ]不同颜色的总数. 直接线段树搞之.不 ...

  5. poj 2777 线段树的区间更新

    Count Color Time Limit: 1000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java ...

  6. poj 2777线段树应用

    敲了n遍....RE愉快的debug了一晚上...发现把#define maxn = 100000 + 10 改成 #define maxn = 100010 就过了....感受一下我呵呵哒的表情.. ...

  7. poj 2777 线段树 区间更新+位运算

    题意:有一个长板子,分成多段,有两种操作,第一种是C给从a到b那段染一种颜色c,另一种是P询问a到b有多少种不同的颜色.Sample Input2 2 4  板长 颜色数目 询问数目C 1 1 2P ...

  8. poj 2886 线段树+反素数

    Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 12744   Acc ...

  9. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

随机推荐

  1. php(一)

    PHP (Hypertext preprocessor 超文本预处理器) 1.环境工具 Xampp等工具 2.apache配置 默认的Apache路径是  c:/xampp/apache 文件夹 可以 ...

  2. 跨库导表数据(sql)

    程序员用 列子: insert into "000".tbFreeReportselect ReportCode ,ReportName ,GroupNamefrom openda ...

  3. Farseer.net轻量级开源框架 中级篇:UrlRewriter 地址重写

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: Cookies.Session.Request 下一篇:Farseer.net轻量 ...

  4. ARM开发板如何选型-I.MX6Q开发板

    拥有丰富扩展能力,供货周期长的开发平台,省事安心   处理器:迅为-i.MX6开发板恩智浦Cortex-A9 四核i.MX6Q处理器,主频1GHz,内存2G,存储16GB. 系统支持:i.MX6开发板 ...

  5. AIX 10G HA RAC卸载

    删除 1:crs_stat –t资源都停掉 2:停ha 3: 删除oracle 4:删除crs 5: 删除ha smit hacmp 6: 删除vg exportvg 7;卸载hacmp smitty

  6. 自定义对话框(jDialog)

    [配置项]jDialog options点击收起 一.接口功能 jDialog的默认配置项,本组件提供的所有对话框,都可以通过修改这些配置项来实现不同的效果. 二.详细配置项 /** * 对话框的默认 ...

  7. CHECKPOINT - 强制一个事务日志检查点

    SYNOPSIS CHECKPOINT DESCRIPTION 描述 预写式日志(Write-Ahead Logging (WAL))缺省时在事务日志中每隔一段时间放一个检查点. (要调整这个原子化的 ...

  8. CAD参数绘制固定批注(网页版)

    js中实现代码说明: 自定义实体绘制函数 function ExplodeFun(pCustomEntity, pWorldDraw, txt) { var sGuid = pCustomEntity ...

  9. Debug:This kind of launch is configured to openthe debug perspective when it解决办法

    http://blog.sina.com.cn/s/blog_7ca3aa020100zlha.html 启动tomcat时,myeclipse报错: This kind of launch is c ...

  10. 01Hibernate

    Hibernate Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自 ...