hdu1166 敌兵布阵--树状数组

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166


#define _CRT_SECURE_NO_DEPRECATE

#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 1000000000
#define eps 0.0001
using namespace std;

int t;
int n;
int a[50005];

int lowBit(int pos)
{
	return pos&(-pos);
}

void update(int pos, int value)
{
	while (pos <= n)
	{
		a[pos] += value;
		pos += lowBit(pos);
	}
}

int sum(int pos)
{
	int ans = 0;
	while (pos >= 1)
	{
		ans += a[pos];
		pos -= lowBit(pos);
	}

	return ans;
}

int main() 
{
	int cas = 1;
	int v;
	char s[7];
	int x, y;
	int l, r;

	scanf("%d", &t);

	while (t--)
	{
		memset(a, 0, sizeof(a));
		scanf("%d", &n);
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &v);
			update(i, v);
		}

		printf("Case %d:\n", cas++);
		while (scanf("%s", s))
		{
			if (s[0] == 'E')
				break;
			scanf("%d%d", &x, &y);
			if (s[0] == 'A')
				update(x, y);
			else if (s[0] == 'S')
				update(x, -y);
			else
				printf("%d\n", sum(y) - sum(x - 1));
		}
	}
	return 0;
}




你可能感兴趣的:(【数据结构】--树状数组)