首页 > 代码库 > hdu 4046 Panda

hdu 4046 Panda

题意:即3个连续的wbw算是一个love,看一下某个区间共有多少个love,多次询问。还有替换某个位置的字母,然后询问。

用树状数组处理,题目并不难,但因为一处想当然错了N次。。。

题目链接:

Panda

#include"stdio.h"
#include"string.h"
#define N 50005
#define lowbit(i) (i&(-i))
char str[N];
int c[N],n;
int judge(char c1,char c2,char c3)
{
	if(c1=='w'&&c2=='b'&&c3=='w')
		return 1;
	return 0;
}
void modify(int x,int d)
{
	int i;
	for(i=x;i<n;i+=lowbit(i))
		c[i]+=d;
}
int getsum(int x)
{
	int i,s=0;
	for(i=x;i>0;i-=lowbit(i))
		s+=c[i];
	return s;
}

void inti()
{
	int i;
	for(i=2;i<n;i++)
	{
		if(judge(str[i-2],str[i-1],str[i]))
			modify(i,1);
	}
}
int main()
{
	int T,m,op,l,r,k,cnt=1;
	char ch;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		scanf("%s",str);
		memset(c,0,sizeof(c));
		inti();
		printf("Case %d:\n",cnt++);
		while(m--)
		{
			scanf("%d",&op);
			if(op==0)
			{
				scanf("%d%d",&l,&r);
				if(r-l<2)
				{
					printf("0\n");
					continue;
				}
				printf("%d\n",getsum(r)-getsum(l+1));
			}
			else
			{
				scanf("%d %c",&k,&ch);
				if(str[k]==ch)
					continue;
				if(k>=2&&judge(str[k-2],str[k-1],str[k]))
					modify(k,-1);
				if(k>=2&&judge(str[k-2],str[k-1],ch))
					modify(k,1);
				if(k>=1&&k+1<n&&judge(str[k-1],str[k],str[k+1]))
					modify(k+1,-1);
				if(k>=1&&k+1<n&&judge(str[k-1],ch,str[k+1]))
					modify(k+1,1);
				if(k+2<n&&judge(str[k],str[k+1],str[k+2]))
					modify(k+2,-1);
				if(k+2<n&&judge(ch,str[k+1],str[k+2]))
					modify(k+2,1);
				str[k]=ch;
			}
		}
	}
	return 0;
}