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. .babelrc参数小解

    .babelrc是用来设置转码规则和插件的,这种文件在window上无法直接创建,也无法在HBuilder中创建,甚至无法查看,但可以在sublime text中创建.查看并编辑. 当我们使用es6语 ...

  2. 20145231第二周Java学习笔记

    20145231 <Java程序设计>第2周学习总结 教材学习内容总结 本周的学习采用的依然是先看课本,再看视频,然后实践敲代码,最后根据学习笔记总结完成博客. 第三章:基础语法 知识点比 ...

  3. ubuntu 忘记root密码

    Ubuntu14.04系统中,因为误操作导致管理员密码丢失或无效,并且忘记root密码,此时无法进行任何root/sudo权限操作.可以通过GRUB重新设置root密码,并恢复管理员账户到正常状态. ...

  4. 网络安全-跨站脚本攻击XSS(Cross-Site Scripting)

    一.XSS攻击简介 作为一种HTML注入攻击,XSS攻击的核心思想就是在HTML页面中注入恶意代码,而XSS采用的注入方式是非常巧妙的. 在XSS攻击中,一般有三个角色参与:攻击者.目标服务器.受害者 ...

  5. Apache Phoenix的子查询

    Phoenix现在支持在WHERE 和FROM 中使用子查询.子查询可以被指定在很多地方,比如 IN/NOT IN, EXISTS/NOTEXISTS等. Subqueries with INor N ...

  6. 导入Jquery.min.js时 JQuery 上打红X了

    问题解决:右击jquery.min.js——>MyEclipse——>点击Exclude From Validation——>点击Run Validation 即可

  7. Redis 创建多个端口

    默认的是6379 可以用6380,6381开启多个 1.开启 ./redis-server ../etc/redis.6380.conf & 2.链接 redis-cli -p 6380 查看 ...

  8. 智课雅思词汇---十九、前缀se是什么意思

    智课雅思词汇---十九.前缀se是什么意思 一.总结 一句话总结:前缀:se- 表示“分开, 离开, 区别开” 前缀:se- [词根含义]:分离 [同源单词]:secede, secession, s ...

  9. jquery判断密码是否一致?

    密码 请输入密码 重新输入密码 请输入新密码 <input type="text" id="btn0"> 密码 <span class=&qu ...

  10. 浪漫爱心--第三方开源--PeriscopeLayout

    点此下载 使用很简单,首先在xml里面添加 <Button android:id="@+id/btn_start" android:layout_width="wr ...