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. jmeter+ant+jenkins 搭建接口自动化测试

    一.jmeter  我用的jmeter3.2   jmeter要运行,必须本地有java环境,所以需要配置jdk什么的,自行配置 二.ant 安装ant 第一步:下载ant  http://ant.a ...

  2. postman 第6节录制case

    我们先安装Postman Interceptor 记得一定要安装Postman Interceptor插件,这样在谷歌浏览器上访问的信息都会在postman的app端同步显示.安装好后Postman ...

  3. 项目管理svn

    https://nchc.dl.sourceforge.net/project/tortoisesvn/1.9.6/Application/TortoiseSVN-1.9.6.27867-x64-sv ...

  4. Mysql 数据库date, datetime类型设置0000-00-00默认值(default)报错问题

    Mysql 数据库date, datetime类型设置0000-00-00默认值报错问题 现象:MySQL5.7版本之后,date, datetime类型设置默认值"0000-00-00&q ...

  5. Django视图,与数据库交互并返回数据

    环境:python 2.7.13  数据库:sqlite3(Django自带) 在学习Django的时候,遇到了困难.大概就是取到数据库数据后一直不能转成json数据.最后终于自己琢磨解决了. 要点就 ...

  6. 转载 Java基本数据类型

    Java语言是静态类型的(statical typed),也就是说所有变量和表达式的类型再编译时就已经完全确定.由于是statical typed,导致Java语言也是强类型(Strong typed ...

  7. spring配置datasource三种方式

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp34 spring配置datasource三种方式 1.使用org.spri ...

  8. 10 Logistic Regression

    线性分类中的是非题 --->概率题 (设置概率阈值后,大于等于该值的为O,小于改值的为X) --->逻辑回归 O为1,X为0 逻辑回归假设 逻辑函数/S型函数:光滑,单调 自变量趋于负无穷 ...

  9. oracle12之 多租户容器数据库架构

    解读: 这张幻灯片展示了三个被部署的应用程序的整合 三个不同的非cdbs成为一个单一的.幻灯片中的图形显示了一个多租户 容器数据库有四个容器:根和三个可插入的数据库.每一个 可插入数据库有它自己的专用 ...

  10. 【Alpha阶段】第一次Scrum Meeting!

    每日任务 1.本次会议为第一次 Meeting会议: 2.本次会议在中午12:30,在第五社区5号楼楼下,召开本次会议为30分钟讨论接下来的任务: 一.今日站立式会议照片 二.每个人的工作 (有wor ...