Count Color
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. 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 |
题意:
简单英文,很容易直到意思,就是涂颜色嘛。
解题思路:
一看就是裸的线段树,T给的30很明显就是要你用数位来计数,想到这就很好做了。直接板子一套。
#include<iostream>
#include<cstdio>
#include<map>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int maxn = 100005 ;
int col[maxn<<2] ;
int sum[maxn<<2] ;
int L,T,O ;
int n ;
void PushUp(int rt){
sum[rt] = sum[rt<<1]|sum[rt<<1|1] ;
return ;
}
void PushDown(int rt){
if(col[rt]){
col[rt<<1] = col[rt] ;
col[rt<<1|1] = col[rt] ;
sum[rt<<1] = col[rt] ;
sum[rt<<1|1] = col[rt] ;
col[rt]=0;
}
return ;
}
void build(int l,int r,int rt){
col[rt]=0;
sum[rt]=0;
if(l==r)return ;
int m = (l+r)>>1 ;
build(lson) ;
build(rson) ;
PushUp(rt);
}
void update(int L,int R,int c,int l,int r,int rt){
if(L<=l&&r<=R){
col[rt]=c ;
sum[rt] = c ;
return ;
}
PushDown(rt);
int m = (l+r)>>1 ;
if(L<=m)update(L,R,c,lson) ;
if(m<R)update(L,R,c,rson) ;
PushUp(rt) ;
}
int query(int L,int R,int l,int r,int rt){
if(L<=l&&r<=R){
return sum[rt] ;
}
PushDown(rt) ;
int m = (l+r)>>1 ;
int ret =0;
if(L<=m)ret |= query(L,R,lson) ;
if(m<R) ret |= query(L,R,rson) ;
PushUp(rt) ;
return ret ;
}
int main(){
map<int,int>mapp;
while(~scanf("%d%d%d",&L,&T,&O)){
mapp.clear() ;
n=L ;
build(1,n,1) ;
int cnt=2 ;
update(1,n,1,1,n,1) ;
mapp[1]=1 ;
for(int i=0;i<O;i++){
char op[3];
scanf("%s",op);
if(op[0]=='C'){
int a,b,c ;
scanf("%d%d%d",&a,&b,&c);
if(a>b)swap(a,b) ;
if(mapp.count(c)==0){
mapp[c]=cnt ;
cnt<<=1 ;
//printf("cnt = %d\n",cnt);
}
update(a,b,mapp[c],1,n,1) ;
// for(int j=1;j<=4;j++){
// printf("%d ",sum[j]);
// }printf("\n");
}else{
int a,b ;
scanf("%d%d",&a,&b);
if(a>b)swap(a,b) ;
int x = query(a,b,1,n,1) ;
//printf("x = %d\n",x);
int ans = 0 ;
while(x){
if(x%2==1)ans++ ;
x>>=1 ;
}
printf("%d\n",ans);
}
}
}
return 0;
}
/**
2 2 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2
*/