hdu 5023 线段树+状压
http://acm.hdu.edu.cn/showproblem.php?pid=5023
在片段上着色,有两种操作,如下:
第一种:P a b c 把 a 片段至 b 片段的颜色都变为 c 。
第二种:Q a b 询问 a 片段至 b 片段有哪些颜色,把这些颜色按从小到大的编号输出,不要有重复
片段上默认的初始颜色为编号2的颜色。
颜色30种,状压;线段树进行更新和询问
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std; const int N = 1000005;
int ans[35];
void cntbit(int x)
{
int cnt = 0;
for(int i = 1;i <= 30;++i){
if(x&(1<<(i-1)))
ans[cnt++] = i;
}
for(int i = 0;i < cnt;++i)
printf("%d%c",ans[i]," \n"[i == cnt-1]);
return;
}
struct node
{
int l,r;
int color;//color用位1个数记录那些颜色被涂抹
bool leaf;//当线段恰好覆盖一个节点的区间时就直接对该节操作而不再向下操作
//对于这种线段树,要在获得整块区间时停止并把该节点的leaf改为true
}s[N*3]; void build(int l,int r,int root)
{
s[root].l = l;
s[root].r = r;
s[root].leaf = false;
if(l == r){
return;
}
int mid = (l+r)>>1;
build(l,mid,root<<1);
build(mid+1,r,(root<<1)+1);
return;
}
void insert(int l,int r,int root,int color)
{
if(l == s[root].l && s[root].r == r){
s[root].color = color;
s[root].leaf = true;
return;
}
if(s[root].leaf){
s[root].leaf = false;
s[root<<1].leaf = true;
s[(root<<1)+1].leaf = true;
s[root<<1].color = s[root].color;
s[(root<<1)+1].color = s[root].color;
}
int mid = (s[root].l+s[root].r)>>1;
if(mid >= r)
insert(l,r,root<<1,color);
else if(l > mid)
insert(l,r,(root<<1)+1,color);
else{
insert(l,mid,root<<1,color);
insert(mid+1,r,(root<<1)+1,color);
}
s[root].color = s[root<<1].color | s[(root<<1)+1].color;
}
int query(int l,int r,int root)
{
//s[root].leaf == true的判断不能丢
if( s[root].leaf || (s[root].l == l && s[root].r == r)){
return s[root].color;
}
int mid = (s[root].l + s[root].r)>>1;
if(r <= mid){
return query(l,r,root<<1);
}else if(l > mid){
return query(l,r,(root<<1)+1);
}else{
return query(l,mid,root<<1) | query(mid+1,r,(root<<1)+1);
}
}
int main()
{
int n,q;
while(~scanf("%d%d",&n,&q),n|q){
build(1,n,1);
insert(1,n,1,2);
int l,r,col;
char ss[5];
while(q--){
scanf("%s%d%d",ss,&l,&r);
if(l > r)
swap(l,r);
if(ss[0] == 'P'){
scanf("%d",&col);
insert(l,r,1,1<<(col-1));
}
else{
cntbit(query(l,r,1));
}
}
}
return 0;
}
hdu 5023 线段树+状压的更多相关文章
- POJ:2777-Count Color(线段树+状压)
Count Color Time Limit: 1000MS Memory Limit: 65536K Description Chosen Problem Solving and Program d ...
- POJ 3468 线段树+状压
题意:给你n个数,有对区间的加减操作,问某个区间的和是多少. 思路:状压+线段树(要用lazy标记,否则会TLE) //By SiriusRen #include <cstdio> #in ...
- Bzoj 3813 奇数国 题解 数论+线段树+状压
3813: 奇数国 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 748 Solved: 425[Submit][Status][Discuss] ...
- HDU 5023线段树区间染色,统计区间内颜色个数
这个也是一个线段树的模板 #include<iostream> #include<string.h> #include<algorithm> #include< ...
- hdu 5023 线段树+位运算
主要考线段树的区间修改和区间查询,这里有一个问题就是这么把一个区间的多种颜色上传给父亲甚至祖先节点,在这里题目告诉我们最多30颜色,那么我们可以把这30中颜色用二进制储存和传给祖先节点,二进制的每一位 ...
- poj2777Count Color——线段树+状压
题目:http://poj.org/problem?id=2777 状压每个颜色的选择情况,取答案时 | 一番: 注意题目中的区间端点可能大小相反,在读入时换一下位置: 注意pushdown()中要l ...
- hdu 5023 线段树延迟更新+状态压缩
/* 线段树延迟更新+状态压缩 */ #include<stdio.h> #define N 1100000 struct node { int x,y,yanchi,sum; }a[N* ...
- HDU_3071 Gcd & Lcm game 【素数分解 + 线段树 + 状压】
一.题目 Gcd & Lcm game 二.分析 非常好的一题. 首先考虑比较暴力的做法,肯定要按区间进行处理,对于$lcm$和$gcd$可以用标准的公式进行求,但是求$lcm$的时候是肯定 ...
- hdu 5023 线段树
成端更新+统计区间内的值 挺模板的题... 一开始没想起来用set统计,傻傻地去排序了[大雾 #include<iostream> #include<cstdio> #incl ...
随机推荐
- git 使用 添加分支
http://jingyan.baidu.com/album/19192ad83ea879e53e5707ce.html?picindex=1 修改配置 git config --global use ...
- RNA-seq 安装 fastaqc,tophat,cuffilnks,hisat2
------------------------------------------ 安装fastqc------------------------------------------------- ...
- sshd_config优化
sshd_config优化linux系统调优,参考百度搜索 linux ssh命令 /etc/init.d/sshd restart 重启ssh 193 ls 194 vim /e ...
- 通过阿里OSS文件服务返回的URL获取文件流下载
我们都知道将文件上传到阿里的OSS文件服务上后,可以通过generatePresignedUrl(bucketName, key, expiration)方法获取该文件的防问路径,但是当我们知道该文件 ...
- 【c++】多层次继承类对象的构造函数参数的传递方法
#include <iostream.h> //基类CBase class CBase { int a; public: CBase(int na) { a=na; cout<< ...
- collections系列之OrderedDict【有序字典】与DefaultDict【默认字典】
今天来向大家介绍一下collections系列中的OrderedDict和DefaultDict,这两种类均是通过collections来创建的,均是对dict字典加工,所有都继承了dict字典的方法 ...
- 3-QT程序运行时报错E:\SogouInput\6.7.0.0329\程序异常终止,该怎么解决?
https://bbs.csdn.net/topics/390653779 出现这个错误的原因,使用声明的对象时,没有使用new对对象进行实例化. 包括:数组.
- silverlight的Datagrid控件列绑定属性笔记
<data:DataGridTemplateColumn Header="给作者留言"> <data:DataGridTemplateColumn.CellTem ...
- Redis数据持久化
持久化选项 Redis提供了两种不同的持久化方法来将数据存储到硬盘里面.一种方法叫快照(snapshotting),它可以将存在于某一时刻的所有数据都写入硬盘里面.另一种方法叫只追加文件(append ...
- [PHP]require include