线段树 SegmentTree note
2026-11-13 18:54:53
发布于:上海
本人正在试图让自己看懂这个东西
前置芝士:树状数组
时间复杂度
看图:
tree[1]->[1,n]
/ \
tree[2]->[1,(n+1)/2] tree[3]->[n+1,n]
/ \ / \
tree[4] tree[5] tree[6] tree[7]
......
/
tree[x]->[1,1]
肯由昂得斯丹德?
(感觉上图有点像二分的思想)
这啥玩意儿
居然看懂代码了
namespace ST{
ll a[N],tr[N*4];
void push_up(int p){
tr[p]=tr[p*2]+tr[p*2+1];
}void build(int p,int l,int r){
if(l==r){tr[p]=a[l];return;}
int m=l+(r-l)/2;
build(p*2,l,m);
build(p*2+1,m+1,r);
push_up(p);
}void update(int p,int l,int r,int x,ll v){
if(l==r){tr[p]=v;return;}
int m=l+(r-l)/2;
if(x<=m)update(p*2,l,m,x,v);
else update(p*2+1,m+1,r,x,v);
push_up(p);
}ll query(int p,int l,int r,int L,int R){
if(L<=l and r<=R)return tr[p];
int m=l+(r-l)/2;
ll res=0;
if(L<=m)res+=query(p*2,l,m,L,R);
if(R>m)res+=query(p*2+1,m+1,r,L,R);
return res;
}
}
U644876 区区一个模版,看我一击破之(
namespace ST{
ll a[N],tr[N*4];
void push_up(int p){
tr[p]=tr[p*2]+tr[p*2+1];
}void build(int p,int l,int r){
if(l==r){tr[p]=a[l];return;}
int m=l+(r-l)/2;
build(p*2,l,m);
build(p*2+1,m+1,r);
push_up(p);
}void update(int p,int l,int r,int x,ll v){
if(l==r){tr[p]=v;return;}
int m=l+(r-l)/2;
if(x<=m)update(p*2,l,m,x,v);
else update(p*2+1,m+1,r,x,v);
push_up(p);
}ll query(int p,int l,int r,int L,int R){
if(L<=l and r<=R)return tr[p];
int m=l+(r-l)/2;
ll res=0;
if(L<=m)res+=query(p*2,l,m,L,R);
if(R>m)res+=query(p*2+1,m+1,r,L,R);
return res;
}
}
using namespace ST;
namespace HQ{
void Main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
}build(1,1,n);
while(q--){
int op,x,y;
cin>>op>>x>>y;
if(op==1)update(1,1,n,x+1,y);
else cout<<query(1,1,n,x+1,y)<<'\n';
}
return;
}
}
U644889 区区第二模版,看我再击破之(((
namespace ST{
ll a[N],tr[N*4];
void push_up(int p){
tr[p]=min(tr[p*2],tr[p*2+1]);
}void build(int p,int l,int r){
if(l==r){tr[p]=a[l];return;}
int m=l+(r-l)/2;
build(p*2,l,m);
build(p*2+1,m+1,r);
push_up(p);
}void update(int p,int l,int r,int x,ll v){
if(l==r){tr[p]=v;return;}
int m=l+(r-l)/2;
if(x<=m)update(p*2,l,m,x,v);
else update(p*2+1,m+1,r,x,v);
push_up(p);
}ll query(int p,int l,int r,int L,int R){
if(L<=l and r<=R)return tr[p];
int m=l+(r-l)/2;
ll res=INF;
if(L<=m)res=min(query(p*2,l,m,L,R),res);
if(R>m)res=min(query(p*2+1,m+1,r,L,R),res);
return res;
}
}
using namespace ST;
namespace HQ{
void Main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
}build(1,1,n);
while(q--){
int op,x,y;
cin>>op>>x>>y;
if(op==1)update(1,1,n,x+1,y);
else cout<<query(1,1,n,x+1,y)<<'\n';
}
return;
}
}
11/13
懒标记不会啊,来个大蛇教教我呃呃呃呃呃呃呃
U646399区区 ___ ___ ___ ___ ,___ ___ ___ ___ ___ ___(((
namespace ST{
ll a[N];
struct segtree{
ll lazy;
}tr[N*4];
int n,m;
ll query(int p,int l,int r,int x){
ll res=tr[p].lazy;
if(l==r)return res;
int mid=l+(r-l)/2;
if(x<=mid)return res+query(p*2,l,mid,x);
return res+query(p*2+1,mid+1,r,x);
}void update(int p,int l,int r,int L,int R,ll v){
if(L<=l and r<=R){
tr[p].lazy+=v;
return;
}int mid=l+(r-l)/2;
if(L<=mid)update(p*2,l,mid,L,R,v);
if(R>mid)update(p*2+1,mid+1,r,L,R,v);
}
}
using namespace ST;
namespace HQ{
int n,m;
void Main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m;
while(m--){
int op;
cin>>op;
if(op==1){
int l,r;
ll v;
cin>>l>>r>>v;
update(1,1,n,l+1,r,v);
}else{
int x;
cin>>x;
cout<<query(1,1,n,x+1)<<'\n';
}
}
return;
}
}
全部评论 4
d
2026-10-30 来自 上海
1感觉线段树理解起来不难,就是做题的时候调代码容易飞起来
1周前 来自 浙江
0这是真的,能理解但写出的石山自己看不懂
1周前 来自 上海
0
dsa
2026-11-09 来自 上海
0PPP
1周前 来自 上海
0
d
2026-10-30 来自 上海
0
























有帮助,赞一个