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 ...
随机推荐
- 里氏替换原则中is和as分别的作用
is 是用于检查对象是否指定类型兼容 if(empls[i] is SE){ ((SE)empls).SayHi(); } as 不用强转可以直接转换 if(empls[i] is SE){ SE s ...
- was--创建概要文件(典型)
1.第一步 2 .创建 3.创建 4 .典型 5 下一步 6 下一步 7.下一步 8.输入用户和密码,下一步 9.下一步 10.下一步 11.下一步 12.下一步 13.下一步 14.创建 ...
- java-IO操作性能对比
在软件系统中,IO速度比内存速度慢,IO读写在很多情况下会是系统的瓶颈. 在java标准IO操作中,InputStream和OutputStream提供基于流的IO操作,以字节为处理单位:Reader ...
- selenium兼容非标准chrome内核的浏览器
多浏览器兼容性测试(1) RIDE已经支持多浏览器兼容性测试,例如: firefox ie chrome safari 但是,项目要求支持360极速和360安全浏览器.所以,我们需要增加代码让RIDE ...
- 04Hibernate连接数据库环境配置
Hibernate连接数据库环境配置
- C# defult关键字
一.问题 今天写一个函数提示用defult,因为第一次用记录一下 public static T GetConfig<T>(string strConfig) { try { return ...
- Spring Data Redis入门示例:数据序列化 (四)
概述 RedisTemplate默认使用的是基于JDK的序列化器,所以存储在Redis的数据如果不经过相应的反序列化,看到的结果是这个样子的: 可以看到,出现了乱码,在程序层面上,不会影响程序的运行, ...
- Python --- 二叉树的层序建立与三种遍历
二叉树(Binary Tree)时数据结构中一个非常重要的结构,其具有....(此处省略好多字)....等的优良特点. 之前在刷LeetCode的时候把有关树的题目全部跳过了,(ORZ:我这种连数据结 ...
- rem怎么算
* rem : html根标签的 font-size决定* em 相对于父标签的font-size 决定的* 设计稿的宽一般都是 750px ---> 20px* 750px 1rem = 10 ...
- C++链表STL
#include <iostream> #include <list> #include <algorithm> #include <stdlib.h> ...