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 ...
 
随机推荐
- bootstrap中给表格设置display之后表格宽度变小问题解决
			
问题描述:bootstrap中给表格设置display之后表格宽度变小了 解决方案:给表格加上 display:table样式就可以了.
 - Centos7 創建快捷方式
			
usr/share/applications 創建文件 Gogland.desktop [Desktop Entry]Version=1Type=ApplicationName=GoglandGene ...
 - shell脚本面试题
			
Q:1 Shell脚本是什么.它是必需的吗? 答:一个Shell脚本是一个文本文件,包含一个或多个命令.作为系统管理员,我们经常需要使用多个命令来完成一项任务,我们可以添加这些所有命令在一个文本文件( ...
 - xpath的层级与逻辑定位
			
xpath的层级与逻辑定位: 之前我们是通过class和id,name,如果我们所需要的元素没有class,id,name这样的元素,怎么定位呢 1.在不使用xpath情况下:元素没有属性值得时候怎么 ...
 - Java finalize以及Garbage Collection
			
Java的垃圾回收机制: Java的垃圾回收并不等于C++中的析构.Java中,只有在程序濒临存储空间用完的那一刻,对象占用的空间才会释放.所以,在JAVA程序中,我们通常只考虑创建对象,而从不关心对 ...
 - 什么场景应该用 MongoDB(转)
			
很多人比较关心 MongoDB 的适用场景,也有用户在话题里分享了自己的业务场景,比如: 案例1 用在应用服务器的日志记录,查找起来比文本灵活,导出也很方便.也是给应用练手,从外围系统开始使用Mong ...
 - Web服务器和应用服务器简介
			
通俗的讲,Web服务器传送页面使浏览器可以浏览,然而应用程序服务器提供的是客户端应用程序可以调用(call)的方法(methods).确切一点,你可以说:Web服务器专门处理HTTP请求(reques ...
 - c语言定义函数指针和typedef简写
			
二种方法来定义函数指针 #include<stdio.h> #include<stdlib.h> #include<Windows.h> int add(int a ...
 - 内部存储  openFileInputStream  openFileOutputStream
			
package com.qianfeng.gp08_day24_internalstorage; import java.io.FileInputStream; import java.io.File ...
 - 关于bootstrap的认识
			
学习一个框架最好的方法当然就是去它的官网查看它的官方文档,看看官网是怎么描述这个框架的吧------Sleek, intuitive, and powerful front-end framework ...