大意是一塊定長的木板,在上面塗顏色,每次塗一個區間,當詢問某個區間時,要返回這個區間中的顏色數目。
這道題目其實和3468非常之像,也是要更新區間,也用到了lazy思想。可能略有不同的是,這道題目有個用到位運算的小技巧。由於總顏色數目較少(<= 30),一個區間上的顏色可以用一個32位的 int 表示,最後統計總顏色數時,只要運用或運算符(|)就可以。
#include<iostream>
#include<cstdio>
#define MID(x, y) ((x + y) >> 1)
#define R(x) (x << 1 | 1)
#define L(x) (x << 1)
using namespace std;
typedef struct{
int l, r;
long long board;
long long c;
} NODE;
NODE st[440000];
void build(int t, int l, int r){
st[t].l = l;
st[t].r = r;
st[t].c = 0;
st[t].board = 1;
// cout << "l--r: " << l <<" "<< r<< endl;
if(l == r)
return;
else{
build(L(t), l, MID(l, r));
build(R(t), MID(l, r) + 1, r);
}
}
void update(int t, int l, int r, int c){
int mid = MID(st[t].l, st[t].r);
//cout << t << l << r << c << endl;;
if(st[t].l == l && st[t].r == r){
st[t].board = 1;
st[t].board = st[t].board << c - 1;
st[t].c = c;
return;
}
if(st[t].c != 0){
st[L(t)].board = 1;
st[L(t)].board = st[L(t)].board << st[t].c - 1;
st[L(t)].c = st[t].c;
st[R(t)].board = 1;
st[R(t)].board = st[R(t)].board << st[t].c - 1;
st[R(t)].c = st[t].c;
st[t].c = 0;
}
if(r <= mid){
update(L(t), l, r, c);
}else if(l > mid){
update(R(t), l, r, c);
}else{
update(L(t), l, mid, c);
update(R(t), mid + 1, r, c);
}
st[t].board = (st[L(t)].board | st[R(t)].board);
// cout << st[t].board << endl;
}
long long query(int t, int l, int r){
int mid = MID(st[t].l, st[t].r);
if(st[t].l == l && st[t].r == r){
return st[t].board;
}
if(st[t].c != 0){
st[L(t)].board = 1;
st[L(t)].board = st[L(t)].board << st[t].c - 1;
st[L(t)].c = st[t].c;
st[R(t)].board = 1;
st[R(t)].board = st[R(t)].board << st[t].c - 1;
st[R(t)].c = st[t].c;
st[t].c = 0;
}
if(r <= mid){
return query(L(t), l, r);
}else if(l > mid){
return query(R(t), l, r);
}else{
return query(L(t), l, mid) | query(R(t), mid + 1, r);
}
}
int count(long long a){
int b = 0;
while(a != 0){
if(a % 2 == 1)
b++;
a = a >> 1;
}
return b;
}
int main(){
int L, T, O;
char cmd[10];
scanf("%d %d %d", &L, &T, &O);
build(1, 1, L);
while(O--){
scanf("%s", cmd);
if(cmd[0] == 'C'){
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
if(y < x)
swap(x, y);
update(1, x, y, z);
}else{
int x, y;
scanf("%d %d", &x, &y);
if(y < x)
swap(x, y);
printf("%d\n", count(query(1, x, y)));
}
}
}