Count Color
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 34950   Accepted: 10542

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

Source

给出n的长度的木棒,初始的颜色都为1,给出num中颜色,给出m个操作, C l r x 将l到r内的全部颜色更改为x , P l r 问在l到r内有多少种颜色

用线段树存储下当前的每一段的颜色,更改用数组lazy标记,将颜色转化为二进制数,统计一段颜色时,对每一段能够对它的左右子树取 | 这样就能够统计这一段中的颜色出现的种类。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
#define maxn 110000
#define lmin 1
#define rmax n
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define root lmin,rmax,1
#define now l,r,rt
#define int_now int l,int r,int rt
#define INF 0x3f3f3f3f
#define eqs 1e-6
#define LL __int64
#define mod 10007
#define zero(x) ( fabs(x) < eqs ? 0 : x )
#define mem(a,b) (memset(a),b,sizeof(a))
int cl[maxn<<2] ;
int lazy[maxn<<2] ;
void push_up(int_now)
{
cl[rt] = cl[rt<<1] | cl[rt<<1|1] ;
}
void push_down(int_now)
{
if( lazy[rt] )
{
lazy[rt<<1] = lazy[rt<<1|1] = lazy[rt] ;
cl[rt<<1] = cl[rt<<1|1] = lazy[rt] ;
lazy[rt] = 0 ;
}
}
void creat(int_now)
{
cl[rt] = lazy[rt] = 0 ;
if( l != r )
{
creat(lson);
creat(rson);
push_up(now);
}
else
cl[rt] = 1 ;
}
void update(int ll,int rr,int x,int_now)
{
if( ll > r || rr < l )
return ;
if( ll <= l && r <= rr )
{
cl[rt] = lazy[rt] = 1<<(x-1);
return ;
}
push_down(now);
update(ll,rr,x,lson);
update(ll,rr,x,rson);
push_up(now);
}
int query(int ll,int rr,int_now)
{
if(ll > r || rr < l)
return 0;
if(ll <= l && r <= rr)
return cl[rt] ;
push_down(now);
return query(ll,rr,lson) | query(ll,rr,rson);
}
int ans(int x)
{
int aa = 0 ;
while(x)
{
if(x & 1)
aa++ ;
x >>= 1 ;
}
return aa ;
}
int main()
{
int n , ls , m ;
int l , r , x ;
char str[10] ;
while(~scanf("%d %d %d", &n, &ls, &m))
{
creat(root);
while(m--)
{
scanf("%s", str);
if(str[0] == 'C')
{
scanf("%d %d %d", &l, &r, &x);
if(l > r)
swap(l,r);
update(l,r,x,root);
}
else
{
scanf("%d %d", &l, &r);
if(l > r)
swap(l,r);
printf("%d\n",ans( query(l,r,root) ) );
}
}
}
return 0;
}

poj2777--Count Color(线段树,二进制转化)的更多相关文章

  1. [poj2777] Count Color (线段树 + 位运算) (水题)

    发现自己越来越傻逼了.一道傻逼题搞了一晚上一直超时,凭啥子就我不能过??? 然后发现cin没关stdio同步... Description Chosen Problem Solving and Pro ...

  2. POJ2777 Count Color 线段树区间更新

    题目描写叙述: 长度为L个单位的画板,有T种不同的颜料.现要求按序做O个操作,操作分两种: 1."C A B C",即将A到B之间的区域涂上颜色C 2."P A B&qu ...

  3. Count Color(线段树+位运算 POJ2777)

    Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39917 Accepted: 12037 Descrip ...

  4. POJ 2777 Count Color (线段树成段更新+二进制思维)

    题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

  5. poj 2777 Count Color(线段树区区+染色问题)

    题目链接:  poj 2777 Count Color 题目大意:  给出一块长度为n的板,区间范围[1,n],和m种染料 k次操作,C  a  b  c 把区间[a,b]涂为c色,P  a  b 查 ...

  6. POJ 2777 Count Color(线段树之成段更新)

    Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...

  7. poj 2777 Count Color(线段树)

    题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  8. poj 2777 Count Color - 线段树 - 位运算优化

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42472   Accepted: 12850 Description Cho ...

  9. POJ P2777 Count Color——线段树状态压缩

    Description Chosen Problem Solving and Program design as an optional course, you are required to sol ...

  10. poj 2777 Count Color(线段树、状态压缩、位运算)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38921   Accepted: 11696 Des ...

随机推荐

  1. 基于visual Studio2013解决C语言竞赛题之1089牛虎过河

        题目 解决代码及点评 /************************************************************************/ /* ...

  2. 1.1.7-学习Opencv与MFC混合编程之---为画图工具添加工具栏

    源代码:http://download.csdn.net/detail/nuptboyzhb/3961701 哎,其实里面有很多图片的,本来看起开很明了的,资源连接里有详细的文档,比下面的看的舒服的多 ...

  3. 手机SIM卡无法识别解决方案

    SIM卡是工作中测试用的,经常插拔到不同的手机,前两天SIM卡放到手机中都能正常识别,今天插入到另一款手机中发现无法识别.心里糟了,是不是卡坏了,根据之 前的直觉,在公司找了一块橡皮,在SIM卡的芯片 ...

  4. Android 高手进阶,自己定义圆形进度条

    背景介绍 在Android 开发中,我们常常遇到各种各样绚丽的控件,所以,依靠我们Android本身所带的控件是远远不够的,许多时候须要我们自定义控件,在开发的过程中.我们公司遇到了一种须要自己写的一 ...

  5. Java_io体系之BufferedWriter、BufferedReader简介、走进源码及示例——16

    Java_io体系之BufferedWriter.BufferedReader简介.走进源码及示例——16 一:BufferedWriter 1.类功能简介: BufferedWriter.缓存字符输 ...

  6. Revit二次开发之绘制钢筋

    第一次在博客园上写东西,也不知道该写些什么,我想就写点最近项目到遇到的问题吧. 最近在做一个小项目,具体需求大概是在一个revit模型中的对应的楼板位置绘制钢筋. 由于刚接触Revit二次开发,之前也 ...

  7. CTreeCtrl结点拖动实现(与后台联动)

    原帖及讨论:http://bbs.bccn.net/thread-211413-1-1.html 效果描述:鼠标点击并拖动某一结点可以把它以动到其他结点下.原理:把一个结点机器下面的所有结点在需要释放 ...

  8. C++ 虚函数表解析(比较清楚,还可打印虚函数地址)

    C++ 虚函数表解析 陈皓 http://blog.csdn.net/haoel 前言 C++中的虚函数的作用主要是实现了多态的机制.关于多态,简而言之就是用父类型别的指针指向其子类的实例,然后通过父 ...

  9. ListView数据动态刷新

    在Android开发中用到ListView时,经常遇到要更改ListView内容的情形,比如删除或增加ListView中显示的条目,这里给大家提供一下思路:不论ListView要显示的对象是什么(如: ...

  10. go(一)变量

    package main import ( "fmt" ) func main() { var a int a = var a1 string a1 = "my is a ...