此后如竟没有炬火 我便是唯一的光|

Day_Dreamer_D

园龄:3年6个月粉丝:3关注:16

2022-09-10 14:46阅读: 334评论: 0推荐: 0

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
*/

 

本文作者:Day_Dreamer_D

本文链接:https://www.cnblogs.com/2020gyk080/p/16676684.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Day_Dreamer_D  阅读(334)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起