树状数组模板 HDU1166

#include
#include
#include
using namespace std;
int a[50005];
int tree[50005];//树状数组 
char s[20];
int n;
void add(int k, int num)
{
	while(k <= n)
	{
		tree[k] += num;
		k += k & (-k);//lowbit(k) 
	}
}
int search(int k)
{
	int sum=0;
	while(k)
	{
		sum += tree[k];
		k -= k & (-k);
	}
	return sum;
}
int main()
{
	int cases, i, j, x, y;
	j = 1;
	cin>>cases;
	while(cases--)
	{
		memset(tree, 0, sizeof(tree));
		printf("Case %d:\n",j++);
		scanf("%d", &n);
		for(i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			add(i, a[i]);
		} 
		while(scanf("%s", s) != EOF)
		{
			if(strcmp(s, "End") == 0)
			{
				break;
			}
			else if(strcmp(s, "Query") == 0)
			{
				scanf("%d%d", &x, &y);
				int ans = search(y) - search(x-1);
				cout<

你可能感兴趣的:(树状数组,acm)