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$ 个操作. 操作有两种类型: ...
随机推荐
- hive和ORACLE语法对比
- SQLServer 常用日期处理
select GETDATE() as '当前日期',DateName(year,GetDate()) as '年',DateName(month,GetDate()) as '月',DateName ...
- Opera放弃自家内核转投WebKit的背后(转)
Opera在2月13日宣布用户突破3亿,并且带着这3亿用户投入WebKit阵营,自家的Presto内核将会走入历史.Opera为什么选择在现在这个时间点放弃自有内核?之前Opera的坚持自主研发一直被 ...
- openldap自定义schema
参考官方文档 13. Schema Specification http://www.verydemo.com/demo_c161_i74426.html https://oskb.wordpress ...
- CSS3 初步学习
CSS3有一些是与旧版CSS2.1重叠的,有一些是没有浏览器支持的,全学没必要,下面只记录一下有用的. 一.CSS3边框 1.圆角border-radius border-radius:值越大,角越圆 ...
- Linux:安装图形界面
能连接网络的前提下,使用yum安装 yum groupinstall -y "Desktop"yum groupinstall -y "X Window Syste ...
- Winform数据导出Execl小工具
前台界面.cs文件 using System; using System.Collections.Generic; using System.ComponentModel; using System. ...
- TJI读书笔记10-复用类
TJI读书笔记10-复用类 组合语法 继承语法 代理 final关键字 final的数据 final的参数 final的方法 final的类 初始化和类的加载 乱七八糟不知道怎么归类的知识点 代码复用 ...
- IMoniker接口的成员
- ORACLE 回收站导致的故障
ORACLE 回收站导致的故障 一.故障 (1)现象 一个生产环境,oracle数据库挂死,严重影响生产.查死锁sql,发现大量日志插入语句,并且每条运行时间都超过一分钟,插入非常缓慢.据分析 ...