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 ...
随机推荐
- 四、golang内置函数、递归、闭包、数组切片和map
一.总体内容 1.内置函数.递归函数.闭包 2.数组和切片 3.map数据结构 4.package介绍 一.内置函数 注意:值类型用new来分配内存,引用类型用make来分配内存 1.close:主要 ...
- debian下使用dynamic printk分析usb转串口驱动执行流程
看了一篇文章<debug by printing>,文中提到了多种通过printk来调试驱动的方法,其中最有用的就是"Dynamic debugging". “Dyna ...
- poj 2488 A Knight's Journey 【骑士周游 dfs + 记忆路径】
题目地址:http://poj.org/problem?id=2488 Sample Input 3 1 1 2 3 4 3 Sample Output Scenario #1: A1 Scenari ...
- Centos7 docker容器 搭建
Dockerfile 文件如下: # # MAINTAINER Carson,C.J.Zeong <zcy@nicescale.com> # DOCKER-VERSION # # Dock ...
- JConsole操作手册
一篇Sun项目主页上介绍JConsole使用的文章,前段时间性能测试的时候大概翻译了一下以便学习,今天整理一下发上来,有些地方也不知道怎么翻,就保留了原文,可能还好理解点,呵呵,水平有限,翻的不好,大 ...
- webview 最简单的demo
) { return; } view.loadUrl(url); }} <!--activity_test.xml> <?xml version="1.0" en ...
- Teamviewer_相关
1.官网下载:https://www.teamviewer.com/zhcn/download/windows/,里面选择 "Portable"的版本来下载(按钮"下载P ...
- php:使用XHProf查找PHP性能瓶颈
https://www.cnblogs.com/casatwy/archive/2013/01/17/2865241.html XHProf是facebook 开发的一个测试php性能的扩展,本文记录 ...
- phalcon 连接多个数据库 phalcon multi-database
db: //This service returns a MySQL database $di->set('dbMaster', function() { return new \Phalcon ...
- 机器学习(六)—随机森林Random Forest
1.什么是随机采样? Bagging可以简单的理解为:放回抽样,多数表决(分类)或简单平均(回归): Bagging的弱学习器之间没有boosting那样的联系,不存在强依赖关系,基学习器之间属于并列 ...