Count Color

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 39917 Accepted: 12037

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

POJ Monthly–2006.03.26,dodo

这道线段树的题敲的总体还算比较顺,就是在初始化的时候被坑惨了,说是第一种颜色却手残的写成了1,明明是(1<<1).悲哀啊

#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <string>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
struct node
{
int sta;
bool lazy;
}Tree[500000];
int Trans(int ans)
{
int sum=0;
while(ans)
{
if(ans&1)
{
sum++;
}
ans/=2;
}
return sum;
}
void Build(int L,int R,int site)
{
Tree[site].lazy=false;
Tree[site].sta=2;//初始化为颜色1就是(1<<1),看了一晚上才看出来
if(L==R)
{
return ;
}
int mid=(L+R)>>1;
Build(L,mid,site<<1);
Build(mid+1,R,site<<1|1);
}
void update(int L,int R,int l,int r,int site,int ans)
{
if(L==l&&R==r)//更新到区间
{
Tree[site].sta=(1<<ans);
Tree[site].lazy=true;
return ;
}
if(Tree[site].lazy)//向下更新
{
Tree[site<<1].sta=Tree[site<<1|1].sta=Tree[site].sta;
Tree[site<<1].lazy=Tree[site<<1|1].lazy=true;
Tree[site].lazy=false;
}
int mid=(L+R)>>1;
if(r<=mid)
{
update(L,mid,l,r,site<<1,ans);
}
else if(l>mid)
{
update(mid+1,R,l,r,site<<1|1,ans);
}
else
{
update(L,mid,l,mid,site<<1,ans);
update(mid+1,R,mid+1,r,site<<1|1,ans);
}
Tree[site].sta=Tree[site<<1].sta|Tree[site<<1|1].sta;//区间的合并
} int Query(int L,int R,int l,int r,int site)
{
if(L==l&&R==r)
{
return Tree[site].sta;
}
if(Tree[site].lazy)//向下更新
{
Tree[site<<1].sta=Tree[site<<1|1].sta=Tree[site].sta;
Tree[site<<1].lazy=Tree[site<<1|1].lazy=true;
Tree[site].lazy=false;
}
int mid=(L+R)>>1;
if(r<=mid)
{
return Query(L,mid,l,r,site<<1);
}
else if(l>mid)
{
return Query(mid+1,R,l,r,site<<1|1);
}
else
{
return Query(L,mid,l,mid,site<<1)|Query(mid+1,R,mid+1,r,site<<1|1);
}
}
int main()
{
int L,T,O;
char s[3];
int a,b,c;
while(~scanf("%d %d %d",&L,&T,&O))
{
Build(1,L,1);
for(int i=1;i<=O;i++)
{
scanf("%s %d %d",s,&a,&b);
if(a>b)
{
swap(a,b);
}
if(s[0]=='C')
{
scanf("%d",&c);
update(1,L,a,b,1,c);
}
else
{
int ans=Query(1,L,a,b,1);
ans=Trans(ans);
printf("%d\n",ans);
}
}
}
return 0;
}

Count Color(线段树+位运算 POJ2777)的更多相关文章

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

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

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

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

  3. Count Colour_poj2777(线段树+位)

    POJ 2777 Count Color (线段树)   Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

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

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

  5. POJ 2777 Count Color(线段树+位运算)

    题目链接:http://poj.org/problem?id=2777 Description Chosen Problem Solving and Program design as an opti ...

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

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

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

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

  8. poj 3225 线段树+位运算

    略复杂的一道题,首先要处理开闭区间问题,扩大两倍即可,注意输入最后要\n,初始化不能随便memset 采用线段树,对线段区间进行0,1标记表示该区间是否包含在s内U T S ← S ∪ T 即将[l, ...

  9. poj 2777 Count Color(线段树)

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

随机推荐

  1. Hibernate HQL的update方法详解

    虽然hibernate提供了许多方法对数据库进行更新,但是这的确不能满足开发需要.现在讲解一下用hql语句对数据进行更新. 不使用参数绑定格式String hql="update User ...

  2. bootstrap日期插件

    <!DOCTYPE HTML> <html> <head> <link href="http://netdna.bootstrapcdn.com/t ...

  3. 面试题-->写一个函数,返回一个数组中所有元素被第一个元素除的结果

    package com.rui.test; import java.util.Random; /** * @author poseidon * @version 1.0 * @date:2015年10 ...

  4. VPN fq工具的选择

    豆荚VPN还是不错的.有时候百度会打不开,重新连接一下就可以了 http://wandou.shouyo99.com/ 如果高速模式不可以,请记得选择PPTP模式!!!但有个副作用就是百度打不开了--

  5. Layer弹窗组件

    layer是一款近年来备受青睐的web弹层组件,她具备全方位的解决方案,致力于服务各水平段的开发人员,您的页面会轻松地拥有丰富友好的操作体验. Layer的开发手册和下载地址 http://layer ...

  6. decode 函数将字符串从某种编码转为 unicode 字符

    环境:Ubuntu, Python 2.7 基础知识 这个程序涉及到的知识点有几个,在这里列出来,不详细讲,有疑问的直接百度会有一堆的. 1.urllib2 模块的 request 对像来设置 HTT ...

  7. IOS第三天

    第三天 ******** 九宫格代码的现实 @interface HMViewController () /** 应用程序列表 */ @property (nonatomic, strong) NSA ...

  8. javaWeb中servlet开发(3)——Servlet生命周期

    生命周期:是一个程序的存在周期,servlet由于是受容器的管理,所以容器来决定其生命周期 1.servlet生命周期 2.servlet生命周期对应的方法 3.servlet生命周期代码 publi ...

  9. M1卡修改各区块控制位值和数据

    (一),以常用设置"08 77 8F 69"控制条件为例,先搞清楚它――具有的访问权限. 1.对"08 77 8F 69"值进行计算,该值定位于各区块3的6,7 ...

  10. nginx优化 突破十万并发

    一.一般来说nginx 配置文件中对优化比较有作用的为以下几项: 1.  worker_processes 8; nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数 (如,2个四核的cpu ...