poj 2777 Count Color(线段树区间更新+位运算)
来源:互联网 发布:js url encode php 编辑:程序博客网 时间:2024/06/18 12:24
问题描述
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.
输入
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.
输出
Ouput results of the output operation in order, each line contains a number.
样例输入
2 2 4C 1 1 2P 1 2C 2 2 2P 1 2
样例输出
21
因为颜色的个数不是很大,所以可以用位运算来存储状态。
#include <iostream>#include <stdio.h>#include <algorithm>#include <cstring>using namespace std;const int maxx=100005;int ans;struct node{ int l,r,c; int f;}no[maxx<<2];void pushdown(int rt){ no[rt<<1].f=no[rt].f; no[rt<<1|1].f=no[rt].f; no[rt<<1|1].c=no[rt].c; no[rt<<1].c=no[rt].c; no[rt].f=0;}void build(int l,int r,int rt){ int mid=(l+r)>>1; no[rt].l=l; no[rt].r=r; no[rt].c=1; no[rt].f=0; if(l==r) { return ; } build(l,mid,rt<<1); build(mid+1,r,rt<<1|1);}void updata(int l,int r,int k,int rt){ int mid=(no[rt].l+no[rt].r)>>1; if(no[rt].l==l&&no[rt].r==r) { no[rt].f=1; no[rt].c=1<<(k-1); return ; } if(no[rt].f>0)pushdown(rt); if(mid>=r) updata(l,r,k,rt<<1); else if(mid<l) updata(l,r,k,rt<<1|1); else { updata(l,mid,k,rt<<1); updata(mid+1,r,k,rt<<1|1); } no[rt].c=no[rt<<1].c|no[rt<<1|1].c;}void query1(int l,int r,int rt){ if(no[rt].f>0||(no[rt].l==l&&no[rt].r==r)) { //cout<<l<<" "<<r<<" "<<no[rt<<1].c<<" "<<no[rt<<1|1].c<<endl; ans|=no[rt].c; return ; }//if(no[rt].f>0)pushdown(rt); int mid=(no[rt].l+no[rt].r)>>1; if(mid>=r) query1(l,r,rt<<1); else if(mid<l) query1(l,r,rt<<1|1); else { query1(l,mid,rt<<1); query1(mid+1,r,rt<<1|1); }}int main(){ int n,t,o; scanf("%d %d %d",&n,&t,&o); build(1,n,1); for(int i=1;i<=o;i++) { char a;int l,r,k; cin>>a; scanf("%d %d",&l,&r); if(a=='C') { scanf("%d",&k); if(l>r) { int te=l; l=r; r=te; } updata(l,r,k,1); } if(a=='P') { if(l>r) { int te=l; l=r; r=te; } ans=0; query1(l,r,1); int ans1=0; for(int i=1;i<=30;i++) { if(1<<(i-1)&ans) ans1++; } printf("%d\n",ans1); } }}
0 0
- POJ 2777 Count Color 线段树区间更新位运算
- poj 2777 Count Color(线段树区间更新+位运算)
- POJ 2777 Count Color(位运算+线段树+lazy+区间更新)
- POJ 2777 Count Color 【线段树 区间更新 按位或运算】
- POJ 2777Count Color (线段树区间更新 + 位运算压缩)
- poj 2777 Count Color(位运算+线段树区间更新 可用bitset记录)
- Count Color+POJ+线段树区间更新+位运算(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 (线段树+位运算)
- POJ 2777 Count Color(线段树+位运算)
- POJ 2777 Count Color(线段树+位运算)
- 线段树区间更新,区间统计 poj 2777 Count Color
- Android群英传笔记——第十章:Android性能优化
- hdu 4521 小明系列问题——小明序列(求间距大于d的最长上升序列)
- 离散数学之集合上关系的判断
- [离散化]图形面积
- Boost Test学习总结(C++)
- poj 2777 Count Color(线段树区间更新+位运算)
- mesos上运行docker registry
- Codeforces Round #349 (Div. 2) - C
- 发表在IBM Developworks上的文章,Spark Streaming 图片处理案例介绍
- [乱搞 暴力 并查集] BZOJ 3563 DZY Loves Chinese
- maven在不同环境下打包的应用
- Java 类加载机制详解
- [三进制倍增 || 可并堆] BZOJ 4003 [JLOI2015]城池攻占
- Java的对象是采用值传递还是引用传递?