POJ 2777 Count Color(线段树 + 染色问题)
传送门:Count Color
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
Source
题意:
给定一个长度为N(N <= 100000)的数列Si,紧接着Q(Q <= 100000)条操作,操作 形式有两种: 1. "C A B C" 将A到B的数都染成C这种颜色。 2. "P A B" 输出A和B之间不同颜色的数目。
题解:
首先要想到的就是线段树,经典的区间维护问题,这里需要用到一个叫懒惰标记的方法,
这样可以大大缩短更新所需要的时间
这里还有一个巧妙地地方,就是区间染色的个数,这个可以用二进制位来解决,最多30种,用
一个int就可以存下,每个位代表一个颜色,区间上的颜色可以取其子区间或和(详细看代码)。
关于懒惰标记:
lazy是一个很经典的思想。所谓lazy,就是懒惰,每次不想做太多,只要插入的区间完
全覆盖了当前结点所管理的区间就不再往下做了,在当前结点上打上一个lazy标记,然
后直接返回。下次如果遇到当前结点有lazy标记的话,直接传递给两个儿子,自己的标
记清空。这样做肯定是正确的。我们以染色为例,可以这样想,如果当前结点和它的子
孙都有lazy标记的话,必定是子孙的先标记,因为如果是自己先标记,那么在访问子孙
的时候,必定会将自己的标记下传给儿子,而自己的标记必定会清空,那么lazy标记也
就不存在了。所以可以肯定,当前的lazy标记必定覆盖了子孙的,所以直接下传即可,
不需要做任何判断
代码:
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <vector>
#define mem(arr, num) memset(arr, 0, sizeof(arr))
#define _for(i, a, b) for (int i = a; i <= b; i++)
#define __for(i, a, b) for (int i = a; i >= b; i--)
#define IO \
ios::sync_with_stdio(false); \
cin.tie(); \
cout.tie();
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f;
;
const ll mod = 1000000007LL;
<< ;
int dat[N];
void down(int k)
{
dat[k << ] = dat[k << | ] = dat[k];
}
void update(int a, int b, int k, int l, int r, int c)
{
if (b < l || a > r)
;
else if (a <= l && b >= r)
{
dat[k] = << (c - );
}
else if (a <= r && b >= l)
{
if (log2(dat[k]) == (int)log2(dat[k])) down(k);
update(a, b, k << , l, (l + r) / , c);
update(a, b, k << | , (l + r) / + , r, c);
dat[k] = dat[k << ] | dat[k << | ];
}
}
int query(int a, int b, int k, int l, int r)
{
;
if (a <= l && b >= r)
{
return dat[k];
}
else if (a <= r && b >= l)
{
if (log2(dat[k]) == (int)log2(dat[k])) down(k);
, l, (l + r) / ) | query(a, b, k << | , (l + r) / + , r);
}
}
int main()
{
int n, t, o, a, b, c;
while (scanf("%d%d%d", &n, &t, &o) != EOF)
{
mem(dat,);
dat[] = ;
_for(i, , o)
{
char op;
getchar();
scanf("%c", &op);
if (op == 'C')
{
scanf("%d%d%d", &a, &b, &c);
if (a > b) swap(a, b);
update(a, b, , , n, c);
}
else
{
scanf("%d%d", &a, &b);
if (a > b) swap(a, b);
, , n);
;
), sum++;
printf("%d\n", sum);
}
}
}
;
}
/*
8 5 30
C 1 3 2
C 2 4 3
C 3 5 4
C 4 6 5
C 5 7 6
C 6 8 7
P 1 8
*/
POJ 2777 Count Color(线段树 + 染色问题)的更多相关文章
- poj 2777 Count Color(线段树区区+染色问题)
题目链接: poj 2777 Count Color 题目大意: 给出一块长度为n的板,区间范围[1,n],和m种染料 k次操作,C a b c 把区间[a,b]涂为c色,P a b 查 ...
- poj 2777 Count Color(线段树)
题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- poj 2777 Count Color(线段树、状态压缩、位运算)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38921 Accepted: 11696 Des ...
- poj 2777 Count Color - 线段树 - 位运算优化
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42472 Accepted: 12850 Description Cho ...
- POJ 2777 Count Color(线段树之成段更新)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- POJ P2777 Count Color——线段树状态压缩
Description Chosen Problem Solving and Program design as an optional course, you are required to sol ...
- POJ 2777 Count Color(段树)
职务地址:id=2777">POJ 2777 我去.. 延迟标记写错了.标记到了叶子节点上.. . . 这根本就没延迟嘛.. .怪不得一直TLE... 这题就是利用二进制来标记颜色的种 ...
- POJ 2777 Count Color(线段树染色,二进制优化)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42940 Accepted: 13011 Des ...
- poj 2777 Count Color
题目连接 http://poj.org/problem?id=2777 Count Color Description Chosen Problem Solving and Program desig ...
随机推荐
- 优化Hadoop Balancer运行速度
(如果运行hbase的话建议为16384),指定用于在DataNode间传输block数据的最大线程数,老版本的对应参数为dfs.datanode.max.xcievers 2.修改dfs.datan ...
- 【Android】完善Android学习(三:API 3.0)
备注:之前Android入门学习的书籍使用的是杨丰盛的<Android应用开发揭秘>,这本书是基于Android 2.2API的,目前Android已经到4.4了,更新了很多的API,也增 ...
- Enterprise Architect 13 : 将绘制的图形导出成图片 或者 拷贝到剪贴板中
使用Enterprise Architect 13 工具画类图.包图.需求图.状态图.时序图什么的导出成图片格式 或者拷贝到剪贴版,步骤如下: Publish -> Save Image -&g ...
- Sass 基本特性-基础 笔记
一.变量声明 $ 变量的声明使用 $ 所有的变量必须声明到变量调用之前 从3.4版本开始,Sass已经可以正确处理作用域的概念 在局部范围声明一个已经存在于全局内的变量时,局部变量就会成为全 ...
- 诱惑当前 你的孩子能hold住吗?
1.打好态度.动机.价值观基础 培养未成熟主体远大的志向,并不是向他们讲一些抽象的道理,而是根据他们的年龄特点,从形象的故事.童话开始,从身边的人物.事例出发,逐渐渗透一些人生的哲理.有一位自觉性非常 ...
- 【uva11987】带删除的并查集
题意:初始有N个集合,分别为 1 ,2 ,3 .....n.有三种操件1 p q 合并元素p和q的集合2 p q 把p元素移到q集合中3 p 输出p元素集合的个数及全部元素的和. 题解: 并查集.只是 ...
- 【poj2104-求区间第k大数(不修改)】主席树/可持续化线段树
第一道主席树~然而是道比较水的...因为它不用修改... 转载一个让我看懂的主席树的讲解吧:http://blog.csdn.net/regina8023/article/details/419106 ...
- 【NOIP】提高组2012 国王游戏
[题意] 恰逢H国国庆,国王邀请n位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右 手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这n位大臣排 成一排,国王站在队伍的最前面.排 ...
- html+js实现的触屏版贪吃蛇
查看线上demo(服务器经常断开,推荐下载源码本地打开): http://47.93.103.19:8044/client/ ; 使用手机打开或者chrome浏览器的手机模式打开 源码地址 :http ...
- Python3安装Celery模块后执行Celery命令报错
1 Python3安装Celery模块后执行Celery命令报错 pip3 install celery # 安装正常,但是执行celery 命令的时候提示没有_ssl模块什么的 手动在Python解 ...