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.

          --by POJ
http://poj.org/problem?id=2777


题目大意:
区间染色,统计区间颜色数;
解法,线段树维护区间颜色情况,开桶;
因为颜色种类少,故压成二进制,应该快些;
挺水的题,代码也短;
代码如下:
#include<cstdio>
using namespace std;
const int MAXN=;
int tree[MAXN<<];
int lz[MAXN<<];
int n,t,o,L,R;
void up(int );
void down(int ,int ,int );
void col(int ,int ,int ,int );
int ask(int ,int ,int );
int ans(int );
int main()
{
int i,j,k;
char s[];
scanf("%d%d%d",&n,&t,&o);
lz[]=;
tree[]=;
for(i=;i<=o;i++){
scanf("%s",s);
if(s[]=='C'){
scanf("%d%d%d",&L,&R,&j);
if(L>=R)
k=L,L=R,R=k;
col(,n,,j);
}
else{
scanf("%d%d",&L,&R);
if(L>=R)
k=L,L=R,R=k;
j=ask(,n,);
printf("%d\n",ans(j));
}
}
}
void up(int nu){
tree[nu]=tree[nu<<]|tree[nu<<|];
}
void down(int l,int r,int nu){
if(!lz[nu])return ;
tree[nu<<]=lz[nu];
tree[nu<<|]=lz[nu];
lz[nu<<]=lz[nu<<|]=lz[nu];
lz[nu]=;
}
void col(int l,int r,int nu,int x){
if(L<=l&&r<=R){
tree[nu]=lz[nu]=(<<(x-));
return ;
}
down(l,r,nu);
int mid=(l+r)>>;
if(L<=mid)
col(l,mid,nu<<,x);
if(R>mid)
col(mid+,r,nu<<|,x);
up(nu);
}
int ask(int l,int r,int nu){
if(L<=l&&r<=R)
return tree[nu];
down(l,r,nu);
int ans=,mid=(l+r)>>;
if(L<=mid)
ans|=ask(l,mid,nu<<);
if(R>mid)
ans|=ask(mid+,r,nu<<|);
return ans;
}
int ans(int x){
int re=;
while(x){
if(x&)
re++;
x>>=;
}
return re;
}

  

POJ P2777 Count Color——线段树状态压缩的更多相关文章

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

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

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

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

  3. poj 2777 Count Color(线段树)

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

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

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

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

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

  6. POJ 2777 Count Color (线段树成段更新+二进制思维)

    题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

  7. HDU 6183 Color it cdq分治 + 线段树 + 状态压缩

    Color it Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Pro ...

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

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

  9. POJ训练计划2777_Count Color(线段树/成段更新/区间染色)

    解题报告 题意: 对线段染色.询问线段区间的颜色种数. 思路: 本来直接在线段树上染色,lz标记颜色.每次查询的话訪问线段树,求出颜色种数.结果超时了,最坏的情况下,染色能够染到叶子节点. 换成存下区 ...

随机推荐

  1. 浅谈javascript和python语言的深拷贝

    深拷贝: 之前在开发中我遇到一个很大的bug,经过我多次调试之后我发现原本应该有保存数据的地方数据全部被清空,仔细一看发现原来是被人为删除,明明操作的是一个副本,为什么原本也会跟着一起被删除呢?经过了 ...

  2. 基于iTop4412的FM收音机系统设计(二)

    说明:第一版架构为:APP+JNI(NDK)+Driver(linux),优点是开发简单,周期短,也作为自己的毕业设计 现在更新第二版,FM服务完全植入Android系统中,成为系统服务,架构为:AP ...

  3. c# timer使用

    C#里现在有3个Timer类: System.Windows.Forms.Timer System.Threading.Timer System.Timers.Timer 这三个Timer我想大家对S ...

  4. android4.0以上访问网络不能在主线程中进行以及在线程中操作UI的解决方法

    MONO 调用一个线程操作UI 然后报Only the original thread that created a view hierarchy can touch its views.错误 goo ...

  5. 何为Web App,何为Hybird App

    这些概念听起来很火,当下也很流行,真正理解起来却并非易事.如果让我来全面的解释Web App和Hybird App,我觉得还有些困难. 这篇文章只是我深入了解移动领域开发过程中的不断整理和总结,其中涉 ...

  6. 关联容器:unordered_map详细介绍(附可运行代码)

    介绍 1 特性 2 Hashtable和bucket 模版 1 迭代器 功能函数 1 构造函数 12示例代码 2 容量操作 21 size 22 empty 3 元素操作 31 find 32 ins ...

  7. Check类之TypeValidation

    (1)Validator类的visitTypeApply()方法 实例1: class TestTypeVal<T extends InputStream>{ TestTypeVal< ...

  8. JavaScript数据结构-4.栈操作

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. java突破------一撸到底(做Java开发,遇到瓶颈是保持现状还是寻求突破?)

    java突破------一撸到底(做Java开发,遇到瓶颈是保持现状还是寻求突破?) 很多人做Java开发2.3年之后,都会觉得自己遇到了瓶颈.什么都会又什么都不会,如何改变困境,为什么很多人写了7. ...

  10. 《Think Python》第15章学习笔记

    目录 <Think Python>第15章学习笔记 15.1 程序员定义的类型(Programmer-defined types) 15.2 属性(Attributes) 15.3 矩形( ...