void build(int p,int l,int r)
{
if(l==r){
tree[p]=a[l];
return ;
}
int mid=(l+r)>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
tree[p]=tree[p<<1]+tree[p<<1|1];
}
void update(int p,int l,int r,int x,int num)
{
if(l==r){
tree[p]=num;
return ;
}
int mid=(l+r)>>1;
if(x<=mid){
update(p<<1,l,mid,x,num);
}else{
update(p<<1|1,mid+1,r,x,num);
}
tree[p]=tree[p<<1]+tree[p<<1|1];
}
int find(int p,int l,int r,int x,int y)
{
if(x<=l&&r<=y){
return tree[p];
}
int mid=(l+r)>>1;
if(y<=mid){
return find(p<<1,l,mid,x,y);
}else if(x>mid){
return find(p<<1|1,mid+1,r,x,y);
}else{
return find(p<<1,l,mid,x,mid)+find(p<<1|1,mid+1,r,mid+1,y);
}
}
线段树模板
最新推荐文章于 2024-03-01 00:18:20 发布