POJ 2777 Count Color (线段树)

 
Count Color
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 29895   Accepted: 8919

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

 
 

分析 一个有n(n<100000)个单位的画板,但颜色只有30种,我没有马上想到位运算,但后来和一个宋词学仙交流的时候她告诉了我可以用位,不过她也是先用的布尔数组来判断的,可能会TLE
这个线段树的提就比较标准了,我先前写的时候一直没有想到好的延时更新的方法,最后慢慢想了一下午和一晚上终于弄对了
AC代码
 #include<iostream>
#include<cstdio>
#define maxx 100005
#define L(u) (u<<1)
#define R(u) (u<<1|1)
using namespace std;
int n,t,Q;
int result,ans;
struct xx{
int ls,rs;
int clr;
int sta;
}node[maxx<<];
void pushup(int u)
{
node[u].clr=node[R(u)].clr|node[L(u)].clr;
}
void pushdown(int u)
{
node[u].sta=;
node[R(u)].clr=node[u].clr;
node[L(u)].clr=node[u].clr;
if(node[L(u)].ls!=node[L(u)].rs)
node[L(u)].sta=;
if(node[R(u)].ls!=node[R(u)].rs)
node[R(u)].sta=;
}
void Build(int u,int l,int r)
{
node[u].ls=l;
node[u].rs=r;
node[u].clr=;
if(l==r)
return;
int mid=(l+r)>>;
Build(L(u),l,mid);
Build(R(u),mid+,r);
}
void Updata(int u,int l,int r,int c)
{
if(l==node[u].ls&&node[u].rs==r)
{
node[u].clr=(<<c);
if(node[u].ls!=node[u].rs)node[u].sta=;
return;
}
if(node[u].sta)pushdown(u); //下传是必要的
int mid=(node[u].ls+node[u].rs)>>;
if(r<=mid) Updata(L(u),l,r,c);
else if(l>mid) Updata(R(u),l,r,c);
else{
Updata(L(u),l,mid,c);
Updata(R(u),mid+,r,c);
}
pushup(u);
}
void Qurey(int u,int l,int r)
{
if(l==node[u].ls&&node[u].rs==r)
{
result=result|node[u].clr;
return;
}
if(node[u].sta)pushdown(u); //下传是必要的
int mid=(node[u].ls+node[u].rs)>>;
if(r<=mid) Qurey(L(u),l,r);
else if(l>mid) Qurey(R(u),l,r);
else{
Qurey(L(u),l,mid);
Qurey(R(u),mid+,r);
}
}
int main()
{
while(~scanf("%d %d %d\n",&n,&t,&Q))
{
Build(,,n);
while(Q--)
{
char x;
int a,b,c;
scanf("%c",&x);
if(x=='P')
{
result=,ans=;
scanf("%d %d\n",&a,&b);
if(a>b)
{a=a+b;b=a-b;a=a-b;}
Qurey(,a,b);
while(result)
{
result>>=;
if(&result)ans++;
}
cout<<ans<<endl;
}
else
{
scanf("%d %d %d\n",&a,&b,&c);
if(a>b)
{a=a+b;b=a-b;a=a-b;}//注意查询的区间可能左大于右数据怪怪的
Updata(,a,b,c);
}
}
}
return ;
}

Count Colour_poj2777(线段树+位)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. poj 3225 线段树+位运算

    略复杂的一道题,首先要处理开闭区间问题,扩大两倍即可,注意输入最后要\n,初始化不能随便memset 采用线段树,对线段区间进行0,1标记表示该区间是否包含在s内U T S ← S ∪ T 即将[l, ...

  8. poj 2777 Count Color(线段树)

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

  9. Subsequence Count (线段树)

    Time Limit: 1000 ms   Memory Limit: 256 MB Description 给定一个01串 $S_{1 \cdots n}$ 和 $Q$ 个操作. 操作有两种类型: ...

随机推荐

  1. JAVA的包装类 【转】

    Java语言是一个面向对象的语言,但是Java中的基本数据类型却是不面向对象的,这在实际使用时存在很多的不便,为了解决这个不足,在设计类时为每个基本数据类型设计了一个对应的类进行代表,这样八个和基本数 ...

  2. Provisional headers are shown,本地测试成功,服务器运行却失败

    基于MVC的项目 具体情况是一个页面在访问的时候进不了首页,但详细页面却可以进去 下面说说解决方法和思路,以便找出问题所在 第一:把服务器代码下载到本地运行,代码是否出错,出错了,问题找到了,没出错接 ...

  3. Attribute "resource" must be declared for element type "mapper".

    今天在玩mybatis的时候,遇到这个奇葩问题. 最后发现,原因是 dtd文件配置错误了.错把Mapper的直接copy过来 把DOCTYPE mapper改成configuration,Mapper ...

  4. PHP的轻量消息队列php-resque使用说明

    日志未经声明,均为AlloVince原创.版权采用『 知识共享署名-非商业性使用 2.5 许可协议』进行许可. 消息队列处理后台任务带来的问题 项目中经常会有后台运行任务的需求,比如发送邮件时,因为要 ...

  5. PostgreSQL用户角色及其属性介绍

    1.CREATE ROLE创建的用户默认不带LOGIN属性,而CREATE USER创建的用户默认带有LOGIN属性,如下: postgres=# CREATE ROLE pg_test_user_1 ...

  6. 写一个ajax程序就是如此简单

    写一个ajax程序就是如此简单 ajax介绍: 1:AJAX全称为Asynchronous JavaScript and XML(异步JavaScript和XML),指一种创建交互式网页应用的网页开发 ...

  7. python第十三天

    这几天在欺负docker兽,很是开心,但是没想到领导突然让我去殴打openstack兽,完全没有兴趣嘛,毕竟前一阵一直在吊打它,当然啦,对于愚蠢的人类而言openstack兽是十分强大的,不过对于北方 ...

  8. 写shell脚本速查笔记

    linux shell脚本的语法蛋疼,而且对于java开发人员来说又不常用,常常是学了一次等到下次用的时候又忘记了.因此制作这个速查笔记,用于要写shell脚本时快速回忆&速查. 获取当前脚本 ...

  9. 附10 kibana创建新的index patterns

    elk整体架构图: 一.logstash indexer 配置文件: input { stdin{} } filter { } output { elasticsearch { hosts => ...

  10. jquery 滚动到底部加载

    var body_ = $(window); var indexPage = 2; var pageCount = <?php echo $pageCount;?>; var _ajaxR ...