Count Color

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 42507   Accepted: 12856

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.

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

题目大意:

有L个画板,30种颜色,o个操作:P a b :询问a-b 种有多少种颜色不同的,C  a b c:把a-b全部涂成c的颜色(覆盖掉)

解题思路:

线段树+二进制判重

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 100005
using namespace std;
int n,m,q;
char c;
int x,y,z;
int sum;
struct Node
{
int l,r,col;//col用位运算,颜色30种,
int cover;//延时更新,是否涂了颜色
}no[*N];
void pushup(int u)
{
no[u].col=no[u*].col|no[u*+].col;
return;
}
void pushdown(int u)
{
no[u].cover=;
no[u*].cover=;
no[u*].col=no[u].col;
no[u*+].cover=;
no[u*+].col=no[u].col;
return;
}
void build(int u,int left,int right)
{
no[u].l=left;
no[u].r=right;
no[u].col=;
no[u].cover=;//初始状态全为1
if(left==right)
return;//没有赋值
int mid=(no[u].l+no[u].r)>>;
build(u*,left,mid);
build(u*+,mid+,right);
pushup(u);
}
void updata(int u,int left,int right,int val)
{
if(no[u].l==left&&no[u].r==right)
{
no[u].col=<<(val-);//直接等于,覆盖原有颜色
no[u].cover=;
return;
}
if(no[u].col==<<(val-))return;//剪枝:如果颜色一样,不用更新
if(no[u].cover)pushdown(u);//延时更新
int mid=(no[u].l+no[u].r)>>;
if(right<=mid)updata(u*,left,right,val);
else if(left>mid)updata(u*+,left,right,val);
else
{
updata(u*,left,mid,val);
updata(u*+,mid+,right,val);
}
pushup(u);
}
void query(int u,int left,int right)
{
if(no[u].l==left&&no[u].r==right)
{
sum|=no[u].col;
return ;
}
if(no[u].cover)pushdown(u);
int mid=(no[u].l+no[u].r)>>;
if(right<=mid)query(u*,left,right);
else if(left>mid)query(u*+,left,right);
else
{
query(u*,left,mid);
query(u*+,mid+,right);
}
}
int Ans(int sum)
{
int ans=;
while(sum)
{
if(sum&)
ans++;
sum=(sum>>);
}
return ans;
}
int main()
{
freopen("poj2777.in","r",stdin);
freopen("poj2777.out","w",stdout);
scanf("%d%d%d",&n,&m,&q);
build(,,n);
getchar();
for(int i=;i<=q;i++)
{
scanf("%s",&c);
if(c=='C')
{
scanf("%d%d%d",&x,&y,&z);
if(x>y)swap(x,y);
getchar();
updata(,x,y,z);
}
else
{
scanf("%d%d",&x,&y);
getchar();
sum=;
if(x>y)swap(x,y);//x可能>y
query(,x,y);
cout<<Ans(sum)<<endl;;
}
}
return ;
}

POJ 2777(线段树)的更多相关文章

  1. poj 2777(线段树+lazy思想) 小小粉刷匠

    http://poj.org/problem?id=2777 题目大意 涂颜色,输入长度,颜色总数,涂颜色次数,初始颜色都为1,然后当输入为C的时候将x到y涂为颜色z,输入为Q的时候输出x到y的颜色总 ...

  2. POJ 2777——线段树Lazy的重要性

    POJ 2777 Count Color --线段树Lazy的重要性 原题 链接:http://poj.org/problem?id=2777 Count Color Time Limit: 1000 ...

  3. POJ 2777 线段树基础题

    题意: 给你一个长度为N的线段数,一开始每个树的颜色都是1,然后有2个操作. 第一个操作,将区间[a , b ]的颜色换成c. 第二个操作,输出区间[a , b ]不同颜色的总数. 直接线段树搞之.不 ...

  4. poj 2777线段树应用

    敲了n遍....RE愉快的debug了一晚上...发现把#define maxn = 100000 + 10 改成 #define maxn = 100010 就过了....感受一下我呵呵哒的表情.. ...

  5. Count Color POJ - 2777 线段树

    Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds ...

  6. poj 2777 线段树的区间更新

    Count Color Time Limit: 1000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java ...

  7. poj 2777 线段树 区间更新+位运算

    题意:有一个长板子,分成多段,有两种操作,第一种是C给从a到b那段染一种颜色c,另一种是P询问a到b有多少种不同的颜色.Sample Input2 2 4  板长 颜色数目 询问数目C 1 1 2P ...

  8. poj 2886 线段树+反素数

    Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 12744   Acc ...

  9. poj 3468(线段树)

    http://poj.org/problem?id=3468 题意:给n个数字,从A1 …………An m次命令,Q是查询,查询a到b的区间和,c是更新,从a到b每个值都增加x.思路:这是一个很明显的线 ...

随机推荐

  1. [BZOJ]2132: 圈地计划 最小割

    圈地计划 Description 最近房地产商GDOI(Group of Dumbbells Or Idiots)从NOI(Nuts Old Idiots)手中得到了一块开发土地.据了解,这块土地是一 ...

  2. VS项目重命名工具

    VS项目重命名工具 VS项目整体重命名工具 不再为项目重命名和修改命名空间而烦恼,简单几个字,但是开发加上测试大量项目,前前后后竟然跨越了1个月,汗...不过真正的开发时间可能2-3天的样子.  一. ...

  3. Oracle中的User与Schema

    Oracle中有两个概念容易混淆──user和schema,本随笔记录并摘抄了一些促进理解这连个概念的理解方法,希望有助于分清这两个概念. user是控制权限的,而schema则是一个容器,非所有者如 ...

  4. ssh免密码登录记录

    做mha.hadoop安装过程中都要用ssh免密码登陆,查过一些资料,踩过很多坑,下面用简单记录一下 首先要安装ssh linux : centOS 6.5 yum -y install *ssh* ...

  5. Dev 等待提示 WaitDialogForm 升级版

    本文转载:http://www.cnblogs.com/VincentLuo/archive/2011/12/24/2298916.html   一.Dev的等待提示框                 ...

  6. 【转】数据库中的join

    转自:http://coolshell.cn/articles/3463.html 对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有 ...

  7. C++ 外部调用private方法

    1.思考,对于C++,能不能在外部调用私有方法? 2.在Java中,子类继承不能缩小父类成员的访问权限.因为在Java中,继承只是表示Is-A关系,因此,父类提供的接口,子类必须承诺仍然提供,不能缩小 ...

  8. VMWare9下基于Ubuntu12.10搭建Hadoop-1.2.1集群

    VMWare9下基于Ubuntu12.10搭建Hadoop-1.2.1集群 下一篇:VMWare9下基于Ubuntu12.10搭建Hadoop-1.2.1集群-整合Zookeeper和Hbase 近期 ...

  9. mysql中删除表

    有两种方式: 1.delete from table table_name; 2.truncate table table_name; 第一种中,清空表后,主键id会在原先的记录基础上继续增加,而第二 ...

  10. dos常用文件操作命令

    1.DIR 含义: 显示指定目录下的文件和子目录列表 类型: 内部命令 格式: DIR[drive:][path][filename][/p][/w][/A[[:]attributes]][/O[[: ...