HDU 4046 Panda 树状数组

题意:给你一个字符串,由'w' 和 'b' 组成,对该字符串有俩个操作,

当输入为0 a, b时,询问区间[a,b] 内有多少个串 为 "wbw";

当输入为1,k,,ch时,将下标为k的字符改为字符ch;

#include <cstdio>
#include <iostream>
using namespace std;

int n, m;
int c[60000];
char str[60000];

int lowbit ( int x )  
{  
    return x & ( -x );  
}  
  
void update ( int x, int v )  
{  
    for ( int i = x; i <= n; i += lowbit(i) )  
        c[i] += v;   
} 

int getSum ( int x )  
{  
    int sum = 0;  
    for ( int i = x; i > 0; i -= lowbit(i) )  
        sum += c[i];  
    return sum;  
}  

bool check ( char x, char y, char z )
{
	if ( x == 'w' && y == 'b' && z == 'w' )
		return true;
	return false;
}

void init ()
{
	for ( int i = 3; i <= n; i++ )
		if ( check(str[i-2],str[i-1],str[i]) )
			update ( i, 1 );
}

int main()
{
	char change;
	int t, l, r, pos, oper;
	int cnt = 0;

	scanf("%d",&t);

	while ( t-- )
	{
		scanf("%d%d",&n,&m);
		scanf("%s",str+1);

		memset(c,0,sizeof(c));
		init();
		printf("Case %d:\n",++cnt);

		while ( m-- )
		{
			scanf("%d",&oper);
			if ( oper == 0 )
			{
				scanf("%d%d",&l,&r);
				l++; r++;
				if ( l > r )  swap ( l, r );
				if ( r - l < 2 )  printf("0\n");
				else printf ("%d\n", getSum(r) - getSum(l+1) ); /* 边界值一定得搞清楚, 由于是三个字符一组,sum[l+1]里面还可能记录了
str[l-1],str[l], str[l+1]构成的组合,所以要减去getSum(l+1) */
			}
			else
			{
				int i;
				scanf("%d %c",&pos, &change);
				pos++;
				if ( str[pos] == change ) continue;

				i = pos;
				if ( 3 <= i && i <= n )
				{
				    if ( check(str[i-2],str[i-1],str[i]) && !check(str[i-2],str[i-1],change) )
					    update(i,-1);
				    if ( !check(str[i-2],str[i-1],str[i]) && check(str[i-2],str[i-1],change) )
					    update(i,1);
				}

				i = pos + 1;
				if ( 3 <= i && i <= n )
				{
					if ( check(str[i-2],str[i-1],str[i]) && !check(str[i-2],change,str[i]) )
						update(i,-1);
					if ( !check(str[i-2],str[i-1],str[i]) && check(str[i-2],change,str[i]) )
						update(i,1);
				}

				i = pos + 2;
				if ( 3 <= i && i <= n )
				{
					if ( check(str[i-2],str[i-1],str[i]) && !check(change,str[i-1],str[i]) )
						update(i,-1);
					if ( !check(str[i-2],str[i-1],str[i]) && check(change,str[i-1],str[i]) )
						update(i,1);
				}
				str[pos] = change;
			}
		}
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值