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
*/
标签:树状,省选,题解,ll,long,MAXN,数组 来源: https://www.cnblogs.com/2020gyk080/p/16676684.html