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 ...
随机推荐
- thinkphp3.2.3连接sqlserver 2008 R2 数据库
环境: 操作系统——win7 64位旗舰版 PHP——thinkphp 3.23 数据库——Microsoft SQL Server 2008 R2 需要用到的软件: 步骤: 1.搜索SQLSRV30 ...
- Zclip点击复制内容到剪贴板兼容各浏览器
WEB开发中,要让用户复制页面中的一段代码.URL地址等信息,为了避免用户拖动鼠标再进行右键复制操作而可能出现的差错,我们可以直接在页面中放置一个复制按钮,只需要轻轻一点这个复制按钮,内容将会被复制, ...
- 微信小程序组件解读和分析:六、progress进度条
progress进度条组件说明: 进度条,就是表示事情当前完成到什么地步了,可以让用户视觉上感知事情的执行.progress进度条是微信小程序的组件,和HTML5的进度条progress类似. pro ...
- OpenCV3.3安装教程
http://blog.csdn.net/amusi1994/article/details/76768775?locationNum=10&fps=1
- list map接口传递
1.传参时可以设置为jsonArray的格式 JSONArray array = new JSONArray(); JSONObject json = new JSONObject(); json.p ...
- redis 其他特性
1.消息订阅与发布 subscribe my1 订阅频道 psubscribe my1* 批量订阅频道,订阅以my1开头的所有频道 publish my1 hello 在指定频道中发布消息,返回值为接 ...
- basename - 从文件名中剥离目录和后缀
总览 (SYNOPSIS) basename NAME [SUFFIX] basename OPTION 描述 (DESCRIPTION) 显示 去掉 目录成分 后的 NAME. 如果 指定了 SUF ...
- Unexpected token d in JSON at position 669 while parsing near '...ct-mode":"^6.0.2"}
问题 在安装 babel 的时候,遇到问题 Unexpected token d in JSON at position 669 while parsing near '...ct-mode" ...
- 零基础入门学习Python(21)--函数:lambda表达式
知识点 lambda 表达式 Python 允许使用lambda关键字创建匿名函数 lambda 函数怎么使用? 单个参数 >>> def add(x): return 2*x + ...
- http主要请求头
一.内容协商 1.Accept:希望服务器返回的数据格式,如下面的:text/javascript, application/javascript, application/ecmascript, a ...