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. 射击的乐趣:WIN32诠释打飞机游戏源码补充

    打飞机游戏源码补充 从指定位置加载bmp并显示到对话框. , TRUE);, , LR_LOADFROMFILE);          {         BITMAP bmpinfo;        ...

  2. webservice 地址

    快递查询WEB服务 http://webservice.36wu.com/ExpressService.asmx 支持上百家快递/物流查询,准确高效,所有数据均来自快递服务商.此数据返回类型进行了封装 ...

  3. hdu3589 Jacobi symbol(二次剩余 数论题)

    本题的注意点:n=p1*p2*p3......Pm 解法:直接利用公式a^((p-1)/2)=(a/p)mod p 即可求解. #include<stdio.h> #include< ...

  4. IOS 横屏中添加UIImagePickerController获取系统图片

    今天写ipad的项目,然后需要调用系统相册选择图片,然后用了UIImagePickerController ,崩溃了,后来查了一下,UIImagePickerController只支持竖屏,但是... ...

  5. Android ADB使用之详细篇

    Android开发环境中,ADB是我们进行Android开发经常要用的调试工具,它的使用当然是我们Android开发者必须要掌握的. ADB概述 Android Debug Bridge,Androi ...

  6. iOS UIView 基本属性用法

    .创建UIView UIView * redView = [[UIView alloc] initWithFrame:CGRectMake(, , , )]; UIView * blueView = ...

  7. linux高级命令组合

    ps -auxww | grep httpd 快速找到正在运行的apache服务安装目录 find / -path  'sina_app_v3*' 快速找到根目录下面的sina_app_v3目录 fi ...

  8. Android利用Looper在子线程中改变UI

    MainActivity如下: package cn.testlooper; import android.app.Activity; import android.os.Bundle; import ...

  9. mysql数据库常用语句2

    关于mysql常用语句的整理,上一篇涉及到ddl.dml以及一些简单的查询语句. 1:mysql分页查询 select * from table_name limit 5,10;   从下标为5元素查 ...

  10. 【二分答案+贪心】解决“最小值最大”问题(UVa 12124 - Assemble)

    Problem A - Assemble Time limit: 2 seconds Recently your team noticed that the computer you use to p ...