HDU 1166 敌兵布阵(树状数组模板)

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

题意:区间查询,单点更新。

题解:树状数组模板。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX=50000+10;
int c[MAX];
int N;
string op;

int lowbit(int x)
{
	return x&(-x);
}

//查询【1,x】区间和
int query(int x)
{
	int s=0;
	while(x>0)
	{
		s+=c[x];
		x-=lowbit(x);
	}
	return s;
}
//单点更新,对节点x加data
void update(int x,int data)
{
	while(x<=N)
	{
		c[x]+=data;
		x+=lowbit(x);
	}
}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int T;
	scanf("%d",&T);
	int tcase=1;
	while(T--)
	{
		scanf("%d",&N);
		memset(c,0,sizeof(c));
		for(int i=1;i<=N;i++)
		{
			int val;
			scanf("%d",&val);
			update(i,val);
		}
		printf("Case %d:\n",tcase++);
		while(cin>>op&&op!="End")
		{
			int a,b;
			scanf("%d%d",&a,&b);
			if(op=="Query") printf("%d\n",query(b)-query(a-1));
			if(op=="Add") update(a,b);
			if(op=="Sub") update(a,-b);
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值