$$Count Color$$

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 50865

Accepted: 15346

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

Submit

题解

题目大意:我们需要设计一个程序,支持两种操作

  • 修改:把区间\([l,r]\)内的颜色全部换成\(Q\)
  • 询问:区间\([l,r]\)内有多少种不同的颜色
  • 其中颜色数T\(<=30\)

本来博主蒟蒻看错题了,以为是SDOI2011染色那样询问区间内有多少个颜色段,然后迅速码完交了一发,结果WA,然后还以为是自己打错了,一直在调,后来发现是自己理解错题意了,重新打了一次,看到了颜色\(T<=30\),这对int类型状态压缩是完全没有问题的,然后就是乱打了,我们把颜色i表示为\(1<<(i-1)\)代表第i中颜色,这样在统计颜色数的时候把每一位拆开,看这一位是否是1

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#define in(i) (i=read())
#define ll(i) (i<<1)
#define rr(i) (i<<1|1)
#define mid (l+r>>1)
using namespace std;
int read() {
int ans=0,f=1; char i=getchar();
while(i<'0' || i>'9') {if(i=='-') f=-1; i=getchar();}
while(i>='0' && i<='9') {ans=(ans<<1)+(ans<<3)+i-'0'; i=getchar();}
return ans*f;
}
int n,m,T,ans;
int sum[400010],lazy[400010];
inline void pushup(int node) {
sum[node]=sum[ll(node)]|sum[rr(node)];
}
inline void pushdown(int node) {
lazy[ll(node)]=lazy[node];
lazy[rr(node)]=lazy[node];
sum[ll(node)]=sum[rr(node)]=lazy[node];
lazy[node]=0;
}
void build(int node,int l,int r) {
if(l==r) {
sum[node]=1;
return;
}
build(ll(node),l,mid);
build(rr(node),mid+1,r);
pushup(node);
}
void update(int node,int l,int r,int left,int right,int k) {
if(l>right || r<left) return;
if(left<=l && r<=right) {
lazy[node]=1<<k-1;
sum[node]=1<<k-1;
return;
}
if(lazy[node]) pushdown(node);
update(ll(node),l,mid,left,right,k);
update(rr(node),mid+1,r,left,right,k);
pushup(node);
}
inline int work(int x) {
int ans=0;
while(x) {
if(x&1) ans++;
x>>=1;
}
return ans;
}
void check(int node,int l,int r,int left,int right) {
if(l>right || r<left) return;
if(left<=l && r<=right) {
ans|=sum[node];
return;
}
if(lazy[node]) pushdown(node);
check(ll(node),l,mid,left,right);
check(rr(node),mid+1,r,left,right);
}
int main()
{
while(scanf("%d%d%d",&n,&T,&m)!=EOF) {
memset(lazy,0,sizeof(lazy));
int x,y,k; build(1,1,n);
for(int i=1;i<=m;i++) {
char op[10]; scanf("%s",op);
if(op[0]=='C') {
in(x); in(y); in(k);
if(x>y) swap(x,y);
update(1,1,n,x,y,k);
}
else {
ans=0; in(x); in(y);
if(x>y) swap(x,y);
check(1,1,n,x,y); ans=work(ans);
printf("%d\n",ans);
}
}
}
return 0;
}

博主蒟蒻,随意转载.但必须附上原文链接

http://www.cnblogs.com/real-l/

[POJ2777] Count Color的更多相关文章

  1. POJ-2777 Count Color(线段树,区间染色问题)

    Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40510 Accepted: 12215 Descrip ...

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

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

  3. POJ2777 Count Color 线段树区间更新

    题目描写叙述: 长度为L个单位的画板,有T种不同的颜料.现要求按序做O个操作,操作分两种: 1."C A B C",即将A到B之间的区域涂上颜色C 2."P A B&qu ...

  4. [POJ2777]Count Color(线段树)

    题目链接:http://poj.org/problem?id=2777 给你一个长为L想线段,向上面染色,颜色不超过30种,一共有O次操作,操作有两种: C a b c 在[a,b]上染上c颜色 P ...

  5. Count Color poj2777 线段树

    Count Color poj2777 线段树 题意 有一个长木板,现在往上面在一定区间内刷颜色,后来刷的颜色会掩盖掉前面刷的颜色,问每次一定区间内可以看到多少种颜色. 解题思路 这里使用线段树,因为 ...

  6. Count Color(线段树+位运算 POJ2777)

    Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39917 Accepted: 12037 Descrip ...

  7. Count Color POJ--2777

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32217   Accepted: 9681 Desc ...

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

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

  9. POJ 2777 Count Color(线段树染色,二进制优化)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42940   Accepted: 13011 Des ...

随机推荐

  1. python应用:爬虫实例(静态网页)

    爬取起点中文网某本小说实例: # -*-coding:utf8-*- import requests import urllib import urllib2 from bs4 import Beau ...

  2. admin添加用户时报错:(1452, 'Cannot add or update a child row: a foreign key constraint fails (`mxonline`.`django_admin_l

    在stackoverflow找到答案: DATABASES = { 'default': { ... 'OPTIONS': { "init_command": "SET ...

  3. R语言学习笔记(二十):stringr包中函数介绍(表格)

    stringr包中的重要函数 函数 功能说明 R Base中对应函数 使用正则表达式的函数 str_extract() 提取首个匹配模式的字符 regmatches() str_extract_all ...

  4. poj 2393 奶牛场生产成本问题 贪心算法

    题意:有一个奶牛场,第i周的生产成本为c,需要数量为 y,每周的存储成本为s.问怎么安排使得成本最低? 思路: 成本最低是吧?求出每周的最低成本*该周需要的数量就是成本最低 每周的成本有两个:自己本周 ...

  5. WCF入门四[WCF的通信模式]

    一.概述 WCF的通信模式有三种:请求/响应模式.单向模式和双工通信. 二.请求/响应模式 请求/响应模式就是WCF的默认模式,前面几篇随笔中的示例都是这种模式,当客户端发送请求后(非异步状态下),即 ...

  6. laravel读excel

    fileName = "test.xls";$filePath = "../storage/app/";Excel::load($filePath.$fileN ...

  7. javascript 自定义发布与订阅

    //声明一个类,与普通的类的声明不一样, function Girl() { //将类的事件声明成一个私有的属性,里面是一个对象 this._events = {} } /* { "失恋&q ...

  8. 『AngularJS』ngShow

    原文 描述 ngShow指令显示或隐藏给定的基于标明ngShow属性的HTML元素.元素的显示或隐藏通过在元素上移除或添加ng-hide CSS类属性.".ng-hide"CSS类 ...

  9. 机器学习/逻辑回归(logistic regression)/--附python代码

    个人分类: 机器学习 本文为吴恩达<机器学习>课程的读书笔记,并用python实现. 前一篇讲了线性回归,这一篇讲逻辑回归,有了上一篇的基础,这一篇的内容会显得比较简单. 逻辑回归(log ...

  10. mongodb数据库高级操作

    1.创建索引 2.索引名称 3.其他索引 4.explain 5.操作索引 6.高级特性 7.固定集合 8.导入导出 9.上锁 10.添加用户 11.主从复制