线段树维护单点修改区间和
直接上代码吧
范围n,m<=
C是修改,Q是查询
#include<bits/stdc++.h>
using namespace std;
#define lc (u<<1)
#define rc ((u<<1)+1)
#define mid ((l+r)>>1)
int t[400050],n,m,x,y;
inline int read(){
char ch=getchar();
int res=0,zgs=1;
while(!isdigit(ch)) {if(ch=='-') zgs=-1;ch=getchar(); }
while(isdigit(ch)) res=(res<<3)+(res<<1)+(ch^48),ch=getchar();
return res*zgs;
}
inline void pushdown(int u,int l,int r,int k,int v)
{
if(l==r)
{
if(l==k)
t[u]+=v;
return;
}
t[u]+=v;
if(mid>=k)
pushdown(lc,l,mid,k,v);
if(mid<k)
{
pushdown(rc,mid+1,r,k,v);
}
}
inline int query(int u,int l,int r,int st,int des)
{
int ans=0;
if(l>=st&&r<=des)
{
return t[u];
}
if(l>des||r<st)
{
return 0;
}
if(l==r)
{
return t[u];
}
if(mid<st)
{
return query(rc,mid+1,r,st,des);
}
if(des<=mid)
{
return query(lc,l,mid,st,des);
}
ans+=query(lc,l,mid,st,des);
ans+=query(rc,mid+1,r,st,des);
return ans;
}
int main(){
n=read(),m=read();
char c;
for(int i=1;i<=m;i++)
{
cin>>c;
if(c=='C')
{
x=read(),y=read();
pushdown(1,1,n,x,y);
}
else if(c=='P')
{
x=read(),y=read();
cout<<query(1,1,n,x,y)<<'\n';
}
}
return 0;
}