POJ 2777 Count Color【线段树】
题目大意:要求完成以下两个操作:1.将一个区间刷上一种颜色2.询问一段区间上有多少种颜色
思路:这两个操作线段树都可以很迅速的完成,具体做法是:线段树上每个节点存这个线段上的颜色数量,由于颜色数很少,因此可以用二进制存颜色,如果二进制的第N位是1,则该区间存在颜色N,因此一个节点等于其两个子节点颜色的或。最后一个问题就是修改操作是对区间修改,因此需要用lazy_tag的思想(虽然这里不完全是),不然一个一个插节点会TLE
#include<cstdio>
#include<string.h>
#include<iostream>
using namespace std;
int tree[500000]={0};
bool lazy[500000]={0};
void swap(int &x,int &y){int temp=x;x=y;y=temp;}
int search(int node,int l,int r,int lq,int rq)
{
int mid=(l+r)>>1,temp=0;
if (lazy[node]==true){return tree[node];}
if (lq<=l && r<=rq){return tree[node];}
else
{
if (lazy[node]==1){lazy[node<<1]=1;tree[node<<1]=tree[node];
tree[(node<<1)+1]=tree[node];lazy[(node<<1)+1]=1;}
if (lq<=mid)temp=search(node<<1,l,mid,lq,rq);
if (mid<rq)temp=temp | search((node<<1)+1,mid+1,r,lq,rq);
}
return temp;
}
void insert(int node,int l,int r,int lq,int rq,int color)
{
int mid=(l+r)>>1;
if (lq<=l && r<=rq){tree[node]=1<<(color-1);lazy[node]=true;return;}
else
{
if (lazy[node]==1)
{
lazy[node]=0;lazy[node<<1]=1;
tree[node<<1]=tree[node];tree[(node<<1)+1]=tree[node];
lazy[(node<<1)+1]=1;
}
if (lq<=mid)insert(node<<1,l,mid,lq,rq,color);
if (rq>mid)insert((node<<1)+1,mid+1,r,lq,rq,color);
}
tree[node]=tree[node<<1] | tree[(node<<1)+1];
}
int f(int u)
{
int t=0;while(u!=0){if ((u & 1)==1)t++;u>>=1;}
return t;
}
int main()
{
int t,o,l,left,right,color;
char ch[2];
scanf("%d%d%d",&l,&t,&o);
for(int i=1;i<=4*l;i++)tree[i]=1;
for(int v=1;v<=o;v++)
{
scanf("%s",ch);
if (ch[0]=='C')
{
scanf("%d%d%d",&left,&right,&color);
if (right<left)swap(right,left);
insert(1,1,l,left,right,color);
}
else
{
scanf("%d%d",&left,&right);
if (right<left)swap(right,left);
int u=search(1,1,l,left,right);
printf("%d\n",f(u));
}
}
return 0;
}
POJ 2777 Count Color【线段树】的更多相关文章
- 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(线段树)
题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- poj 2777 Count Color - 线段树 - 位运算优化
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42472 Accepted: 12850 Description Cho ...
- poj 2777 Count Color(线段树、状态压缩、位运算)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38921 Accepted: 11696 Des ...
- POJ 2777 Count Color(线段树之成段更新)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- POJ P2777 Count Color——线段树状态压缩
Description Chosen Problem Solving and Program design as an optional course, you are required to sol ...
- POJ 2777 Count Color(段树)
职务地址:id=2777">POJ 2777 我去.. 延迟标记写错了.标记到了叶子节点上.. . . 这根本就没延迟嘛.. .怪不得一直TLE... 这题就是利用二进制来标记颜色的种 ...
- poj 2777 Count Color
题目连接 http://poj.org/problem?id=2777 Count Color Description Chosen Problem Solving and Program desig ...
- POJ 2777 Count Color(线段树染色,二进制优化)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42940 Accepted: 13011 Des ...
随机推荐
- (转 )Unity对Lua的编辑器拓展
转 http://blog.csdn.net/ZhangDi2017/article/details/61203505 当前版本的Unity(截至Unity5.5.x)中TextAsset类不支持后缀 ...
- discuz x2.5用户注册后邮箱认证后无法收到邮件或者直接进垃圾箱
又是一个周末,jquery特效继续折腾我那discuz论坛,我开启了个邮箱验证,恶意注册的太恶心了,没有办法. 能稍微屏蔽点,但是问题来了,据亲们反应,无法收到验证邮件,或者有时间直接进入垃圾箱,这个 ...
- MATLAB GUI制作快速入门
创建空白的GUI在MATLAB命令行中输入guide新建GUI,选择Blank GUI (Default),点击确定后就生成了一个空白的GUI制作界面,如下图所示 图1制作GUI的具体过程简单加法器将 ...
- 超不清视频播放器-用Python将视频转成字符
前言 今天分享的这段代码,看起来没啥实际用处,而且有些反潮流,因为现如今大家看视频都追求更高分辨率的超清画质,而我们这个,是一个“超不清”的视频播放器:在控制台里播放视频,用字符来表示画面 不过我觉得 ...
- 时间函数datetime time
time模块 time翻译过来就是时间,有我们其实在之前编程的时候有用到过. #常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行.单位为秒. 2.time.time() 获取 ...
- c++内联函数解析(inline)
一.基本定义 inline是C++语言中的一个关键字,可以用于程序中定义内联函数,inline的引进使内联函数的定义更加简单.说到内联函数,这里给出比较常见的定义,内联函数是C++中的一种特殊函数,它 ...
- Linux用户和组的概念
目 录 第1章 用户和组存在的关系 1 1.1 我们现在所使用的操作系统都是多用户操作系统 1 1.2 id命令查看当前登陆的用户信息 1 1.3 用户UID的分类 1 1.4 ...
- 条款21:必须返回对象时,别妄想返回其reference(Don't try to return a reference when you must return an object)
NOTE: 1.绝不要返回pointer或reference 指向一个local stack 对象,或返回reference 指向一个heap-allocated对象,或返回pointer 或refe ...
- 第五章:C++程序的结构
主要内容: 1.作用域与可见性 2.对象的生存期 3.数据与函数 4.静态成员 5.共享数据的保护 6.友元 7.编译预处理命令 8.多文件结构和工程 作用域:函数原型作用域.块作用域.类作用域.文件 ...
- 一个关于vue+mysql+express的全栈项目(四)------ sequelize中部分解释
一.模型的引入 引入db.js const sequelize = require('./db') sequelize本身就是一个对象,他提供了众多的方法, const account = seque ...