动态开点线段树单点修改区间修改
末然Ender
·
·
算法·理论
//试作动态开点线段树
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+5,M=1e6+10;
const int inf=0x3f3f3f3f;
struct node{
int lc,rc;//表示该节点所的左右儿子 <--这地方变了
ll val/*所存的值*/;
}t[N<<2];
int a[N];//原数组
int tot;
//Pushup:子节点的值的上传给父亲节点
void pushup(int root){
t[root].val=t[t[root].lc].val+t[t[root].rc].val;
}
//单点修改 将范围[lt,rt]中的x节点加上v
inline void update(int& root,int lt,int rt,int x,int v){
if(!root) root=++tot;//如果没有建该节点就建出来
if(lt==rt){//找到了,任务完成=)
t[root].val+=v;
return;
}
//继续向下找
int mid=lt+(rt-lt>>1);
if(x<=mid)update(t[root].lc,lt,mid,x,v);//如果修改的区间覆盖了左儿子就修改左子树
if(x>mid)update(t[root].rc,mid+1,rt,x,v);//右儿子同理
pushup(root);//值上传
}
//区间查询 查询[lt,rt]范围中区间[l,r]的和
ll query(int root,int lt,int rt,int l,int r){
//点不存在直接返回0
if(!root)return 0;
//如果被覆盖,直接返回维护的值
if(l<=lt&&r>=rt) return t[root].val;
int mid=lt+(rt-lt>>1);
//累加查询的答案
ll ans=0;
if(l<=mid)ans+=query(t[root].lc,lt,mid,l,r);
if(r>mid)ans+=query(t[root].rc,mid+1,rt,l,r);
return ans;
}
int n,m,rt;
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
int num;
scanf("%d",&num);
update(rt,1,n,i,num);
}
while(m--){
int op;
scanf("%d",&op);
if(op==1){
int x,y,k;
scanf("%d%d",&x,&k);
update(rt,1,n,x,k);
}else{
int x,y;
scanf("%d%d",&x,&y);
printf("%lld\n",query(rt,1,n,x,y));
}
}
return 0;
}
/*
Input
5 5
1 2 3 4 5
2 1 5
1 1 1
2 1 2
1 3 3
2 1 5
Output
15
4
19
*/