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
*******************************************************************************/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章