Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 53312   Accepted: 16050

Description

Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem.

There is a very long board with length L centimeter, L is a positive integer, so we can evenly divide the board into L segments, and they are labeled by 1, 2, ... L from left to right, each is 1 centimeter long. Now we have to color the board - one segment with only one color. We can do following two operations on the board:

1. "C A B C" Color the board from segment A to segment B with color C. 
2. "P A B" Output the number of different colors painted between segment A and segment B (including).

In our daily life, we have very few words to describe a color (red, green, blue, yellow…), so you may assume that the total number of different colors T is very small. To make it simple, we express the names of colors as color 1, color 2, ... color T. At the beginning, the board was painted in color 1. Now the rest of problem is left to your.

Input

First line of input contains L (1 <= L <= 100000), T (1 <= T <= 30) and O (1 <= O <= 100000). Here O denotes the number of operations. Following O lines, each contains "C A B C" or "P A B" (here A, B, C are integers, and A may be larger than B) as an operation defined previously.

Output

Ouput results of the output operation in order, each line contains a number.

Sample Input

  1. 2 2 4
  2. C 1 1 2
  3. P 1 2
  4. C 2 2 2
  5. P 1 2

Sample Output

  1. 2
  2. 1

题意就是区间染色,然后查询区间颜色数量,不能直接线段树上更新值,会被覆盖掉,所以要用二进制状态压缩一下,学到了小技巧,用二进制进行状态压缩。

有关位运算的老是记不住,笨死了。。。

贴一下大佬的有关位运算的:

位运算总结(按位与,或,异或)

还有一篇有关二进制状态压缩的:

状态压缩 位运算

本篇博客完全就是为了备忘才水的。。。

我写的时候,初始化写挫了,查询的时候,ret应该初始化为0,我写的1。。。

运算规则:0|0=0;  0|1=1;  1|0=1;   1|1=1;

即 :参加运算的两个对象只要有一个为1,其值为1。

代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<bitset>
  6. #include<cassert>
  7. #include<cctype>
  8. #include<cmath>
  9. #include<cstdlib>
  10. #include<ctime>
  11. #include<deque>
  12. #include<iomanip>
  13. #include<list>
  14. #include<map>
  15. #include<queue>
  16. #include<set>
  17. #include<stack>
  18. #include<vector>
  19. using namespace std;
  20. typedef long long ll;
  21. typedef long double ld;
  22. typedef pair<int,int> pii;
  23.  
  24. const double PI=acos(-1.0);
  25. const double eps=1e-;
  26. const ll mod=1e9+;
  27. const int inf=0x3f3f3f3f;
  28. const int maxn=1e5+;
  29. const int maxm=+;
  30. #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  31. #define lson l,m,rt<<1
  32. #define rson m+1,r,rt<<1|1
  33.  
  34. int tree[maxn<<],lazy[maxn<<];
  35.  
  36. void pushup(int rt)
  37. {
  38. tree[rt]=tree[rt<<]|tree[rt<<|];//颜色汇总
  39. }
  40.  
  41. void pushdown(int rt)
  42. {
  43. if(lazy[rt]){
  44. lazy[rt<<]=lazy[rt<<|]=lazy[rt];
  45. tree[rt<<]=tree[rt<<|]=lazy[rt];
  46. lazy[rt]=;
  47. }
  48. }
  49.  
  50. void build(int l,int r,int rt)
  51. {
  52. lazy[rt]=;
  53. tree[rt]=;
  54. if(l==r){
  55. return ;
  56. }
  57.  
  58. int m=(l+r)>>;
  59. build(lson);
  60. build(rson);
  61. pushup(rt);
  62. }
  63.  
  64. void update(int L,int R,int c,int l,int r,int rt)
  65. {
  66. if(r<L||l>R) return ;
  67. if(L<=l&&r<=R){
  68. lazy[rt]=<<(c-);//二进制状态压缩保存颜色
  69. tree[rt]=<<(c-);
  70. return ;
  71. }
  72.  
  73. pushdown(rt);
  74. int m=(l+r)>>;
  75. if(L<=m) update(L,R,c,lson);
  76. if(R> m) update(L,R,c,rson);
  77. pushup(rt);
  78. }
  79.  
  80. int query(int L,int R,int l,int r,int rt)
  81. {
  82. if(r<L||l>R) return ;
  83. if(L<=l&&r<=R){
  84. return tree[rt];
  85. }
  86.  
  87. pushdown(rt);
  88. int m=(l+r)>>;
  89. int ret=;
  90. if(L<=m) ret|=query(L,R,lson);
  91. if(R> m) ret|=query(L,R,rson);
  92. return ret;
  93. }
  94.  
  95. int main()
  96. {
  97. ios;
  98. int n,m,q;
  99. cin>>n>>m>>q;
  100. build(,n,);
  101. while(q--){
  102. char op[];
  103. int l,r,color;
  104. cin>>op;
  105. if(op[]=='C'){
  106. cin>>l>>r>>color;
  107. if(l>r) swap(l,r);
  108. update(l,r,color,,n,);
  109. }
  110. else{
  111. cin>>l>>r;
  112. if(l>r) swap(l,r);
  113. int cnt=query(l,r,,n,);
  114. int ans=;
  115. while(cnt){
  116. if(cnt&) ans++;
  117. cnt>>=;
  118. }
  119. cout<<ans<<endl;
  120. }
  121. }
  122. return ;
  123. }

。。。

  1.  

POJ 2777.Count Color-线段树(区间染色+区间查询颜色数量二进制状态压缩)-若干年之前的一道题目。。。的更多相关文章

  1. poj 2777 Count Color(线段树区区+染色问题)

    题目链接:  poj 2777 Count Color 题目大意:  给出一块长度为n的板,区间范围[1,n],和m种染料 k次操作,C  a  b  c 把区间[a,b]涂为c色,P  a  b 查 ...

  2. poj 2777 Count Color(线段树)

    题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  3. poj 2777 Count Color(线段树、状态压缩、位运算)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38921   Accepted: 11696 Des ...

  4. poj 2777 Count Color - 线段树 - 位运算优化

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42472   Accepted: 12850 Description Cho ...

  5. POJ 2777 Count Color (线段树成段更新+二进制思维)

    题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

  6. POJ 2777 Count Color(线段树之成段更新)

    Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...

  7. POJ P2777 Count Color——线段树状态压缩

    Description Chosen Problem Solving and Program design as an optional course, you are required to sol ...

  8. POJ 2777 Count Color(段树)

    职务地址:id=2777">POJ 2777 我去.. 延迟标记写错了.标记到了叶子节点上.. . . 这根本就没延迟嘛.. .怪不得一直TLE... 这题就是利用二进制来标记颜色的种 ...

  9. POJ2777 Count Color 线段树区间更新

    题目描写叙述: 长度为L个单位的画板,有T种不同的颜料.现要求按序做O个操作,操作分两种: 1."C A B C",即将A到B之间的区域涂上颜色C 2."P A B&qu ...

随机推荐

  1. oracle to_char格式数值

    C:\Users\XXX>sqlplus / as sysdba SQL :: Copyright (c) , , Oracle. All Rights Reserved. 连接到: Oracl ...

  2. java正则 以什么开始,以什么结束

    public class RegTest { public static void main(String[] args){ String regex = "\\[([\\s\\S]*?)\ ...

  3. BAT script to set local account password never expire

    BAT script wmic UserAccount where Name="account123" set PasswordExpires=False

  4. 南阳ACM 题目517:最小公倍数 Java版

    最小公倍数 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 为什么1小时有60分钟,而不是100分钟呢?这是历史上的习惯导致. 但也并非纯粹的偶然:60是个优秀的数字,它 ...

  5. CentOS7安装Memcached 三步曲

    1.yum 安装 yum clean allyum -y updateyum -y install memcached 2.Memcached 运行 memcached -h //查看考号修改配置vi ...

  6. Bzoj2832 / Bzoj3874 宅男小C

    Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 124  Solved: 26 Description 众所周知,小C是个宅男,所以他的每天的食物要靠外 ...

  7. Vue前端开发规范(山东数漫江湖)

    一.强制 1. 组件名为多个单词 组件名应该始终是多个单词的,根组件 App 除外. 正例: export default { name: 'TodoItem', // ... } 反例: expor ...

  8. Fire! (双bfs+预处理)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  9. 复现VGG19训练自定义图像分类

    1.复现VGG训练自定义图像分类,成功了哈哈. 需要代码工程可联系博主qq号,在左边连接可找到. 核心代码: # coding:utf-8 import tensorflow as tf import ...

  10. 伪病毒 Rp_test

    第一个写的对电脑有破坏性的程序= =,然后发现写system的copy的时候不会用字符串替代路径,然后就萎了= =,只能写一个没有自身复制的伪病毒了,坑到了好多同学的电脑,23333.... //By ...