AcCoders 7961 Problem D:【省选基础数据结构 树状数组】树状数组 题解

2022/9/10 23:23:31

本文主要是介绍AcCoders 7961 Problem D:【省选基础数据结构 树状数组】树状数组 题解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

树状数组板子,单点修改,区间查询,注意处理读入字符的问题。

//7961 Problem D:【省选基础数据结构 树状数组】树状数组
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=100005;
ll c[MAXN],a[MAXN],n;
#define lowbit(x) ((x)&-(x))
void add(ll x,ll y)
{
	while(x<=n)
	{
		c[x]+=y;
		x+=lowbit(x);
	}
	return;
}
ll sum(ll x)
{
	ll res=0;
	while(x)
	{
		res+=c[x];
		x-=lowbit(x);
	}
	return res;
}
int main()
{
	ll m,x,y;
	char op;
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
		add(i,a[i]);
	}
	/*for(int i=1;i<=n;i++)
	{
		cerr<<c[i]<<' ';
	}
	cerr<<endl;*/
	scanf("%lld",&m);
	while(m--)
	{
		do
		{
			op=getchar();
		}
		while(op!='C'&&op!='Q');
		scanf("%lld%lld",&x,&y);
		if(op=='C')
		{
			add(x,-a[x]);
			a[x]=y;
			add(x,a[x]);
		}
		else if(op=='Q')
		{
			printf("%lld\n",sum(y)-sum(x-1));
		}
	}
	return 0;
}
/*
 * AcCoders-省选基础5—数据结构
 * http://www.accoders.com/problem.php?cid=2716&pid=3
 * C++20 -O0
 * 2022.9.10
 */

 



这篇关于AcCoders 7961 Problem D:【省选基础数据结构 树状数组】树状数组 题解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程