题解:P3372 【模板】线段树 1

· · 题解

upd on 2025/12/17:修改一处错误

前置知识:二分,递归思想。

若这两个东西学的不是极其好,请不要看此篇文章。

线段树是一种用来处理区间问题的数据结构,其适用于处理一些(转化后)满足结合律的值的修改与查询操作。

不过在 OI 中,我更喜欢将这种结合律称为区间可合并性。

在提高组中,我们会经常用到一种想法,就是分块思想。

换句话说,将整个序列分成一块一块,在查询的时候就可以整块整块地查了,而不是一个一个地计算值。

线段树也是如此。

我们考虑这么一棵线段树:

每一个节点都代表一个区间。

我们设根节点,也就是这个代表区间为 [1,8] 的节点的编号为 1。

对于节点 p,设 p 代表的区间是 [l_p,r_p],并设 mid=\lfloor\frac{l_p+r_p}{2}\rfloor。我们约定:p 的左儿子的编号为 2p,代表的区间为 [l_p,mid];p 的右儿子编号为 2p+1,代表的区间为 [mid+1,r_p]。

我们设 data_p 为节点 p 所代表区间的实值,即 \sum_{i=l_p}^{r_p}。

我们令线段树上的每一个节点的值都是这个节点所代表的区间中所有数的和。

在这里,我们其实相当于是将一个序列分成了若干个块,每个块都代表着一个区间所有数的和。

我们在查询的时候,只需要找到我们所需要的区间就行了。

当然,线段树的每一个节点都得有一个初始的形态,也就是建树。

建树的思路也就是向左,向右递归实现。具体看代码吧

inline void build(const int &p,const int &l,const int &r){
    tree[p].l = l,tree[p].r = r;
    if(l == r){
        tree[p].data = a[l];
        return;
    }
    int mid = (l+r)>>1;
    build(lson(p),l,mid);
    build(rson(p),mid+1,r);
    tree[p].data = tree[lson(p)].data+tree[rson(p)].data;
}

其中,tree[p].l 就是 l_p,tree[p].r 就是 r_p,lson(p) 就是 2p,rson(p) 就是 2p+1。

查询和修改在查找区间时所用的思想都是一样的,所以我们先不考虑修改。修改操作中的 pushdown 对初学者来说较为复杂与难以理解,后文再说。

我们考虑如何查询我们想要的区间。

先说算法:

我们考虑递归查询区间。设查询函数为 query(p,l,r),其返回值为 \sum_{i=l}^r a_i。其含义为:我们要查询的目标区间为 [l,r],当前节点为 p。

初始我们递归 query(1,l,r),代表从整个大区间开始,一步一步向下递归查找。

  1. 若 l \leq l_p 且 r_p \leq r,则返回节点 p 的值,即 data_p,递归结束。否则执行以下操作:
  2. 设 mid=\lfloor \frac{l_p+r_p}{2}\rfloor,并设 res 为 0。
  3. 若 l \leq mid,则令 res 加上 query(2p,l,r)
  4. 若 mid<r,则令 res 加上 query(2p+1,l,r)。
  5. 此次递归结束,返回 res。

为什么这样是对的呢?我们一条一条分析:

对于第 1 条,这说明我们找到了一个在 [l,r] 之间的区间,直接返回即可。

对于第 2 \sim 4 条,若左边有我们的目标区间的一部分,则说明我们需要向左递归,右边同理。

对于第 5 条,我们已经处理完了这个查询的任务,向上返回这个值就行了。

这里比较抽象,请读者自行模拟。若此处看不懂,建议先不要往下看。

那么查询的代码如下:

inline int query(const int &p,const int &l,const int &r){
    if(l<=tree[p].l&&tree[p].r<=r){
        return tree[p].data;
    }
    pushdown(p);
    int mid = (tree[p].l+tree[p].r)>>1,res = 0;
    if(l<=mid){
        res+=query(lson(p),l,r);
    }
    if(r>mid){
        res+=query(rson(p),l,r);
    }
    return res;
}

好了,现在查询讲完了。我们开始讲最难的地方:区间修改。

刚才说到线段树实质上是将序列分成了多个块,然后再查询的。那么如果我们对序列进行区间加操作,我们实质上是在线段树上找到对应的区间及其子节点都加上 x。

那么这个时间复杂度是多少呢?

线段树有 O(n) 个节点(注意,O(n) 指的是量级,不是指确切有 n 个节点),如果我们每次都将整个序列都改一遍的话,时间复杂度也是 O(n) 的,会死。

那还不如暴力呢。

这个时候,我们引出线段树中最牛逼,最难,最好玩的一个东西:懒惰标记。

读者们,不要理解为懒惰标记,更好的理解方法是理解为延迟标记。

我们使用与查询操作相似的操作:设区间修改函数为 modify(p,l,r),其含义为:我们要修改的目标区间为 [l,r],当前节点为 p。

我们设节点 p 的懒惰标记值为 lazy_p,节点 p 的实值,也就是其代表的值,为 data_p = \sum_{i=l_p}^{r_p} a_i;设 len(p) 为节点 p 的长度,即 r_p-l_p+1。

初始我们递归 modify(1,l,r),代表从整个大区间开始,一步一步向下递归查找。

  1. 若 l \leq l_p 且 r_p \leq r,则将其懒惰标记 lazy 加上 x,并将这个区间的确切值加上 len(p)。递归结束。否则执行以下操作:
  2. 将节点 p 的懒惰标记下推(pushdown)到它的左右儿子:即令左右儿子的懒惰标记各加上 x,左儿子的实值加上 x \times len(2p),右儿子的实值加上 x \times len(2p+1)。
  3. 设 mid=\lfloor \frac{l_p+r_p}{2}\rfloor,并设 res 为 0。
  4. 若 l \leq mid,则向左递归 modify(2p,l,r)。
  5. 若 mid<r,则向右递归 modify(2p+1,l,r)。
  6. 更新节点 p 的实值 data_p = data_{2p}+data_{2p+1}。此次递归结束,返回。

对于每一条的解释可以直接参考查询。

其实在查询的第二步之前也应该将延迟标记 pushdown 下去。

这里的思想是什么呢?我在区间修改的时候,找到对应的区间,我就不再递归了,取而代之的操作是打上一个延迟标记。等我后续再递归到这个区间的时候再将延迟标记下推,以进行修改。

那么修改的代码如下:

inline void modify(const int &p,const int &l,const int &r,const int &x){
    if(l<=tree[p].l&&tree[p].r<=r){
        tree[p].data+=x*(tree[p].r-tree[p].l+1);
        tree[p].lazy+=x;
        return;
    }
    pushdown(p);
    int mid = (tree[p].l+tree[p].r)>>1;
    if(l<=mid){
        modify(lson(p),l,r,x);
    }
    if(r>mid){
        modify(rson(p),l,r,x);
    }
    tree[p].data = tree[lson(p)].data+tree[rson(p)].data;
}

而 pushdown 的代码就是:

inline void pushdown(const int &p){
    if(!tree[p].lazy){
        return;
    }
    tree[lson(p)].lazy+=tree[p].lazy;
    tree[rson(p)].lazy+=tree[p].lazy;
    tree[lson(p)].data+=tree[p].lazy*(tree[lson(p)].r-tree[lson(p)].l+1);
    tree[rson(p)].data+=tree[p].lazy*(tree[rson(p)].r-tree[rson(p)].l+1);
    tree[p].lazy = 0;
}

这就是我们伟大的线段树。

那么说了这么多,区间可合并性体现在哪呢?

体现在 \sum_{i=l}^r a_i = \sum_{i=l}^{mid}a_i+\sum_{i=mid+1}^r a_i 中。

事实上,\gcd,\operatorname{lcm} 也是具有区间可合并性的。

线段树还有更多更多的用处,这里就不一一细说了。请各位读者自行刷题以理解~

code:

#include<bits/stdc++.h>
#define int __int128
#define R register
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
using namespace std;
namespace fastIO{char *p1,*p2,buf[100000];
    #define nc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
    inline void read(int&n){int x=0,f=1;char ch=nc();while(ch<48||ch>57){if(ch=='-'){f=-1;}ch=nc();}while(ch>=48&&ch<=57){x=(x<<3)+(x<<1)+(ch^48),ch=nc();}n=x*f;}
    inline void read(string&s){s="";char ch=nc();while(ch==' '||ch=='\n'){ch=nc();}while(ch!=' '&&ch!='\n'){s+=ch,ch=nc();}}
    inline void read(char&ch){ch=nc();while(ch==' '||ch=='\n'){ch=nc();}}
    inline void write(int x){if(x<0){putchar('-'),x=-x;}if(x>9){write(x/10);}putchar(x%10+'0');return;}
    inline void write(const string&s){for(R int i=0;i<(int)s.size();i++){putchar(s[i]);}}
    inline void write(const char&c){putchar(c);}
}using namespace fastIO;
constexpr int N = 1e5;
int n,m,a[N+1];
struct segmentTree{
    int l,r,data,add = 0;
}tree[N<<2];
inline void build(const int &p,const int &l,const int &r){
    tree[p].l = l,tree[p].r = r;
    if(l == r){
        tree[p].data = a[l];
        return;
    }
    int mid = (l+r)>>1;
    build(lson(p),l,mid);
    build(rson(p),mid+1,r);
    tree[p].data = tree[lson(p)].data+tree[rson(p)].data;
}
inline void pushdown(const int &p){
    if(!tree[p].add){
        return;
    }
    tree[lson(p)].add+=tree[p].add;
    tree[rson(p)].add+=tree[p].add;
    tree[lson(p)].data+=tree[p].add*(tree[lson(p)].r-tree[lson(p)].l+1);
    tree[rson(p)].data+=tree[p].add*(tree[rson(p)].r-tree[rson(p)].l+1);
    tree[p].add = 0;
}
inline void modify(const int &p,const int &l,const int &r,const int &x){
    if(l<=tree[p].l&&tree[p].r<=r){
        tree[p].data+=x*(tree[p].r-tree[p].l+1);
        tree[p].add+=x;
        return;
    }
    pushdown(p);
    int mid = (tree[p].l+tree[p].r)>>1;
    if(l<=mid){
        modify(lson(p),l,r,x);
    }
    if(r>mid){
        modify(rson(p),l,r,x);
    }
    tree[p].data = tree[lson(p)].data+tree[rson(p)].data;
}
inline int query(const int &p,const int &l,const int &r){
    if(l<=tree[p].l&&tree[p].r<=r){
        return tree[p].data;
    }
    pushdown(p);
    int mid = (tree[p].l+tree[p].r)>>1,res = 0;
    if(l<=mid){
        res+=query(lson(p),l,r);
    }
    if(r>mid){
        res+=query(rson(p),l,r);
    }
    return res;
}
signed main(){
    read(n),read(m);
    for(R int i = 1;i<=n;i++){
        read(a[i]);
    }
    build(1,1,n);
    while(m--){
        int op;
        read(op);
        if(op == 1){
            int l,r,x;
            read(l),read(r),read(x);
            modify(1,l,r,x);
        }else if(op == 2){
            int l,r;
            read(l),read(r);
            write(query(1,l,r)),write('\n');
        }
    }
    return 0;
}