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. AdapterView及其子类之二:使用ListActivity及ArrayAdapter创建列表

    见归档项目ListActivityDemo.zip. 基本步骤如下: 1.创建一个TextView,用于指定每一个ListView的格式 <?xml version="1.0" ...

  2. linux搜索jar内容

    linux搜索  spring-beans-2.5.6.jar  内容 1.jar tvf spring-beans-2.5.6.jar -c  创建新的归档文件 -t  列出归档目录 -x  解压缩 ...

  3. 将java源码打成jar包

    方法一:通过jar命令 jar命令的用法: 下面是jar命令的帮助说明: 用法:jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] ...

  4. mysql slow log分析工具的比较

    mysql 中的 slow log 是用来记录执行时间较长(超过 long_query_time 秒)的 sql 的一种日志工具. 启用 slow log 在 my.cnf 中设置 [mysqld] ...

  5. 教你50招提升ASP.NET性能(十一):避免在调试模式下运行网站

    (17)Avoid running sites in debug mode 招数17: 避免在调试模式下运行网站 When it comes to ASP.NET, one of the most c ...

  6. cocos2d-x 网格动画深入分析

    转自:http://www.2cto.com/kf/201212/179828.html 在TestCpp中的EffectsTest示例中展示了一些屏幕特效,它是将屏幕划分为多个格子,并对这些格子进行 ...

  7. 找回丢失的SQL Server性能计数器

    There was one time when I was delivering a Service using a tool that gathers performance data throug ...

  8. ThinkPHP CURD方法盘点:page方法

    page方法也是模型的连贯操作方法之一,是完全为分页查询而诞生的一个人性化操作方法. 用法 我们在前面已经了解了关于limit方法用于分页查询的情况,而page方法则是更人性化的进行分页查询的方法,例 ...

  9. 用C# sqlserver实现增删改查

    using System.Data;using System.Data.SqlClient;//先打开两个类库文件SqlConnection con = new SqlConnection(); // ...

  10. java_实现接口的枚举类

    package ming; interface GenderDoc { void info(); } enum Gender implements GenderDoc { // public stat ...