POJ2777(线段树涂色问题)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 42828 | Accepted: 12973 |
Description
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 思路:因为只有30种颜色,所以可以用2进制数颜色。
注意:l可能大于r
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = ;
struct Node{
int l, r;
int color;
bool lazy;
}a[MAXN*];
int n, t, m, tag;
void build(int rt, int l, int r)
{
a[rt].l = l;
a[rt].r = r;
a[rt].lazy = false;
if(l == r)
{
a[rt].color = ;
return ;
}
int mid = (l + r) >> ;
build(rt << , l, mid);
build((rt << ) | , mid + , r);
a[rt].color = a[rt<<].color | a[(rt<<)|].color;
}
void pushDown(int rt)
{
if(a[rt].lazy)
{
a[rt<<].color = a[rt].color;
a[(rt<<)|].color = a[rt].color;
a[rt<<].lazy = a[rt].lazy;
a[(rt<<)|].lazy = a[rt].lazy;
a[rt].lazy = false;
}
}
void update(int rt, int l, int r, int val)
{
if(a[rt].l == l && a[rt].r == r)
{
a[rt].color = << (val - );
a[rt].lazy = true;
return ;
}
pushDown(rt);
int mid = (a[rt].l + a[rt].r) >> ;
if(r <= mid)
{
update(rt << , l, r, val);
}
else if(mid < l)
{
update((rt << ) | , l, r, val);
}
else
{
update(rt << , l, mid, val);
update((rt << ) | , mid + , r, val);
}
a[rt].color = a[rt<<].color | a[(rt<<)|].color;
}
void query(int rt, int l, int r)
{
if(a[rt].l == l && a[rt].r == r)
{
tag |= a[rt].color;
return ;
}
pushDown(rt);
int mid = (a[rt].l + a[rt].r) >> ;
if(r <= mid)
{
query(rt << , l, r);
}
else if(mid < l)
{
query((rt << ) | , l, r);
}
else
{
query(rt << , l, mid);
query((rt << ) | , mid + , r);
}
}
int main()
{
while(scanf("%d %d %d", &n, &t, &m) != EOF)
{
build(, , n);
while(m--)
{
char op;
scanf("%*c%c", &op);
if(op == 'C')
{
int l, r, val;
scanf("%d %d %d", &l, &r, &val);
if(l > r) swap(l, r);
update(, l, r, val);
}
else
{
int l, r;
scanf("%d %d", &l, &r);
if(l > r) swap(l, r);
tag = ;
query(, l, r);
int res = ;
while(tag > )
{
if(tag & )
{
res++;
}
tag >>= ;
}
printf("%d\n", res);
}
}
}
return ;
}
POJ2777(线段树涂色问题)的更多相关文章
- ZOJ1610(经典线段树涂色问题)
Description Painting some colored segments on a line, some previously painted segments may be covere ...
- poj-2777线段树刷题
title: poj-2777线段树刷题 date: 2018-10-16 20:01:07 tags: acm 刷题 categories: ACM-线段树 概述 这道题是一道线段树的染色问题,,, ...
- Count Color poj2777 线段树
Count Color poj2777 线段树 题意 有一个长木板,现在往上面在一定区间内刷颜色,后来刷的颜色会掩盖掉前面刷的颜色,问每次一定区间内可以看到多少种颜色. 解题思路 这里使用线段树,因为 ...
- poj2777 线段树
//Accepted 4768 KB 391 ms //线段树,延时标记的应用 //对于每一段,用一个int表示被着色的情况,change标记该段的颜色是否发生整体的改变,即这一段 //用没用被全部涂 ...
- 树链剖分+线段树 HDOJ 5029 Relief grain(分配粮食)
题目链接 题意: 分粮食我就当成涂色了.有n个点的一棵树,在a到b的路上都涂上c颜色,颜色可重复叠加,问最后每一个点的最大颜色数量的颜色类型. 思路: 首先这题的输出是每一个点最后的情况,考虑离线做法 ...
- 【bzoj4826】[Hnoi2017]影魔 单调栈+可持久化线段树
题目描述 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他收集了各式各样的灵魂,包括诗人.牧师.帝王.乞丐.奴隶.罪人,当然,还有英雄.每一个灵魂,都有着自己 ...
- [Sdoi2017]树点涂色 [lct 线段树]
[Sdoi2017]树点涂色 题意:一棵有根树,支持x到根染成新颜色,求x到y颜色数,求x子树里点到根颜色数最大值 考场发现这个信息是可减的,但是没想到lct 特意设计成lct的形式! 如何求颜色数? ...
- 【BZOJ4817】树点涂色(LCT,线段树,树链剖分)
[BZOJ4817]树点涂色(LCT,线段树,树链剖分) 题面 BZOJ Description Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义 ...
- [BZOJ4817][SDOI2017]树点涂色(LCT+DFS序线段树)
4817: [Sdoi2017]树点涂色 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 692 Solved: 408[Submit][Status ...
随机推荐
- PHP类的变量与成员,及其继承、访问与重写要注意的问题
PHP的类及其实例: <?php ?> 后期静态绑定:为了避免子类重写静态属性后,使用继承来的方法仍然方法父类的静态属性,PHP5.3增加了一个新的语法,后期静态绑定,使用static关 ...
- cocos2dx打飞机项目笔记三:HeroLayer类和坐标系
HeroLayer类主要是处理hero的一些相关东西,以及调用bulletLayer的一些方法,因为子弹是附属于hero的~~ HeroLayer 类的成员如下: class HeroLayer : ...
- INSPIRED启示录 读书笔记 - 第25章 快速响应阶段
产品出炉后切莫虎头蛇尾 急于“撤军”是项目管理和产品开发流程中的大忌,只要稍微延长项目周期,观察用户对产品的反应,效果就会有天壤之别.这样做投资之小.回报之高会令你瞠目结舌,绝非其他项目阶段可比 产品 ...
- dreamweaver8快捷键
替换Ctrl+H 处理表格 选择表格(光标在表格中) Ctrl+A 移 动到下一单元格Tab 移 动到上一单元格Shift+Tab 插入行(在当前行之前)Ctrl+M 在表格末插入一行 在最后一个单元 ...
- 新手学逆向,调试abexcm1过程
写在前面的话:在下完全就是一个新手,现在目前在学16位汇编,偶尔用OD调试看看程序,主要就是为了学习,今天写这个帖子,完全就是笔记用,然后拿出来和大家一起讨论交流. 因为刚开始接触,文章可能一些地方有 ...
- 在物理机安装CentOS6.5
这两天就要开始在用户的新服务器上部署生产环境了.之前一直都是在服务器上搭虚拟机,而在物理机上安装还是第一次. 首先是要准备启动程序.我用的U盘作为启动盘. 刻盘的操作参考 http://jingyan ...
- OpenStack企业私有云新需求(1):Nova 虚机支持 GPU
作者:Sammy Liu 刘世民 本系列会介绍OpenStack 企业私有云的几个需求: GPU 支持 自动扩展(Auto-scaling)支持 混合云(Hybrid cloud)支持 物理机(Bar ...
- openstack nova 基础知识——Quota(配额管理)
一.什么是配额(quota)管理 简单的讲就是控制用户资源的数量.在openstack里,管理员为每一个工程(project)分配的资源都是有一定限制的,这些资源包括实例(instance).cpu. ...
- review37
线程的常用方法 1.start() 线程调用该方法将启动线程,使之从新建状态进入就绪队列排队. 2.run() 3.sleep() 4.isAlive() 线程处于新建状态时,线程调用isAlive( ...
- CSS3中的变形功能
一.变形主要值得是利用transform功能来实现文字或图片的旋转,缩放,倾斜,移动这四种处理. 1.旋转-----transform:rotate(xxdeg);( IE9以上,safari 3.1 ...