C T H(下)
#include<bits/stdc++.h>
using namespace std;
int n,m;
const int MAX=1e5+10;
int a[MAX];
struct node{
int l,r,max,sum;
}tree[MAX<<2];
int lson(int x)
{
return x << 1;
}
int rson(int x)
{
return x<<1|1;
}
void build(int rt,int l,int r)
{
tree[rt].l=l;
tree[rt].r=r;
if(l==r)
{
tree[rt].sum=tree[rt].max=a[l];
return;
}
int mid=(l+r)>>1;
build(lson(rt),l,mid);
build(rson(rt),mid+1,r);
tree[rt].sum=tree[lson(rt)].sum+tree[rson(rt)].sum;
tree[rt].max=max(tree[lson(rt)].max,tree[rson(rt)].max);
}
void update(int rt,int pos,int v)
{
//cout<<'#';
if(tree[rt].l==tree[rt].r)
{
tree[rt].sum=tree[rt].max=v;
return ;
}
int mid=(tree[rt].l+tree[rt].r)>>1;
if(pos<=mid) update(lson(rt),pos,v);
else update(rson(rt),pos,v);
tree[rt].sum=tree[lson(rt)].sum+tree[rson(rt)].sum;
tree[rt].max=max(tree[lson(rt)].max,tree[rson(rt)].max);
}
int query(int rt,int l,int r)
{
if(l<=tree[rt].l&&tree[rt].r<=r) return tree[rt].sum;
int mid=(tree[rt].l+tree[rt].r)>>1;
if(r<=mid) return query(lson(rt),l,r);
else if(l>mid) return query(rson(rt),l,r);
else return (query(lson(rt),l,r)+query(rson(rt),l,r));
}
int main()
{
cin>>n;
if(n==0)
{
//cout<<0;
return 0;
}
for(int i=1;i<=n;i++) cin>>a[i];
build(1,1,n);
cin>>m;
string s;
for(int i=1;i<=m;i++)
{
//for(int i=1;i<=n;i++)
cin>>s;
if(s[0]=='S')
{
int x,y;
cin>>x>>y;
cout<<query(1,x,y)<<endl;
}
else
{
int x,y;
cin>>x>>y;
update(1,x,y+a[x]);
a[x]=y+a[x];
}
}
}
CTH 是这样的使人快活,可是没有他,别人也便这么过。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)