Count Colour_poj2777(线段树+位)
POJ 2777 Count Color (线段树)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 29895 | Accepted: 8919 |
Description
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
Output
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
#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(线段树+位)的更多相关文章
- Count Color(线段树+位运算 POJ2777)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39917 Accepted: 12037 Descrip ...
- poj 2777 Count Color - 线段树 - 位运算优化
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42472 Accepted: 12850 Description Cho ...
- [poj2777] Count Color (线段树 + 位运算) (水题)
发现自己越来越傻逼了.一道傻逼题搞了一晚上一直超时,凭啥子就我不能过??? 然后发现cin没关stdio同步... Description Chosen Problem Solving and Pro ...
- poj 2777 Count Color(线段树、状态压缩、位运算)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38921 Accepted: 11696 Des ...
- poj 2777 Count Color(线段树区区+染色问题)
题目链接: poj 2777 Count Color 题目大意: 给出一块长度为n的板,区间范围[1,n],和m种染料 k次操作,C a b c 把区间[a,b]涂为c色,P a b 查 ...
- POJ 2777 Count Color(线段树之成段更新)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...
- poj 3225 线段树+位运算
略复杂的一道题,首先要处理开闭区间问题,扩大两倍即可,注意输入最后要\n,初始化不能随便memset 采用线段树,对线段区间进行0,1标记表示该区间是否包含在s内U T S ← S ∪ T 即将[l, ...
- poj 2777 Count Color(线段树)
题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- Subsequence Count (线段树)
Time Limit: 1000 ms Memory Limit: 256 MB Description 给定一个01串 $S_{1 \cdots n}$ 和 $Q$ 个操作. 操作有两种类型: ...
随机推荐
- study
1.perf, top, vtune, /sys/kernel/debug/mid_pmu_states使用 2.cpu hotplug 3.camera record时有可能耗电的地方: 硬件加速是 ...
- Thinkpad 笔记本VMware Workstation 安装虚拟机出现“此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态”解决方法
今天在使用VMware打算在机器中安装新的虚拟机时,出现"此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态"错误如下: 提示信息: 已将该虚拟机配 ...
- FMDB实用攻略
一.首先创建模型类 User.h #import <Foundation/Foundation.h> @interface User : NSObject @property(nonato ...
- 计算hashCode的常见方法
把某个非零常数值,比如说17,保存在一个叫result的int类型的变量中. 2.对于对象中每一个关键域f(值equals方法中考虑的每一个域),完成以下步骤: a.为该域计算int类型的散列吗c: ...
- 初探 performance – 监控网页与程序性能
使用 window.performance 提供了一组精确的数据,经过简单的计算就能得出一些网页性能数据. 配合上报一些客户端浏览器的设备类型等数据,就可以实现简单的统计啦! 额,先看下兼容性如何:h ...
- C# 數據事務操作
public sealed class SQLFunc { #region Methods #region OpenConnection /// <summary>指定包含連接字串的字串 ...
- GBDT基本理论及利用GBDT组合特征的具体方法(收集的资料)
最近两天在学习GBDT,看了一些资料,了解到GBDT由很多回归树构成,每一棵新回归树都是建立在上一棵回归树的损失函数梯度降低的方向. 以下为自己的理解,以及收集到的觉着特别好的学习资料. 1.GBDT ...
- git免密码pull,push
执行git config --global credential.helper store
- Linux课程实践一:Linux基础实践(基础操作)
一.软件源维护 1. 基本操作 (1)查看源列表 sudo vim /etc/apt/sources.list deb:二进制软件安装包 deb-src:源码包 (2)备份软件源列表 sudo cp ...
- python学习笔记3-celery分布式任务处理器
celery是用python写的一个异步的任务框架,功能非常强大,具体的说明可以查看官网,这里主要提供点demo让你迅速使用该框架 1.环境安装 默认安装好了redis pip install c ...