树状数组板子,单点修改,区间查询,注意处理读入字符的问题。
//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
*/
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/288617.html