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. sed武功心法(info sed翻译+注解)

    本文中的提到GNU扩展时,表示该功能是GNU为sed提供的(即GNU版本的sed才有该功能),一般此时都会说明:如果要写具有可移植性的脚本,应尽量避免在脚本中使用该选项. 本文中的正则表达式几乎和gr ...

  2. django富文本编辑器

    -------------------tinymce富文本编辑器1.下载安装 1.在网站pypi网站搜索并下载"django-tinymce-2.4.0" 2.解压:tar zxv ...

  3. localStorage sessionStorage 和cookie等前端存储方式总结

    localStorage sessionStorage 和cookie localStorage localStorage是本地存储的,除非清空本地数据 localStorage不会自动把数据发给服务 ...

  4. 从送外卖到建站售主机还有共享自行车说起-2017年8月江西IDC排行榜与发展报告

    曾几何时,送外卖,这样的"低技术含量"工作,很难被互联网公司看上,直到百度将其当作连接终端用户与大数据的管道. 同样,销售主机域名和建站业务,本也是"微小体量" ...

  5. iOS js oc相互调用(JavaScriptCore 下)

    下来我们使用js调用iOS js调用iOS分两种情况 一,js里面直接调用方法 二,js里面通过对象调用方法 首先我们看第一种,直接调用方法. 其中用到了iOS的block 上代码 -(void)we ...

  6. ADO.NET的学习

    ADO.NET的几个对象 Connection:管理数据库的连接 Command:对数据库执行命令 DataReader:数据流读取器,返回的数据都是快速的且只是"向前"的数据流. ...

  7. tomcat学习笔记

    1.安装和运行[必须] * 解压apache-tomcat-6.0.35.zip * 位置:不能包含中文和空格 * 运行: %tomcat%/bin/startup.bat * 测试 * http:/ ...

  8. 基于JZ2440开发板编写bootloader总结(一)

    凡走过必留下痕迹,学点什么都会有用的. 本系列博文总结了自己在学习嵌入式Linux编程过程中的收获,若有错误,恳请指正,谢谢! --参考教材韦东山系列教材 bootloader 是一个用于启动linu ...

  9. 结对编程1.四则运算GUI版

    201421123022 王若凡        201421123026  欧阳勇 coding详细代码 a.需求分析: 这个程序做成GUI(可以是Windows PC 上的,也可以是Mac.Linu ...

  10. SNS团队第四次站立会议(2017.04.25)

    一.当天站立式会议照片 本次会议主要内容:汇报工作进度,根据完成情况调整进度 二.每个人的工作 成员 今天已完成的工作 明天计划完成的工作 罗于婕 相关数据库文件建立起来  完善数据库文件 龚晓婷 研 ...