题目:
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
题目大意:在一个长为L的木板上,进行染色,颜色的种类不超过T种,O表示操作几次。
起初木板上的颜色都是1,C i j k,代表将i到j之间的木板染为k, P i j,代表计算从i到j这之间有几种颜色,并输出。
思路:
线段树区间更新,维护一个区间的颜色种类数就行了,由于只有30种颜色,可以用一个数组来标记有几种颜色。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#define ll long long
using namespace std;
int L,t,o;
const int INF=1e5+5;
struct node{
int right;
int left;
int lazy;//懒惰标记
int mp;//标记颜色的类型
}f[4*INF];
int vis[50];//用来标记颜色的种类
void built(int ans,int l,int r)//建树
{
f[ans].right =r;f[ans].left =l;
f[ans].mp =1;f[ans].lazy =0;
if(l==r)return;
int mid=(l+r)/2;
built(ans<<1,l,mid);
built(ans<<1|1,mid+1,r);
}
void pushdown(int k)
{
if(f[k].lazy ==0)return;
int x=f[k].lazy ;
f[k<<1].mp =x;f[k<<1|1].mp=x;
f[k<<1].lazy =x;f[k<<1|1].lazy =x;
f[k].lazy =0;
}
void update(int ans,int l,int r,int x)
{
int j,k;
if(f[ans].left ==l&&f[ans].right ==r)
{
f[ans].mp =x;
f[ans].lazy =x;
return;
}
if(f[ans].mp ==x)return;//区间已经是那个颜色了,无需再染
pushdown(ans);
int mid=(f[ans].left +f[ans].right )/2;
if(r<=mid){
update(ans<<1,l,r,x);
}
else if(l>mid)
{
update(ans<<1|1,l,r,x);
}
else {
update(ans<<1|1,mid+1,r,x);
update(ans<<1,l,mid,x);
}
j=f[ans<<1].mp ;k=f[ans<<1|1].mp ;
if(j!=k||j==-1||k==-1)f[ans].mp=-1;//为-1,则说明,区间内不只一种颜色
else f[ans].mp =j;
}
void getsum(int ans,int l,int r)
{
pushdown(ans);
int i,j;
if(f[ans].mp !=-1)
{
vis[f[ans].mp ]=1;// 有这种颜色则标记为1
return ;
}
int mid=(f[ans].left +f[ans].right )/2;
if(r<=mid)getsum(ans<<1,l,r);
else if(l>mid)getsum(ans<<1|1,l,r);
else {
getsum(ans<<1,l,mid);
getsum(ans<<1|1,mid+1,r);
}
}
int main()
{
while(scanf("%d%d%d",&L,&t,&o)!=EOF)
{
char s;
int i,j,k,val,n;
built(1,1,L);
for(i=1;i<=o;i++)
{
getchar();
scanf("%c",&s);
if(s=='C')
{
scanf("%d %d %d",&j,&k,&val);
if(j>k)swap(j,k);//注意
update(1,j,k,val);
}
else if(s=='P')
{
scanf("%d %d",&j,&k);
if(j>k)swap(j,k);
memset(vis,0,sizeof(vis));//初始化为0
getsum(1,j,k);
int ans=0;
for(j=1;j<=t;j++)//统计种类
{
if(vis[j]==1)ans++;
}
printf("%d\n",ans);
}
}
}
return 0;
}