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 ...
随机推荐
- @Transactionl注解
spring 事务注解 默认遇到throw new RuntimeException("...");会回滚 需要捕获的throw new Exception("...&q ...
- 尴尬!Jsp内置对象
今天挺尴尬的,上网络安全课做错了ppt ,尴尬到头皮发麻. JSP内置对象 JSP内置对象是Web容器创建的一组对象,不使用new关就可以使用的内置对象. <%int[ ]value= {60, ...
- MVC 发布到IIS中的配置方法
MVC 发布到IIS中的配置方法 http://msdn.microsoft.com/zh-cn/library/gg703322(v=vs.98).aspx
- Mybatis-Generator自动生成Dao、Model、Mapping等相关映射文件(懒人版)
今天在学习mybatis生成相关的映射文件的时候,发现了往期的生成Dao.Model.Mapping等文章多数都是一样的,我也在学着重复造轮子,不过是懒人造的.本文旨在解决开发过程,简化配置文件的“手 ...
- css3将图片、内容换为灰色
直接用filter属性-webkit-filter: grayscale(100%);-moz-filter: grayscale(100%);-ms-filter: grayscale(100%); ...
- 【校招面试 之 C/C++】第26题 C++ 智能指针(二)之 share_ptr
1.综述 shared_ptr 是一个标准的共享所有权的智能指针, 允许多个指针指向同一个对象. 定义在 memory 文件中(非memory.h), 命名空间为 std. shared_ptr 是为 ...
- DICOM
DICOM(Digital Imaging and Communications in Medicine)即医学数字成像和通信,是医学图像和相关信息的国际标准(ISO 12052).它定义了质量能满足 ...
- 1. Install Git and GitExtension
Install Git Step 1: Run
- 5 个关键点!优化你的 UI 原型设计
当你和你的团队着手开始一个产品开发的时候,最开始的一步一般是绘制线框图,这是大部分产品项目的第一步,它不复杂但是却对整个产品的完成形态和质量有着至关重要的作用. 很多刚开始工作设计师或者产品经理都会提 ...
- 编写可维护的JavaScript 收纳架
如果你看过Nicolas C.Zakas写过的任何作品,你必须承认他是个不折不扣的天才.也只有天才级的才能写出<JavaScript高级程序设计>让所有的前端攻城师人手一本.Nicolas ...