poj 2777 Count Color(位运算+线段树区间更新 可用bitset记录)

Count Color
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 43754 Accepted: 13239

Description

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

Source



题意:

有一块长为l的板子,初始时涂上颜色1,现在有两种操作:

C l r c:将区间[l,r]涂上颜色c

P l r:将区间[l,r]颜色种类输出。

总的颜色种类数<=30。


题解:

看了题解才恍然大悟,由于颜色数目很少,可以在线段树结点上用二进制的一位记录该区间是否有该颜色(也可以用bitset记录),那么区间合并时只需要|一下就行了。


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
const int MAXN=100000+100;
struct node
{
	int l,r,tag,lazy,sum;
}a[MAXN*4];
void build(int i,int l,int r)
{
	a[i].l=l,a[i].r=r,a[i].sum=2,a[i].tag=0;
	if(l==r) return;
	int m=(l+r)/2;
	build(i*2,l,m);
	build(i*2+1,m+1,r);
}
void pushdown(int i)
{
	//a[i].tag=a[i].sum=0;
	if(a[i].l!=a[i].r)
	{
		a[i*2].tag=a[i*2+1].tag=a[i].tag;
		a[i*2].sum=a[i*2+1].sum=(1<<a[i].tag);
		a[i].tag=0;
	}
	return;
}
void update(int i,int l,int r,int c)
{
	if(a[i].l==l&&a[i].r==r)
	{
		a[i].tag=c;
		a[i].sum=(1<<c);
		return;
	}
	if(a[i].tag)
	pushdown(i);
	int m=(a[i].l+a[i].r)/2;
	if(r<=m) update(i*2,l,r,c);
	else if(l>m) update(i*2+1,l,r,c);
	else 
	{
		update(i*2,l,m,c);
		update(i*2+1,m+1,r,c);
	}
	a[i].sum=a[i*2].sum|a[i*2+1].sum;
	return;
}
int query(int i,int l,int r)
{
	if(a[i].l==l&&a[i].r==r)
	{
		return a[i].sum;
	}
	if(a[i].tag) pushdown(i);
	int m=(a[i].l+a[i].r)/2;
	if(r<=m) return query(i*2,l,r);
	else if(l>m) return query(i*2+1,l,r);
	else return query(i*2,l,m)|query(i*2+1,m+1,r);
}
int get(int x)
{
	int ans=0;
	while(x)
	{
		if(x&1) ans++;
		x/=2;
	}
	return ans;
}
int main()
{
	int l1,t,q;
	scanf("%d%d%d",&l1,&t,&q);
	build(1,1,l1);
	while(q--)
	{
		char s[2];
		scanf("%s",s);
		int l,r,c;
		if(s[0]=='C') 
		{
			scanf("%d%d%d",&l,&r,&c);
			if(l>r) swap(l,r);
			update(1,l,r,c);
		}
		else 
		{
			scanf("%d%d",&l,&r);
			if(l>r) swap(l,r);
			int ans=get(query(1,l,r));
			printf("%d\n",ans);
		}
	}
	return 0;
}

bitset记录:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<bitset>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
const int MAXN=100000+100;
typedef bitset<31> P;
struct node
{
	int l,r,tag,lazy;
	P sum;
}a[MAXN*4];
void build(int i,int l,int r)
{
	a[i].l=l,a[i].r=r,a[i].sum=2,a[i].tag=0;
	if(l==r) return;
	int m=(l+r)/2;
	build(i*2,l,m);
	build(i*2+1,m+1,r);
}
void pushdown(int i)
{
	if(a[i].l!=a[i].r)
	{
		a[i*2].tag=a[i*2+1].tag=a[i].tag;
		a[i*2].sum=(1<<a[i].tag);
		a[i*2+1].sum=(1<<a[i].tag);
		a[i].tag=0;
	}
	return;
}
void update(int i,int l,int r,int c)
{
	if(a[i].l==l&&a[i].r==r)
	{
		a[i].tag=c;
		a[i].sum=(1<<c);
		return;
	}
	if(a[i].tag)
	pushdown(i);
	int m=(a[i].l+a[i].r)/2;
	if(r<=m) update(i*2,l,r,c);
	else if(l>m) update(i*2+1,l,r,c);
	else 
	{
		update(i*2,l,m,c);
		update(i*2+1,m+1,r,c);
	}
	a[i].sum=a[i*2].sum|a[i*2+1].sum;
	return;
}
P query(int i,int l,int r)
{
	if(a[i].l==l&&a[i].r==r)
	{
		return a[i].sum;
	}
	if(a[i].tag) pushdown(i);
	int m=(a[i].l+a[i].r)/2;
	if(r<=m) return query(i*2,l,r);
	else if(l>m) return query(i*2+1,l,r);
	else return query(i*2,l,m)|query(i*2+1,m+1,r);
}
int get(int x)
{
	int ans=0;
	while(x)
	{
		if(x&1) ans++;
		x/=2;
	}
	return ans;
}
int main()
{
	int l1,t,q;
	scanf("%d%d%d",&l1,&t,&q);
	build(1,1,l1);
	while(q--)
	{
		char s[2];
		scanf("%s",s);
		int l,r,c;
		if(s[0]=='C') 
		{
			scanf("%d%d%d",&l,&r,&c);
			if(l>r) swap(l,r);
			update(1,l,r,c);
		}
		else 
		{
			scanf("%d%d",&l,&r);
			if(l>r) swap(l,r);
			P ans=query(1,l,r);
			printf("%d\n",ans.count());
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值