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 ...
随机推荐
- Spark学习之Spark调优与调试(7)
Spark学习之Spark调优与调试(7) 1. 对Spark进行调优与调试通常需要修改Spark应用运行时配置的选项. 当创建一个SparkContext时就会创建一个SparkConf实例. 2. ...
- Objective -C Object initialization 对象初始化
Objective -C Object initialization 对象初始化 1.1 Allocating Objects 分配对象 Allocation is the process by w ...
- 唤醒键盘时取消对特定类的position:fixed定位
/* 唤起键盘时取消对特定类的position:fixed定位 */ var windheight = $(window).height(); /*未唤起键盘时当前窗口高度*/ $(window).r ...
- 从0开始搭建SQL Server 2012 AlwaysOn 第二篇(配置故障转移集群)
本篇主要讲配置Windows 故障转移集群及遇到的相关问题(坑),因为AlwaysOn是基于Windows的故障转移集群的 在讲解步骤之前需要了解一下故障转移集群仲裁配置 四种集群的仲裁配置: 1.多 ...
- 数据字典Dictionary存放键值对
1. 方法思路: 使用数据字典[Dictionary<string, string>],声明一个list集合,将“XML子节点名称”.“节点值”以键[节点名称]值[节点值]对的形式 ...
- scrapy 的分页爬取 CrawlSpider
1.创建scrapy工程:scrapy startproject projectName 2.创建爬虫文件:scrapy genspider -t crawl spiderName www.xxx.c ...
- close - 关闭一个文件描述符
SYNOPSIS 总览 #include <unistd.h> int close(int fd); DESCRIPTION 描述 close 关闭 一个 文件 描述符 , 使它 不在 指 ...
- Spring Data Redis入门示例:基于RedisTemplate (三)
使用底层API:RedisConnection操作Redis,需要对数据进行手动转换(String <---->byte),需要进行多数重复性工作,效率低下:org.springframe ...
- Java 序列化Serializable详解(附详细例子)
Java 序列化Serializable详解(附详细例子) 1.什么是序列化和反序列化Serialization(序列化)是一种将对象以一连串的字节描述的过程:反序列化deserialization是 ...
- [Luogu] P2817 宋荣子的城堡
题目描述 saruka有一座大大的城堡!城堡里面有n个房间,每个房间上面都写着一个数字p[i].有一天,saruka邀请他的小伙伴LYL和MagHSK来城堡里玩耍(为什么没有妹子),他们约定,如果某一 ...