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. Java常见错误及解决方案

    1.类定义未找到:java.lang.NoClassDefFoundError java类文件没有上传:上传了,服务器没找到,建议将JSP页面重新更新或重启服务器. 2.

  2. 通过设置Ionic-Cli代理解决ionic serve跨域调试问题

    Ionic-Cli代理设置: 打开ionic.config.json文件,添加proxies代理配置字段: { "name": "ion", "app ...

  3. github上关于campbell数据采集的一些代码。

    数据自动采集: https://github.com/USGS-OWI/deployer-campbell   program that reads loggernet files and refor ...

  4. Attribute基本介绍

    一.基础知识点 1.什么是Attribute? MSDN:公共语言运行时允许你添加类似关键字的说明,叫做Attribute,它可以对程序中的元素进行标注,如类型.字段.方法和属性等.Attribute ...

  5. JavaScript数据结构-7.链表

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

  6. SOA与微服务

    SOA 面向服务架构,它可以根据需求通过网络对松散耦合的粗粒度应用组件进行分布式部署.组合和使用.服务层是SOA的基础,可以直接被应用调用,从而有效控制系统中与软件代理交互的人为依赖性. SOA是一种 ...

  7. linux 安装jdk 二进制版本,非安装版

    0.下载jdk8 登录网址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html选择对 ...

  8. QuestaSim自动化仿真之do文件

    一.编写基本的do文件 下面按照实际仿真的步骤来说明do文件中需要用到的各个tcl命令. 1.quit -sim ---- 退出原来的仿真工程: 2.cd ---- 设置工作目录的路径,就是仿真工程路 ...

  9. Spring中使用JMS

    JMS为了Java开发人员与消息代理(message broker)交互和收发消息提供了一套标准API.而且,由于每个message broker都支持JMS,所以我们就不需要学习额外的消息API了. ...

  10. Angular 应用中的登陆与身份验证

    Angular 经常会被用到后台和管理工具的开发,这两类都会需要对用户进行鉴权.而鉴权的第一步,就是进行身份验证.由于 Angular 是单页应用,会在一开始,就把大部分的资源加载到浏览器中,所以就更 ...