POJ 2777 Count Color(位运算+线段树+lazy+区间更新)
来源:互联网 发布:网络电视华人台 编辑:程序博客网 时间:2024/06/06 18:03
Count Color
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 39905 Accepted: 12034
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 4C 1 1 2P 1 2C 2 2 2P 1 2
Sample Output
21写完之后就是wrong,看别人说要用位运算,第一次用位运算ac代码:#include<stdio.h>struct s{int color;int left;int right;int lazy;}tree[4000000+1000];void pushdown(int i){if(tree[i].lazy){tree[i*2].lazy=tree[i*2+1].lazy=1;tree[i*2].color=tree[i].color;tree[i*2+1].color=tree[i].color;tree[i].lazy=0;}}void build(int l,int r,int i){int mid;tree[i].left=l;tree[i].right=r;tree[i].lazy=1;tree[i].color=1;if(l==r)return;mid=(l+r)/2;build(l,mid,i*2);build(mid+1,r,i*2+1);}void update(int l,int r,int c,int i){int mid;if(tree[i].left>=l&&tree[i].right<=r){tree[i].lazy=1;tree[i].color=(1<<(c-1));return;}pushdown(i);mid=(tree[i].left+tree[i].right)/2;if(r<=mid)update(l,r,c,i*2);else if(l>mid)update(l,r,c,i*2+1);else{update(l,mid,c,i*2);update(mid+1,r,c,i*2+1);}tree[i].color=tree[i*2].color|tree[i*2+1].color;}int query(int l,int r,int i){int mid;if(l<=tree[i].left&&r>=tree[i].right)return tree[i].color;pushdown(i);mid=(tree[i].left+tree[i].right)/2;if(r<=mid)return query(l,r,i*2);else if(l>mid)return query(l,r,i*2+1);elsereturn query(l,mid,i*2)|query(mid+1,r,i*2+1);}int fun(int x){int num=0;while(x){if(x%2)num++;x/=2;}return num;}int main(){int i,n,l,t;int a,b,c,ans;char ch[3];while(scanf("%d%d%d",&l,&t,&n)!=EOF){build(1,l,1);for(i=0;i<n;i++){scanf("%s",ch);if(ch[0]=='C'){scanf("%d%d%d",&a,&b,&c);if(b<a)update(b,a,c,1);elseupdate(a,b,c,1);}else if(ch[0]=='P'){scanf("%d%d",&a,&b);if(b<a)ans=query(b,a,1);elseans=query(a,b,1);printf("%d\n",fun(ans));}} }return 0;}
0 0
- POJ 2777 Count Color(位运算+线段树+lazy+区间更新)
- POJ 2777 Count Color 线段树区间更新位运算
- Count Color+POJ+线段树区间更新+位运算(lazy标志的运用)
- POJ 2777 Count Color 【线段树 区间更新 按位或运算】
- POJ 2777Count Color (线段树区间更新 + 位运算压缩)
- poj 2777 Count Color(线段树区间更新+位运算)
- poj 2777 Count Color(位运算+线段树区间更新 可用bitset记录)
- poj 2777 Count Color(线段树 Lazy-Tag思想 成段更新+区间统计)
- POJ 2777 Count Color (线段树的区间更新+lazy tag)
- poj 2777 Count Color(线段树+lazy优化+位运算)
- 线段树区间更新,区间统计 poj 2777 Count Color
- POJ 2777 Count Color(线段树区间修改+位运算)
- POJ 2777 Count Color (线段树区间更新 位压缩)
- POJ 2777 Count Color (线段树区间更新)
- poj 2777 Count Color (线段树区间更新)
- [POJ 2777]Count Color[线段树区间更新查询]
- poj 2777 Count Color(线段树 区间更新)
- poj 2777 Count Color (线段树 区间更新 染色)
- 虚拟机使用同一镜像做NLB测试失败的问题
- ural 1297
- Thinking in Java---线程通信+三种方式实现生产者消费者问题
- ecplise中tomcat插件
- 利用javadoc生成API文档
- POJ 2777 Count Color(位运算+线段树+lazy+区间更新)
- 网站前端技术学习 笔记
- allegro不规则焊盘制作
- 链栈的学习(回文数)
- 小马哥----高仿小米4 主板型号M168 拆机刷机机型图与开机界面识别图 多图展示
- 高质量编程的金玉良言——开放-封闭原则
- java内存回收机制
- Android handler最简单的用法
- Linux学习记录(7)Linux文件系统的挂载