http://acm.hdu.edu.cn/showproblem.php?pid=5023

在片段上着色,有两种操作,如下:

第一种:P a b c 把 a 片段至 b 片段的颜色都变为 c 。

第二种:Q a b 询问 a 片段至 b 片段有哪些颜色,把这些颜色按从小到大的编号输出,不要有重复

片段上默认的初始颜色为编号2的颜色。

颜色30种,状压;线段树进行更新和询问

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. const int N = 1000005;
  8. int ans[35];
  9. void cntbit(int x)
  10. {
  11. int cnt = 0;
  12. for(int i = 1;i <= 30;++i){
  13. if(x&(1<<(i-1)))
  14. ans[cnt++] = i;
  15. }
  16. for(int i = 0;i < cnt;++i)
  17. printf("%d%c",ans[i]," \n"[i == cnt-1]);
  18. return;
  19. }
  20. struct node
  21. {
  22. int l,r;
  23. int color;//color用位1个数记录那些颜色被涂抹
  24. bool leaf;//当线段恰好覆盖一个节点的区间时就直接对该节操作而不再向下操作
  25. //对于这种线段树,要在获得整块区间时停止并把该节点的leaf改为true
  26. }s[N*3];
  27.  
  28. void build(int l,int r,int root)
  29. {
  30. s[root].l = l;
  31. s[root].r = r;
  32. s[root].leaf = false;
  33. if(l == r){
  34. return;
  35. }
  36. int mid = (l+r)>>1;
  37. build(l,mid,root<<1);
  38. build(mid+1,r,(root<<1)+1);
  39. return;
  40. }
  41. void insert(int l,int r,int root,int color)
  42. {
  43. if(l == s[root].l && s[root].r == r){
  44. s[root].color = color;
  45. s[root].leaf = true;
  46. return;
  47. }
  48. if(s[root].leaf){
  49. s[root].leaf = false;
  50. s[root<<1].leaf = true;
  51. s[(root<<1)+1].leaf = true;
  52. s[root<<1].color = s[root].color;
  53. s[(root<<1)+1].color = s[root].color;
  54. }
  55. int mid = (s[root].l+s[root].r)>>1;
  56. if(mid >= r)
  57. insert(l,r,root<<1,color);
  58. else if(l > mid)
  59. insert(l,r,(root<<1)+1,color);
  60. else{
  61. insert(l,mid,root<<1,color);
  62. insert(mid+1,r,(root<<1)+1,color);
  63. }
  64. s[root].color = s[root<<1].color | s[(root<<1)+1].color;
  65. }
  66. int query(int l,int r,int root)
  67. {
  68. //s[root].leaf == true的判断不能丢
  69. if( s[root].leaf || (s[root].l == l && s[root].r == r)){
  70. return s[root].color;
  71. }
  72. int mid = (s[root].l + s[root].r)>>1;
  73. if(r <= mid){
  74. return query(l,r,root<<1);
  75. }else if(l > mid){
  76. return query(l,r,(root<<1)+1);
  77. }else{
  78. return query(l,mid,root<<1) | query(mid+1,r,(root<<1)+1);
  79. }
  80. }
  81. int main()
  82. {
  83. int n,q;
  84. while(~scanf("%d%d",&n,&q),n|q){
  85. build(1,n,1);
  86. insert(1,n,1,2);
  87. int l,r,col;
  88. char ss[5];
  89. while(q--){
  90. scanf("%s%d%d",ss,&l,&r);
  91. if(l > r)
  92. swap(l,r);
  93. if(ss[0] == 'P'){
  94. scanf("%d",&col);
  95. insert(l,r,1,1<<(col-1));
  96. }
  97. else{
  98. cntbit(query(l,r,1));
  99. }
  100. }
  101. }
  102. return 0;
  103. }

hdu 5023 线段树+状压的更多相关文章

  1. POJ:2777-Count Color(线段树+状压)

    Count Color Time Limit: 1000MS Memory Limit: 65536K Description Chosen Problem Solving and Program d ...

  2. POJ 3468 线段树+状压

    题意:给你n个数,有对区间的加减操作,问某个区间的和是多少. 思路:状压+线段树(要用lazy标记,否则会TLE) //By SiriusRen #include <cstdio> #in ...

  3. Bzoj 3813 奇数国 题解 数论+线段树+状压

    3813: 奇数国 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 748  Solved: 425[Submit][Status][Discuss] ...

  4. HDU 5023线段树区间染色,统计区间内颜色个数

    这个也是一个线段树的模板 #include<iostream> #include<string.h> #include<algorithm> #include< ...

  5. hdu 5023 线段树+位运算

    主要考线段树的区间修改和区间查询,这里有一个问题就是这么把一个区间的多种颜色上传给父亲甚至祖先节点,在这里题目告诉我们最多30颜色,那么我们可以把这30中颜色用二进制储存和传给祖先节点,二进制的每一位 ...

  6. poj2777Count Color——线段树+状压

    题目:http://poj.org/problem?id=2777 状压每个颜色的选择情况,取答案时 | 一番: 注意题目中的区间端点可能大小相反,在读入时换一下位置: 注意pushdown()中要l ...

  7. hdu 5023 线段树延迟更新+状态压缩

    /* 线段树延迟更新+状态压缩 */ #include<stdio.h> #define N 1100000 struct node { int x,y,yanchi,sum; }a[N* ...

  8. HDU_3071 Gcd & Lcm game 【素数分解 + 线段树 + 状压】

    一.题目  Gcd & Lcm game 二.分析 非常好的一题. 首先考虑比较暴力的做法,肯定要按区间进行处理,对于$lcm$和$gcd$可以用标准的公式进行求,但是求$lcm$的时候是肯定 ...

  9. hdu 5023 线段树

    成端更新+统计区间内的值 挺模板的题... 一开始没想起来用set统计,傻傻地去排序了[大雾 #include<iostream> #include<cstdio> #incl ...

随机推荐

  1. Winform 无纸化办公-屏幕联动

    最近做无纸化办公,对接硬件,用了挺多东西总结一下 技术上主要是:asp.net .winform.activeX控件.chrome插件.socket编程,websocket. 其实看着需求挺简单的,在 ...

  2. RNA-seq 安装 fastaqc,tophat,cuffilnks,hisat2

    ------------------------------------------ 安装fastqc------------------------------------------------- ...

  3. ElementTree 解析xml(minidom解析xml大文件时,MemoryError)

    在使用minido解析xml文件时,因为文件过大,结果报错MemoryError.查询后得知是因为minidom在解析时是将所有文件放到内存里的,很占用内存,所以要考虑换一种方法来处理xml文件.   ...

  4. 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 ...

  5. HttpURLConnection(二)

    package com.cmy.urlcon; import java.io.BufferedReader; import java.io.InputStream; import java.io.In ...

  6. python之列表【list】

    这里介绍下列表的功能 #切片:列表[a:b],从下标为a开始,到下标为(b-1)的元素 # name = [0,1,2,3,4,5,6,7,8,9] # print(name[1:6]) # # 结果 ...

  7. python之类之多继承

    class A(object): def test_func(self): print("from A") class B(A): pass # def test_func(sel ...

  8. 【原创】Junit4详解二:Junit4 Runner以及test case执行顺序和源代码理解

    概要: 前一篇文章我们总体介绍了Junit4的用法以及一些简单的测试.之前我有个疑惑,Junit4怎么把一个test case跑起来的,在test case之前和之后我们能做些什么? Junit4执行 ...

  9. Spring框架的特点

    1. 为什么要学习Spring的框架 * 方便解耦,简化开发 * Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理 * AOP编程的支持 * Spring提供面向切面 ...

  10. phython学习

    Python 中文学习大本营 关于作者 赞助本站 The Python Tutorial (Python 2.7.X) 的中文翻译版本.Python Tutorial 为初学 Python 必备官方教 ...