Count Color
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 42828   Accepted: 12973

Description

Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem.

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

First line of input contains L (1 <= L <= 100000), T (1 <= T <= 30) and O (1 <= O <= 100000). Here O denotes the number of operations. Following O lines, each contains "C A B C" or "P A B" (here A, B, C are integers, and A may be larger than B) as an operation defined previously.

Output

Ouput results of the output operation in order, each line contains a number.

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(线段树涂色问题)的更多相关文章

  1. ZOJ1610(经典线段树涂色问题)

    Description Painting some colored segments on a line, some previously painted segments may be covere ...

  2. poj-2777线段树刷题

    title: poj-2777线段树刷题 date: 2018-10-16 20:01:07 tags: acm 刷题 categories: ACM-线段树 概述 这道题是一道线段树的染色问题,,, ...

  3. Count Color poj2777 线段树

    Count Color poj2777 线段树 题意 有一个长木板,现在往上面在一定区间内刷颜色,后来刷的颜色会掩盖掉前面刷的颜色,问每次一定区间内可以看到多少种颜色. 解题思路 这里使用线段树,因为 ...

  4. poj2777 线段树

    //Accepted 4768 KB 391 ms //线段树,延时标记的应用 //对于每一段,用一个int表示被着色的情况,change标记该段的颜色是否发生整体的改变,即这一段 //用没用被全部涂 ...

  5. 树链剖分+线段树 HDOJ 5029 Relief grain(分配粮食)

    题目链接 题意: 分粮食我就当成涂色了.有n个点的一棵树,在a到b的路上都涂上c颜色,颜色可重复叠加,问最后每一个点的最大颜色数量的颜色类型. 思路: 首先这题的输出是每一个点最后的情况,考虑离线做法 ...

  6. 【bzoj4826】[Hnoi2017]影魔 单调栈+可持久化线段树

    题目描述 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他收集了各式各样的灵魂,包括诗人.牧师.帝王.乞丐.奴隶.罪人,当然,还有英雄.每一个灵魂,都有着自己 ...

  7. [Sdoi2017]树点涂色 [lct 线段树]

    [Sdoi2017]树点涂色 题意:一棵有根树,支持x到根染成新颜色,求x到y颜色数,求x子树里点到根颜色数最大值 考场发现这个信息是可减的,但是没想到lct 特意设计成lct的形式! 如何求颜色数? ...

  8. 【BZOJ4817】树点涂色(LCT,线段树,树链剖分)

    [BZOJ4817]树点涂色(LCT,线段树,树链剖分) 题面 BZOJ Description Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义 ...

  9. [BZOJ4817][SDOI2017]树点涂色(LCT+DFS序线段树)

    4817: [Sdoi2017]树点涂色 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 692  Solved: 408[Submit][Status ...

随机推荐

  1. iOS_KVC与KVO

    目 录: 一.KVC   二.KVO                                                                                   ...

  2. Kubernetes Heapster

    Heapster是容器集群监控和性能分析工具,HPA.Dashborad.Kubectl top都依赖于heapster收集的数据. 但是Heapster从kubernetes 1.8以后已经被遗弃了 ...

  3. StringTemplateLoader的用法

    作为一个模板框架,freemarker的功能还是很强大的.在模板处理方面,freemarker有多种形式,最常见的方式是将模板文件放在一个统一的文件夹下面,如下形式:Configuration cfg ...

  4. Maven配置一键部署远程Tomcat

    1. 首先需要配置远程Tomcat的访问权限(设置访问用户名密码) http://www.cnblogs.com/liuchao102/p/5519345.html 2. 配置pom.xml 添加to ...

  5. SQL Server technical bulletin - How to resolve a deadlock

    https://support.microsoft.com/en-us/help/832524/sql-server-technical-bulletin-how-to-resolve-a-deadl ...

  6. mysql一次运行多个SQL文件

    在文件 batch.sql 中写下多个SQL文件 source file1.SQLsource file2.SQLsource file3.SQL 然后运行 source batch.sql

  7. 直播P2P技术3-伙伴节点质量评估及子流订阅

    以上模型,暂且称之为W-P2P吧.

  8. Spring Boot入门(二)

    一.Spring Boot项目打包 1.引入maven打包相关插件 2.项目右击run as>Maven clean.run as>Maven install在target文件夹下会生成相 ...

  9. PHP 图片操作(按照指定尺寸压缩,按照比例裁剪)

    提供二个常用的图片处理方法: 1.按照指定的尺寸压缩图片 /** * 按照指定的尺寸压缩图片 * @param $source_path 原图路径 * @param $target_path 保存路径 ...

  10. ActionContext实现原理

    StrutsPrepareAndExecuteFilter [http://www.tuicool.com/articles/NVNbYn] struts2 和 struts1 的一个重要区别就是它进 ...