Count Color

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

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
 #include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define ll long long
typedef struct abcd
{
ll d,nu;
} abcd;
abcd a[];
void build(int b,int c,int x)
{
if(b==c)
{
a[x].nu=<<;
return ;
}
int m=(b+c)>>;
build(b,m,x<<);
build(m+,c,x<<|);
a[x].nu=<<;
}
void fun(int x)
{
a[x<<].d=a[x<<|].d=a[x].d;
a[x<<].nu=<<a[x].d;
a[x<<|].nu=<<a[x].d;
a[x].d=;
}
void update(int x,int y,int b,int c,int t,int z)
{
if(x<=b&&y>=c)
{
a[t].d=z;
a[t].nu=<<z;
return ;
}
if(a[t].d)
fun(t);
int m=(b+c)>>;
if(x<=m)update(x,y,b,m,t<<,z);
if(y>m)update(x,y,m+,c,t<<|,z);
a[t].nu=a[t<<].nu|a[t<<|].nu;
}
ll query(int x,int y,int b,int c,int t)
{
if(x<=b&&y>=c)
return a[t].nu;
if(a[t].d)
fun(t);
int m=(b+c)>>;
ll r=;
if(x<=m)r=query(x,y,b,m,t<<);
if(y>m)r|=query(x,y,m+,c,t<<|);
return r;
}
int unn(ll x)
{
ll ans=;
while(x)
{
ans+=x&;
x>>=;
}
return ans;
}
int main()
{
//freopen("in.txt","r",stdin);
int l,t,o,i,aa,ab,ac;
char x;
while(~scanf("%d%d%d",&l,&t,&o))
{
memset(a,,sizeof(a));
build(,l,);
for(i=; i<o; i++)
{
getchar();
x=getchar();
if(x=='C')
{
scanf("%d%d%d",&aa,&ab,&ac);
if(aa>ab)swap(aa,ab);
update(aa,ab,,l,,ac);
}
else
{
scanf("%d%d",&aa,&ab);
if(aa>ab)swap(aa,ab);
ll ans=query(aa,ab,,l,);
printf("%d\n",unn(ans));
}
}
}
}

Count Color 线段树的更多相关文章

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

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

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

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

  3. poj 2777 Count Color(线段树)

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. diplay:table-cell和伪元素:after方法让图片居中

    让图片居中和文字居中是不一样的,文字居中可以通过line-height等调整,让图片居中方法,参考各种资料博文和测试  目前接触两种方法 display:table-cell和伪元素:after方法 ...

  2. HashMap 底层算法分析

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp72 Hash算法HashMap使用Hash算法,所以在解剖HashMap ...

  3. Mock Server 入门

    Mock Server介绍 什么是mock ? 我在去年的时候介绍一篇幅 python mock的基本使用,http://www.cnblogs.com/fnng/p/5648247.html 主要是 ...

  4. setTimeout,setInterval你不知道的…

    javascript线程解释(setTimeout,setInterval你不知道的事) 标签: javascript引擎任务浏览器functionxmlhttprequest 2011-11-21 ...

  5. 自制mpls ldp实验

    实验步骤 步骤1:完成EIGRP BGP 及宣告配置 步骤2:完成LDP 的配置 过程校验 步骤1:校验和理解LDP 邻居关系的发现和邻接关系的建立 R4#show mpls ldp discover ...

  6. 201521123091 《Java程序设计》第12周学习总结

    Java 第十一周总结 第十一周的作业. 目录 1.本章学习总结 2.Java Q&A 3.码云上代码提交记录及PTA实验总结 4.课后阅读 1.本章学习总结 1.1 以你喜欢的方式(思维导图 ...

  7. C语言数据在内存分配

    一个由C/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)— 程序运行时由编译器自动分配,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中的栈.程序结束时由编译器自动释放 ...

  8. 201521123096《Java程序设计》第七周学习总结

    1. 本周学习总结 2. 书面作业 ArrayList代码分析 1.1 解释ArrayList的contains源代码 contains遍历了ArrayList,如果ArrayList中存在与o相等的 ...

  9. 读Zepto源码之Touch模块

    大家都知道,因为历史原因,移动端上的点击事件会有 300ms 左右的延迟,Zepto 的 touch 模块解决的就是移动端点击延迟的问题,同时也提供了滑动的 swipe 事件. 读 Zepto 源码系 ...

  10. oracle-获取数据库中所有表的注释 comments

    公司dba提供的脚本: set serveroutput on set feedback off spool /tmp/getcomments.out select 'comment on table ...