HDU 4046 树状数组

/*******************************************************************************
   WA了N多次。。。结果是忘记初始化st数组了。。。悲剧得1B。。。
   解法就是考虑修改位置id的字符,观察左右"wbw"是否被更改,如果被更改了,那么就更新下,这个地方要
特别小心。。。具体可以见代码~
*******************************************************************************/
#include <iostream>
#include <functional>
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <utility>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
using namespace std;

#define LOWBIT(x) ( (x) & ( (x) ^ ( (x) - 1 ) ) )
#define CLR(x, k) memset((x), (k), sizeof(x))
#define CPY(t, s) memcpy((t), (s), sizeof(s))
#define SC(t, s) static_cast<t>(s)
#define LEN(s) static_cast<int>( strlen((s)) )
#define SZ(s) static_cast<int>( (s).size() )

typedef double LF;
typedef __int64 LL;		//VC
typedef unsigned __int64 ULL;

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;

typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<double> VF;
typedef vector<string> VS;

template <typename T>
T sqa(const T & x)
{
	return x * x;
}
template <typename T>
T gcd(T a, T b)
{
	if (!a || !b)
	{
		return max(a, b);
	}
	T t;
	while (t = a % b)
	{
		a = b;
		b = t;
	}
	return b;
};

const int INF_INT = 0x3f3f3f3f;
const LL INF_LL = 0x7fffffffffffffffLL;		//15f
const double oo = 10e9;
const double eps = 10e-7;
const double PI = acos(-1.0);

#define  ONLINE_JUDGE

const int MAXN = 50004 << 1;

int test, n, m;
char ch[MAXN];
int st[MAXN];

bool judge(int ind)	//判断ind是为"wbw"中的b位置
{
	if (1 <= ind && ind < n - 1)	//显然ind=0和ind=n-1的时候是不存在"wbw"的
	{
		if ('b' == ch[ind] && 'w' == ch[ind - 1] && 'w' == ch[ind + 1])
		{
			return true;
		}
	}
	return false;
}
void update(int x, int inc)
{
	for (int i = x; i < MAXN; i += LOWBIT(i))
	{
		st[i] += inc;
	}
	return ;
}
int query(int x)
{
	int res = 0;
	for (int i = x; i > 0; i -= LOWBIT(i))
	{
		res += st[i];
	}
	return res;
}
void ace()
{
	int cas = 1;
	int op, x, y;
	char cc[4];
	for (scanf("%d", &test); test--; ++cas)
	{
		scanf("%d %d%*c%s", &n, &m, ch);
		//printf("ch: %s\n", ch);
		CLR(st, 0);
		for (int i = 1; i < n - 1; ++i)	//超级杯具的初始化。。。
		{
			if (judge(i))
			{
				update(i, 1);
			}
		}
		printf("Case %d:\n", cas);
		while (m--)
		{
			scanf("%d %d", &op, &x);
			if (0 == op)
			{
				scanf("%d", &y);
				if (x > y)
				{
					swap(x, y);
				}
				if (y - x + 1 < 3)
				{
					puts("0");
					continue ;
				}
				printf("%d\n", query(y - 1) - query(x));
			}
			else
			{
				scanf("%s", cc);
				if (cc[0] == ch[x])
				{
					continue ;
				}
				if ('w' == cc[0])	//以下全是坑
				{
					if (judge(x))
					{
						update(x, -1);
					}
					ch[x] = 'w';
					if (judge(x - 1))
					{
						update(x - 1, 1);
					}
					if (judge(x + 1))
					{
						update(x + 1, 1);
					}
				}
				else
				{
					if (judge(x - 1))
					{
						update(x - 1, -1);
					}
					if (judge(x + 1))
					{
						update(x + 1, -1);
					}
					ch[x] = 'b';
					if (judge(x))
					{
						update(x, 1);
					}
				}
			}
		}
	}
	return ;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	ace();
	return 0;
}
/*******************************************************************************
Test Data...
4

5 2
bwbwb
0 0 4
0 1 3

5 5
wbwbw
0 0 4
0 0 2
0 2 4
1 2 b
0 0 4

11 11
wbwbbwbwbwb
0 0 10
0 0 1
0 0 2
0 1 2
0 1 3
0 1 5
0 1 6
0 1 7
0 2 6
0 2 7
0 2 10

9 10
bwbbwbwbw
0 2 6
0 2 5
0 2 7
*******************************************************************************/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值