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
题意:区间1~L,Q次操作,C A B C 代表把区间A~B的颜色改为C,P A B表示查询A~B有几种不同的颜色
一开始1~L区间都是1这个颜色,并且A和B大小不确定
思路:颜色总共有不超过30种,可以考虑用二进制的每一位表示一个颜色,比如:1 代表颜色1 ,10代表颜色2,100代表颜色3....,父节点的tree[node]值就等于左右子节点做或|操作
tree[node]存储的是二进制11011.....
lazy[node]存储的是更新的颜色C
import java.util.Scanner;
public class Main {
static final int max=100005;
static int L,T,Q;
static int tree[]=new int[max<<2];
static int lazy[]=new int[max<<2];
public static void pushup(int node){
tree[node]=tree[node<<1]|tree[node<<1|1];//通过或操作可以求得tree父节点的值,然后通过求1有几个就求出了不同颜色的个数
}
public static void pushdown(int node){
if(lazy[node]!=-1){
lazy[node<<1]=lazy[node<<1|1]=lazy[node];
tree[node<<1]=tree[node<<1|1]=1<<(lazy[node]-1);
lazy[node]=-1;
}
}
public static void update(int node,int l,int r,int a,int b,int c){
if(l>=a&&r<=b){
lazy[node]=c;
tree[node]=1<<(c-1);//颜色为1,就是1;颜色是2,就是1<<1=10;颜色是3,就是1<<2=100。。。
return;
}
pushdown(node);
int mid=(l+r)/2;
if(b<=mid) update(node<<1,l,mid,a,b,c);
else if(a>mid) update(node<<1|1,mid+1,r,a,b,c);
else{
update(node<<1,l,mid,a,b,c);
update(node<<1|1,mid+1,r,a,b,c);
}
pushup(node);
}
public static int query(int node,int l,int r,int a,int b){
if(l>=a&&r<=b){
return tree[node];
}
pushdown(node);
int mid=(l+r)/2;
if(b<=mid) return query(node<<1,l,mid,a,b);
else if(a>mid) return query(node<<1|1,mid+1,r,a,b);
else return query(node<<1,l,mid,a,b)|query(node<<1|1,mid+1,r,a,b);
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
L=scan.nextInt();
T=scan.nextInt();
Q=scan.nextInt();
lazy[1]=1;//一开始所有tree节点都是1,lazy【1】=1其实就代表对所有tree节点赋值为1,原因就是pushdown操作需要就向下更新
while(Q!=0){
String s=scan.next();
if(s.charAt(0)=='C'){
int a=scan.nextInt();
int b=scan.nextInt();
int c=scan.nextInt();
if(a>b){
int t=a;
a=b;
b=t;
}
update(1,1,L,a,b,c);
}
else{
int a=scan.nextInt();
int b=scan.nextInt();
if(a>b){
int t=a;
a=b;
b=t;
}
int ans=query(1,1,L,a,b);
int cnt=0;
while(ans>0){
if((ans&1)==1) cnt++;
ans>>=1;
}
System.out.println(cnt);
}
Q--;
}
}
}