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. MySQL-LRU_List Free_List Flush_List

    关于 LRU_List ,Free_List,Flush_List的介绍:   LRU算法:(Latest Recent Used)最近最少使用      数据库的缓冲池通过LRU算法来进行管理.   ...

  2. linux下安装jsp开发运行环境(centos7)

    1 开发环境包括 1)apache-tomcat 2)java-jdk 3)mysql 2 apache-tomcat安装(应该先装java再装tomcat) 1)到官网下载最新版本(不建议用yum安 ...

  3. Cocos2d-x项目移植到WP8系列之一:前传

    原文链接: http://www.cnblogs.com/zouzf/p/3969993.html 许久没动笔了,随想一直都有动笔的想法,但拖来拖去,归根到底还是一个懒字吧 .发现人的惰性真是太强大了 ...

  4. org.apache.flume.ChannelException: Take list for MemoryTransaction, capacity 100 full, consider committing more frequently, increasing capacity, or increasing thread count

    flume在抽取MySQL数据到kafka时报错,如下 [SinkRunner-PollingRunner-DefaultSinkProcessor] ERROR org.apache.flume.s ...

  5. Gerrit使用感受

    CodeReivew好工具,可以随业务需求灵活配置权限等.

  6. VMWARE TOOLS安装出错:THE PATH IS NOT A VALID PATH TO THE 3.11.0.12-GENERIC KERNEL HEADERS

    VMWARE TOOLS安装提示THE PATH IS NOT A VALID PATH TO THE GENERIC KERNEL HEADERS I solved this problem, I ...

  7. Http协议与生命周期

    一.Http知识:    1.基于socket        浏览器(格式一)        web服务器(格式一)        MYSQL客户端(格式二)        MYSQL服务端(格式三) ...

  8. Java编程思想 Random(47)

    Random类包含两个构造方法,下面依次进行介绍:1. public Random()该构造方法使用一个和当前系统时间对应的相对时间有关的数字作为种子数,然后使用这个种子数构造Random对象.2. ...

  9. js工厂方法

    工厂方法与简单工厂的区别在于工厂方法没有switch条件分支,实例化哪一个子类放到了客户端(可以利用反射),这样整个工厂和产品体系都没有修改的变化,而只是扩展的变化,这就完全符合了开放-封闭原则的精神 ...

  10. EXCEL对比重复数据

    一.       EXCEL 突出重复项 1.      选择对应的数据 EXCEL 里选择好数据 2.      选择条件格式 这样就完成了数据重复的突出,可以按条件筛选.选择自己想要的数据