POJ-2777 Count Color

经典区间染色板子题

POJ-2777 Count Color_第1张图片

#include
#include
#include
using namespace std;
const int N = 1e6+10;
struct Segment{
	int l,r,id;
}tr[N<<2];
int n,color,m;

void pushdown(int u){
	if(tr[u].id){tr[u<<1].id = tr[u<<1|1].id = tr[u].id;tr[u].id = 0;}
}

void build(int u,int l,int r){
	tr[u] = {l,r,1};
	if(l==r)return;
	int mid = l+r>>1;
	build(u<<1,l,mid),build(u<<1|1,mid+1,r);
}


void modify(int u,int l,int r,int c){
	if(tr[u].l>=l&&tr[u].r<=r){
		tr[u].id = c;return;
	}
	pushdown(u);
	int mid = tr[u].l+tr[u].r>>1;
	if(l<=mid)modify(u<<1,l,r,c);
	if(r>mid)modify(u<<1|1,l,r,c);
}

int query(int u,int l,int r){
	if(tr[u].id)return tr[u].id;
	
	int res = 0;
	int mid = tr[u].l+tr[u].r>>1;
	if(l<=mid)res|=query(u<<1,l,r);
	if(r>mid)res|=query(u<<1|1,l,r);
	return res;
}
int lowbit(int x){return x&-x;}

int cal(int x){
	int res = 0;
	for(int i=x;i;i-=lowbit(i))res++;
	return res;
}


int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>color>>m;
	build(1,1,n);
	while(m--){
		char op;int l,r,c;
		cin>>op>>l>>r;
		if(l>r)swap(l,r);
		if(op=='C'){cin>>c;modify(1,l,r,1<<(c-1));}
		else cout<

你可能感兴趣的:(线段树,数据结构,c++,算法)