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
2 2 4 C 1 1 2 P 1 2 C 2 2 2 P 1 2
Sample Output
2 1
线段树模板题,用二进制位存储,AC的C++程序如下:
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
const int MAXN=1e5+10;
int sum[4*MAXN],add[4*MAXN];//sum用来保存颜色的二进制位,add表示颜色
void init()
{
memset(sum,0,sizeof(sum));
memset(add,0,sizeof(add));
}
void push_up(int rt){ //向上更新,通过当前节点rt把值递归到父节点
sum[rt]=sum[rt<<1]|sum[rt<<1|1];
}
void push_down(int rt){ //更新rt的子节点
if(add[rt]){
add[rt<<1]=add[rt];
add[rt<<1|1]=add[rt];
sum[rt<<1]=add[rt];
sum[rt<<1|1]=add[rt];
add[rt]=0;
}
}
void build(int l,int r,int rt){
if(l==r){
sum[rt]=1;
return;
}
int mid=(l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
push_up(rt);
}
void update(int a,int b,int c,int l,int r,int rt){
if(a<=l&&b>=r){
sum[rt]=(1<<(c-1));
add[rt]=(1<<(c-1));
return;
}
push_down(rt); //向下更新
int mid=(l+r)>>1;
if(a<=mid) update(a,b,c,l,mid,rt<<1);
if(b>mid) update(a,b,c,mid+1,r,rt<<1|1);
push_up(rt); //向上更新
}
int query(int a,int b,int l,int r,int rt){
if(a<=l&&b>=r) return sum[rt];
push_down(rt);
int mid=(l+r)>>1;
int ans=0,ans1=0,ans2=0;
if(a<=mid) ans1=query(a,b,l,mid,rt<<1);
if(b>mid) ans2=query(a,b,mid+1,r,rt<<1|1);
ans=ans1|ans2;
return ans;
}
int main(){
int n,t,m;
scanf("%d%d%d",&n,&t,&m);
init();
build(1,n,1);
while(m--){
char str[2];
int a,b,c,aa,bb;
scanf("%s",str);
if(str[0]=='C'){
scanf("%d%d%d",&a,&b,&c);
aa=min(a,b);
bb=max(a,b);
update(aa,bb,c,1,n,1);
}else{
scanf("%d%d",&a,&b);
aa=min(a,b);
bb=max(a,b);
int ans=query(aa,bb,1,n,1);
int ccount=0;
while(ans){
if(ans&1) ccount++;
ans=ans>>1;
}
printf("%d\n",ccount);
}
}
return 0;
}
这是一道关于线段树应用的问题,要求在一条长度为L的板子上进行颜色涂染操作。操作包括涂色和查询不同颜色的区间数量。题目给出的样例展示了一个利用二进制位存储颜色信息的C++解决方案。

1302

被折叠的 条评论
为什么被折叠?



