Count Color POJ - 2777 线段树
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
Output
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 线段树的更多相关文章
- POJ 2777——线段树Lazy的重要性
POJ 2777 Count Color --线段树Lazy的重要性 原题 链接:http://poj.org/problem?id=2777 Count Color Time Limit: 1000 ...
- poj 2777(线段树+lazy思想) 小小粉刷匠
http://poj.org/problem?id=2777 题目大意 涂颜色,输入长度,颜色总数,涂颜色次数,初始颜色都为1,然后当输入为C的时候将x到y涂为颜色z,输入为Q的时候输出x到y的颜色总 ...
- POJ 2777(线段树)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42507 Accepted: 12856 Des ...
- POJ 2777 线段树基础题
题意: 给你一个长度为N的线段数,一开始每个树的颜色都是1,然后有2个操作. 第一个操作,将区间[a , b ]的颜色换成c. 第二个操作,输出区间[a , b ]不同颜色的总数. 直接线段树搞之.不 ...
- poj 2777 线段树的区间更新
Count Color Time Limit: 1000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java ...
- poj 2777线段树应用
敲了n遍....RE愉快的debug了一晚上...发现把#define maxn = 100000 + 10 改成 #define maxn = 100010 就过了....感受一下我呵呵哒的表情.. ...
- poj 2777 线段树 区间更新+位运算
题意:有一个长板子,分成多段,有两种操作,第一种是C给从a到b那段染一种颜色c,另一种是P询问a到b有多少种不同的颜色.Sample Input2 2 4 板长 颜色数目 询问数目C 1 1 2P ...
- poj 2886 线段树+反素数
Who Gets the Most Candies? Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 12744 Acc ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
随机推荐
- 平板&Safari 开发tips
css: *{ margin: 0; padding: 0; /* 禁止用户点选网页内容 */ -webkit-touch-callout:none; -webkit-user-select:non ...
- Android与H5互调(通过实例来了解Hybrid App)
前些日子,Android原生开发将被取缔的吵得火热,JavaScript是能做一个完全的APP,但只使用JavaScript做出来的APP也不会牛逼到哪里去.最好的是混合(Hybrid)开发,在需要的 ...
- R Programming week 3-Loop functions
Looping on the Command Line Writing for, while loops is useful when programming but not particularly ...
- -webkit/IE/Firefox的一些样式
仅限于-webkit的样式特效:-webkit-overflow-scrolling:touch;滚动时回弹效果:如果出现偶尔卡住不动的情况,那么在使用该属性的元素上不设置定位或者手动设置定位为sta ...
- (转)淘淘商城系列——maven工程debug调试
http://blog.csdn.net/yerenyuan_pku/article/details/72784074 上文我们已经实现了商品列表展示的功能,在实际开发中我们肯定是要经常对maven工 ...
- windows sdk编程为应用程序添加图标
#include <windows.h> /*消息处理函数声明*/ HRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM ...
- CAD交互绘制文字(com接口)
在cad设计时,需要绘制文字,用户可以设置设置绘制文字的高度等属性. 主要用到函数说明: _DMxDrawX::DrawText 绘制一个单行文字.详细说明如下: 参数 说明 DOUBLE dPosX ...
- spark版本不支持(降版本打包)
在做项目的时候代码已经更新为hadoop 2.7 spark 2.1 scala 2.11.8版本,但是服务器版本使用的是hadoop2.6 spark1.6 以及scala2.10.6版本,,主程 ...
- 20面向对象三特征 之继承 方法重写 super
继承是:多个类有重复内容,把重复内容放到一个新类中,就可以通过extends关键词去让原来的类和新类产生继承关系,子类只能拿到父类一部分信息.通过extends关键词去指明类与类之间的关系,一个父类可 ...
- LINUX:解压问题tar: Child returned status
解压某个文件时 #tar -zxvf xxxxx.tar.gz 出现下面的错误提示: gzip: stdin: not in gzip formattar: Child returned status ...