poj2777--Count Color(线段树,二进制转化)
Count Color
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 34950 | Accepted: 10542 |
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
给出n的长度的木棒,初始的颜色都为1,给出num中颜色,给出m个操作, C l r x 将l到r内的全部颜色更改为x , P l r 问在l到r内有多少种颜色用线段树存储下当前的每一段的颜色,更改用数组lazy标记,将颜色转化为二进制数,统计一段颜色时,对每一段能够对它的左右子树取 | 这样就能够统计这一段中的颜色出现的种类。
#include <cstdio> #include <cstring> #include <cmath> #include <queue> #include <vector> #include <algorithm> using namespace std; #define maxn 110000 #define lmin 1 #define rmax n #define lson l,(l+r)/2,rt<<1 #define rson (l+r)/2+1,r,rt<<1|1 #define root lmin,rmax,1 #define now l,r,rt #define int_now int l,int r,int rt #define INF 0x3f3f3f3f #define eqs 1e-6 #define LL __int64 #define mod 10007 #define zero(x) ( fabs(x) < eqs ? 0 : x ) #define mem(a,b) (memset(a),b,sizeof(a)) int cl[maxn<<2] ; int lazy[maxn<<2] ; void push_up(int_now) { cl[rt] = cl[rt<<1] | cl[rt<<1|1] ; } void push_down(int_now) { if( lazy[rt] ) { lazy[rt<<1] = lazy[rt<<1|1] = lazy[rt] ; cl[rt<<1] = cl[rt<<1|1] = lazy[rt] ; lazy[rt] = 0 ; } } void creat(int_now) { cl[rt] = lazy[rt] = 0 ; if( l != r ) { creat(lson); creat(rson); push_up(now); } else cl[rt] = 1 ; } void update(int ll,int rr,int x,int_now) { if( ll > r || rr < l ) return ; if( ll <= l && r <= rr ) { cl[rt] = lazy[rt] = 1<<(x-1); return ; } push_down(now); update(ll,rr,x,lson); update(ll,rr,x,rson); push_up(now); } int query(int ll,int rr,int_now) { if(ll > r || rr < l) return 0; if(ll <= l && r <= rr) return cl[rt] ; push_down(now); return query(ll,rr,lson) | query(ll,rr,rson); } int ans(int x) { int aa = 0 ; while(x) { if(x & 1) aa++ ; x >>= 1 ; } return aa ; } int main() { int n , ls , m ; int l , r , x ; char str[10] ; while(~scanf("%d %d %d", &n, &ls, &m)) { creat(root); while(m--) { scanf("%s", str); if(str[0] == 'C') { scanf("%d %d %d", &l, &r, &x); if(l > r) swap(l,r); update(l,r,x,root); } else { scanf("%d %d", &l, &r); if(l > r) swap(l,r); printf("%d\n",ans( query(l,r,root) ) ); } } } return 0; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥