POJ 2777 Count Color(线段树染色,二进制优化)
Count Color
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 42940 | Accepted: 13011 |
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.
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
2 2 4 C 1 1 2 P 1 2 C 2 2 2 P 1 2
Sample Output
2 1
Source
题目链接:POJ 2777
大致题意就是区间染色覆盖,区间查询出现的颜色种类。跟以往的普通线段树倒是有点不一样,就是如何进行颜色的记录问题,看了一下别人的做法,就是用2的幂数来代表颜色,由于题目中颜色小于等于30种,小于log2(INT_MAX),可以存的下,那怎么表示呢?看下面的表格
1 | 1 |
2 | 10 |
3 | 100 |
4 | 1000 |
5 | 10000 |
6 | 100000 |
.. | ..... |
.. | ..... |
.. | ..... |
30 | 100000..(29个0) |
由以上表格可以看出,某种颜色用2的幂来表示相当于二进制的1和(颜色标号-1)个0,比如6用2的幂表示就是1和5个0。
那为什么要用这个表示呢?因为后面还要用到按位或“|”运算,即重复的二进制中的1也只算一个,只要出现过,就能算计去,出现两次,也只会按位或得到一个1,比如一棵树有5(表示为10000)和2(表示为10),按位或后就得到10010,其中的1的个数就是颜色个数,不信再试试,再和颜色2按位或(表示为10),还是10010,这显然是符合同色只算一次的情况的,然后就稍微改写一下向上更新、向下更新函数,就差不多了,计算出最后的query结果中1的个数也就是答案了。用了bitset的简化求二进制1个数的写法挺方便
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | #include<iostream> #include<algorithm> #include<cstdlib> #include<sstream> #include<cstring> #include<cstdio> #include<bitset> #include<string> #include<deque> #include<stack> #include<cmath> #include<queue> #include<set> #include<map> using namespace std; #define INF 0x3f3f3f3f #define CLR(x,y) memset(x,y,sizeof(x)) #define LC(x) (x<<1) #define RC(x) ((x<<1)+1) #define MID(x,y) ((x+y)>>1) typedef pair< int , int > pii; typedef long long LL; const double PI= acos (-1.0); const int N=100010; struct seg { int l,mid,r; int color; int add; }T[N<<2]; void pushup( int k) { T[k].color=T[LC(k)].color|T[RC(k)].color; } void pushdown( int k) { if (T[k].add) { T[LC(k)].color=T[k].add; T[RC(k)].color=T[k].add; T[LC(k)].add=T[k].add; T[RC(k)].add=T[k].add; T[k].add=0; } } void build( int k, int l, int r) { T[k].l=l; T[k].r=r; T[k].mid=MID(l,r); T[k].add=0; T[k].color=0; if (l==r) { T[k].color=1; return ; } build(LC(k),l,T[k].mid); build(RC(k),T[k].mid+1,r); pushup(k); } void update( int k, int l, int r, int color) { if (r<T[k].l||l>T[k].r) return ; if (l<=T[k].l&&r>=T[k].r) { int bina_val=1<<(color-1); T[k].color=T[k].add=bina_val; return ; } pushdown(k); if (l<=T[k].mid) update(LC(k),l,r,color); if (r>T[k].mid) update(RC(k),l,r,color); pushup(k); } int query( int k, int l, int r) { if (l<=T[k].l&&T[k].r<=r) return T[k].color; else { pushdown(k); if (r<=T[k].mid) return query(LC(k),l,r); else if (l>T[k].mid) return query(RC(k),l,r); else return query(LC(k),l,T[k].mid)|query(RC(k),T[k].mid+1,r); } } int main( void ) { int L,t,O; int i,j,l,r,c; char ops[3]; while (~ scanf ( "%d%d%d" ,&L,&t,&O)) { build(1,1,L); for (i=0; i<O; ++i) { scanf ( "%s" ,ops); if (ops[0]== 'C' ) { scanf ( "%d%d%d" ,&l,&r,&c); if (l>r) swap(l,r); update(1,l,r,c); } else { scanf ( "%d%d" ,&l,&r); if (l>r) swap(l,r); printf ( "%d\n" ,bitset<32>(query(1,l,r)).count()); } } } return 0; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· Winform-耗时操作导致界面渲染滞后
· Phi小模型开发教程:C#使用本地模型Phi视觉模型分析图像,实现图片分类、搜索等功能
· 语音处理 开源项目 EchoSharp
· drools 规则引擎和 solon-flow 哪个好?solon-flow 简明教程