POJ2777-Count Color-线段树+二进制记录

题目链接:http://poj.org/problem?id=2777

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


题意就是给出O个操作,一共两种操作:修改区间[a,b]的值为c,查询区间[a,b]的数字种数。

线段树+二进制记录颜色数:本题涉及到区间修改,当然首先想到用线段树来记录区间信息,当然线段树最难的部分就是怎样维护区间信息了。对于这题,颜色种类的范围是1到30,在这种数据范围不大的情况下,可以考虑用二进制维护颜色种类(巧妙的利用或操作),具体操作请看代码。另外注意两点:输入时a有可能大于b,然后注意建树的时候叶子节点的初始化(我的数据初始化是将1<<c,所以应该初始化颜色为1<<1而不是1。。。。开始就是因为这个wa了几发)

AC code:

#include<iostream>
#include<cstdio>
#include<vector>
#include<stack>
#include<set>
#include<queue>
#include<map>
#include<string>
#include<cstring>
#include<iomanip>
#include<ctime>
#include<fstream>
#include<cstdlib>
#include<algorithm>

using namespace std;
#define eps 0x3f3f3f3f;
#define pii pair<int, int>
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define per(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,a,b) for(int i=a;i>=b;i--)
#define input(a) scanf("%d",&a)
#define input1(a,b) scanf("%d%d",&a,&b)
#define output(a) printf("%d\n",a)
#define lson le,mid,2*k
#define rson mid+1,ri,2*k+1
#define getmid int mid=(le+ri) >> 1;
#define PI 3.1415926535897932384626433832795
#define maxn 100000+10
#define ll long long
int n,t,m,a,b,c,val,ans;

struct node{
	int l,r,v,mark;
}tree[maxn<<2];

int solve(int a) //计算ans二进制表示中有几个1,即表示有几种颜色 
{
	int cnt=0;
	while(a){
		cnt++;
		a=a&(a-1);
	}
	return cnt;
}

void build(int le,int ri,int k) //建树 
{
	tree[k].l=le;
	tree[k].r=ri;
	tree[k].mark=0;
	if(le==ri){
	    tree[k].v=1<<1;   //注意初始化 
	    return;
	}
	getmid;
	build(le,mid,k<<1);
	build(mid+1,ri,k<<1|1);
	tree[k].v=tree[k<<1].v|tree[k<<1|1].v;
}

void down(int k)            //下传懒惰标记 
{
	tree[k<<1].v=tree[k].v;
	tree[k<<1|1].v=tree[k].v;
	tree[k<<1].mark=1;
	tree[k<<1|1].mark=1;
	tree[k].mark=0;
}

void update(int k)             //更新区间 
{
	if(a<=tree[k].l&&tree[k].r<=b){
		tree[k].v=val;
		tree[k].mark=1;
		return;
	}
	if(tree[k].mark) down(k);
	int mid=(tree[k].l+tree[k].r)>>1;
	if(a<=mid) update(k<<1);
	if(b>mid) update(k<<1|1);
	tree[k].v=tree[k<<1].v|tree[k<<1|1].v;
}

void query(int k)     //查询区间 
{
	if(tree[k].l>=a&&tree[k].r<=b){
		ans|=tree[k].v;    
		return;
	}
	if(tree[k].mark) down(k);
	int mid=(tree[k].l+tree[k].r)>>1;
	if(a<=mid) query(k<<1);
	if(b>mid) query(k<<1|1);
}

int main()
{
	char nouse[10];
	while(~scanf("%d%d%d",&n,&t,&m)){
	    build(1,n,1);
	    per(i,1,m){
		    scanf("%s",nouse);
		    if(nouse[0]=='C'){
			    val=1;
			    scanf("%d%d%d",&a,&b,&c);
			    if(a>b) swap(a,b);
			    val<<=c;     //数据预处理 
			    update(1);
		    }
		    else{
			    ans=0;
			    scanf("%d%d",&a,&b);
			    if(a>b) swap(a,b);
			    query(1);
			    printf("%d\n",solve(ans));
		    }
	    }
    }
}


猜你喜欢

转载自blog.csdn.net/qq_40791842/article/details/80871520