poj2777 Count Color

不开longlong见祖宗

题目没说操作中的a,b大小要注意

接着就是状压+线段树

http://poj.org/problem?id=2777

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
const int M=1e5+5;
ll tree[M<<2],lazy[M<<2];
int n;
void up(int root){
	tree[root]=tree[root<<1]|tree[root<<1|1];
}
void pushdown(int root){
	ll sign=lazy[root];
	lazy[root<<1]=lazy[root<<1|1]=sign;
	tree[root<<1]=tree[root<<1|1]=sign;
	lazy[root]=0ll;
}
void build(int root,int l,int r){
	if(l==r){
		tree[root]=1ll;
		return ;
	}
	int midd=(l+r)>>1;
	build(root<<1,l,midd);
	build(root<<1|1,midd+1,r);
	up(root);
}
void update(int L,int R,int x,int root,int l,int r){
	if(L<=l&&r<=R){
		tree[root]=(1<<(x-1))*1ll;
		lazy[root]=(1<<(x-1))*1ll;
		return;
	}
	if(lazy[root])
		pushdown(root);
	int midd=(l+r)>>1;
	if(L<=midd)
		update(L,R,x,root<<1,l,midd);
	if(R>midd)
		update(L,R,x,root<<1|1,midd+1,r);
	up(root);
}
ll query(int L,int R,int root,int l,int r){
	if(L<=l&&r<=R){
		return tree[root];
	}
	if(lazy[root])
		pushdown(root);
	ll ans=0;
	int midd=(l+r)>>1;
	if(L<=midd)
		ans|=query(L,R,root<<1,l,midd);
	if(R>midd)
		ans|=query(L,R,root<<1|1,midd+1,r);
	up(root);
	return ans;
}
int solve(int l,int r){
	ll x=query(l,r,1,1,n);
	int t=0;
	while(x){
		if(x&1)
			t++;
		x>>=1;
	}
	return t;
}
char s[2];
int main(){
	n;
	int m,p;
	scanf("%d%d%d",&n,&m,&p);
	build(1,1,n);
	
	while(p--){
		scanf("%s",s);
		if(s[0]=='C'){
			int x,y,w;
			if(x>y)
				swap(x,y);
			scanf("%d%d%d",&x,&y,&w);
			update(x,y,w,1,1,n);
		}
		else{
			int x,y;
			scanf("%d%d",&x,&y);
			if(x>y)
				swap(x,y);
			printf("%d
",solve(x,y));
		}
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/starve/p/10821278.html