题目链接如下所示:
题目大意是一个线段,给线段染色,统计一个线段内有多少种颜色。
这里的颜色很少,甚至最大才30,可以用int的每一位代表颜色来维护线段内的颜色种类。
代码如下所示:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<vector>
#include<algorithm>
#include<cstring>
#include<set>
#include<map>
#include<queue>
#include<string>
#include<cmath>
#include<climits>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int MAXN=100001;
int color[MAXN<<2];
int change[MAXN<<2];
int kind,ans;
void push_up(int rt){
color[rt]=color[rt<<1]|color[rt<<1|1];
}
void push_down(int rt){
if (change[rt]){
change[rt<<1]=change[rt];
change[rt<<1|1]=change[rt];
color[rt<<1]=change[rt];
color[rt<<1|1]=change[rt];
change[rt]=0;
}
}
void build(int l,int r,int rt){
change[rt]=0;
if (l==r){
color[rt]=1;
return;
}
int mid=(l+r)>>1;
build(lson);
build(rson);
push_up(rt);
}
void update(int a,int b,int c,int l,int r,int rt){
if (a<=l && b>=r){
change[rt]=c;
color[rt]=c;
return;
}
push_down(rt);
int mid=(l+r)>>1;
if (a<=mid){
update(a,b,c,lson);
}
if (b>mid){
update(a,b,c,rson);
}
push_up(rt);
}
void query(int a,int b,int l,int r,int rt){
if (a<=l && b>=r){
kind|=color[rt];
return;
}
push_down(rt);
int mid=(l+r)>>1;
if (a<=mid){
query(a,b,lson);
}
if (b>mid){
query(a,b,rson);
}
// update的时候该push_up的节点都push_up过了 这里只需要update节点的颜色
// push_up(rt);
}
int main(){
int L,T,O;
int a,b,c;
char op[2];
while (~scanf("%d %d %d",&L,&T,&O)){
build(1,L,1);
while (O--){
scanf("%s",op);
if (op[0]=='C'){
scanf("%d %d %d",&a,&b,&c);
if (a>b) swap(a,b);
// cout<<op<<","<<a<<","<<b<<","<<c<<endl;
c=1<<(c-1);
update(a,b,c,1,L,1);
} else{
scanf("%d %d",&a,&b);
if (a>b) swap(a,b);
// cout<<op<<","<<a<<","<<b<<endl;
kind=0;
ans=0;
query(a,b,1,L,1);
for (int i = 0; i < T; ++i) {
if (kind & (1<<i)){
ans++;
}
}
printf("%d\n",ans);
}
}
}
return 0;
}