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. python 小技巧 防止SSL报错信息

    代码里面 只需要添加如下2行: import ssl ssl._create_default_https_context = ssl._create_unverified_context

  2. RGB转MIPI CSI芯片方案TC358746XBG

    型号:TC358746XBG功能:RGB888/666/565与MIPI CSI 互转通信方式:IIC/SPI分辨率:720p电源:3.3/1.2V封装形式:BGA72深圳有现货库存,价格有优势,样片 ...

  3. RMI,RPC,SOAP对比分析

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp60   1.RMI     使用java的程序员,对于RMI(Remot ...

  4. ionic3 自动创建启动背景splash以及图标icon

    在新建的项目文件夹下的 resources下就是我们放置图标以及启动背景图片的位置了. 如果现在我们想生成自己的图片的启动背景以及图片,我们需要把resources下的 icon.png 以及spla ...

  5. jmeter3.2 创建webservice 测试

    1.创建测试计划 templates→select Template→Building a SOAP WebService Test Plan 2.修改HTTP Request Defaults中的参 ...

  6. 转:面向切面编程AOP的理解

    AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...

  7. ServletResponse的一些知识点

    ServletResponse* 服务器对浏览器做出的响应,将需要发送给浏览器的所有数据全部存放在此对象上.* 发送数据,使用流操作,将所需要的数据,存放在指定的流中,数据将显示到浏览器中* 字符流 ...

  8. 【Socket编程】Java通信是这样炼成的

    简介 网络无处不在,移动互联时代也早已到来,单机版程序慢慢的已没有生命力,所有的程序都要能够访问网络,比如 QQ 网络聊天程序.迅雷下载程序等,这些程序都要同网络打交道,本次将与各位小伙伴们分享的就是 ...

  9. 【集美大学1411_助教博客】团队作业7——Alpha冲刺之事后诸葛亮

    写在前面的话 alpha阶段都顺利完成了,大家这次作业完成得都很认真.我觉得通过这些问题,大家既可以回顾自己的alpha阶段,又可以给beta阶段做一些指引.但看了所有组的博客,没有一个组在这些问题之 ...

  10. 个人附加作业XD --这门课终于结束了~~

    你认为每次项目的评分标准存在哪些问题,你认为的合理评分准则是怎样的(个人/结对/团队算三个) 评分的话我个人觉得是存在一些问题的. 第一,分数差异 问题:一个就是各班的成绩评分有高有低,有的班整体分数 ...