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 ...
随机推荐
- Winform 无纸化办公-屏幕联动
最近做无纸化办公,对接硬件,用了挺多东西总结一下 技术上主要是:asp.net .winform.activeX控件.chrome插件.socket编程,websocket. 其实看着需求挺简单的,在 ...
- RNA-seq 安装 fastaqc,tophat,cuffilnks,hisat2
------------------------------------------ 安装fastqc------------------------------------------------- ...
- ElementTree 解析xml(minidom解析xml大文件时,MemoryError)
在使用minido解析xml文件时,因为文件过大,结果报错MemoryError.查询后得知是因为minidom在解析时是将所有文件放到内存里的,很占用内存,所以要考虑换一种方法来处理xml文件. ...
- 123. Best Time to Buy and Sell Stock III (Array; DP)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- HttpURLConnection(二)
package com.cmy.urlcon; import java.io.BufferedReader; import java.io.InputStream; import java.io.In ...
- python之列表【list】
这里介绍下列表的功能 #切片:列表[a:b],从下标为a开始,到下标为(b-1)的元素 # name = [0,1,2,3,4,5,6,7,8,9] # print(name[1:6]) # # 结果 ...
- python之类之多继承
class A(object): def test_func(self): print("from A") class B(A): pass # def test_func(sel ...
- 【原创】Junit4详解二:Junit4 Runner以及test case执行顺序和源代码理解
概要: 前一篇文章我们总体介绍了Junit4的用法以及一些简单的测试.之前我有个疑惑,Junit4怎么把一个test case跑起来的,在test case之前和之后我们能做些什么? Junit4执行 ...
- Spring框架的特点
1. 为什么要学习Spring的框架 * 方便解耦,简化开发 * Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理 * AOP编程的支持 * Spring提供面向切面 ...
- phython学习
Python 中文学习大本营 关于作者 赞助本站 The Python Tutorial (Python 2.7.X) 的中文翻译版本.Python Tutorial 为初学 Python 必备官方教 ...