线段树

作者: endless_e48c | 来源:发表于2020-02-12 19:42 被阅读0次

    一、线段树建树、单点修改、区间查询

    #include <cstdio>
    const int MAXN = 10005;
    int arr[MAXN] = {1, 3, 5, 7, 9, 11}, tree[MAXN];//arr表示原数组,tree表示线段树数组 
    void build_tree(int node, int start, int end) {//建树,node表示当前节点,[start,end]表示node在arr数组内的区间 
        if (start == end) {
            tree[node] = arr[start];
            return;
        }
        int mid = (start + end) / 2;
        int left_node = node * 2 + 1;//左儿子节点编号 
        int right_node = node * 2 + 2;//有儿子节点编号 
        build_tree(left_node, start, mid);//构建node节点的左子树 
        build_tree(right_node, mid + 1, end);//构建node节点的右子树 
        tree[node] = tree[left_node] + tree[right_node];     
    }
    void update_tree(int node, int start, int end, int idx, int val) {//修改线段树上的值,idx表示修改的下标,val表示修改的值 
        if (start == end) {
            tree[node] = arr[idx] = val;
            return;
        }
        int mid = (start + end) / 2;
        int left_node = node * 2 + 1;
        int right_node = node * 2 + 2;
        if (idx <= mid) {//判断idx是在左子树上还是右子树上 
            update_tree(left_node, start, mid, idx, val);
        } else {
            update_tree(right_node, mid + 1, end, idx, val);
        }
        tree[node] = tree[left_node] + tree[right_node];
    }
    int query_tree(int node, int start, int end, int L, int R) {//查询[L, R]区间上的值 
        //printf("%d %d", start, end);
        if (end < L || start > R) {//当前节点表示的区间不在节点范围内 
            return 0;
        } else if (start == end || (L <= start && end <= R)) {//剪枝 
            return tree[node];
        }
        int mid = (start + end) / 2;
        int left_node = node * 2 + 1; 
        int right_node = node * 2 + 2;
        int left_sum = query_tree(left_node, start, mid, L, R);//左子树和 
        int right_sum = query_tree(right_node, mid + 1, end, L, R);//右子树和 
        //printf("%d %d %d\n", start, end, left_sum + right_sum);
        return left_sum + right_sum;
    }
    int main() {
        build_tree(0, 0, 5);
        for (int i = 0; i < 15; i++) {
            printf("%d ", tree[i]);
        }
        printf("\n");
        /*update_tree(arr, tree, 0, 0, 5, 1, 2);
        for (int i = 0; i < 15; i++) {
            printf("%d ", tree[i]);
        }*/
        printf("%d", query_tree(0, 0, 5, 0, 0));
        return 0;
    }
    

    二、线段树建树、区间修改、区间查询

    #include <cstdio>
    typedef long long ll;
    const int MAXN = 1e6 + 5;
    ll tree[MAXN << 2], tag[MAXN << 2], a[MAXN], tot;
    void pushUp(ll k) {  //向上传递左子树和右子树数字之和
        tree[k] = tree[k << 1] + tree[k << 1 | 1];
    }
    void buildTree(ll left, ll right, ll k) {  //建立线段树
        if (left == right) {
            tree[k] = a[++tot];
            return;
        }
        ll mid = (left + right) >> 1;
        buildTree(left, mid, k << 1);
        buildTree(mid + 1, right, k << 1 | 1);
        pushUp(k);
    }
    void pushDown(ll left, ll right, ll k) {  //向下传递延迟标记
        if (tag[k] != 0) {
            ll mid = (left + right) >> 1;
            tree[k << 1] += (mid - left + 1) * tag[k];
            tree[k << 1 | 1] += (right - mid) * tag[k];
            tag[k << 1] += tag[k];
            tag[k << 1 | 1] += tag[k];
            tag[k] = 0;
            pushUp(k);
        }
    }
    void upData(ll left, ll right, ll L, ll R, ll k, ll val) {
        if (L <= left && right <= R) {
            tree[k] += (right - left + 1) * val;
            tag[k] += val;
            return;
        }
        ll mid = (left + right) >> 1;
        pushDown(left, right, k);
        if (mid >= L) {
            upData(left, mid, L, R, k << 1, val);
        }
        if (mid < R) {
            upData(mid + 1, right, L, R, k << 1 | 1, val);
        }
        pushUp(k);
    }
    ll query(ll left, ll right, ll L, ll R, ll k) {
        if (L <= left && right <= R) {
            return tree[k];
        }
        pushDown(left, right, k);
        ll mid = (left + right) >> 1, sum = 0;
        if (mid >= L) {
            sum += query(left, mid, L, R, k << 1);
        }
        if (mid < R) {
            sum += query(mid + 1, right, L, R, k << 1 | 1);
        }
        pushUp(k);
        return sum;
    }
    int main() {
        ll n, q, l, r, x, flag;
        scanf("%lld %lld", &n, &q);
        for (int i = 1; i <= n; i++) {
            scanf("%lld", &a[i]);
        }
        buildTree(1, n, 1);
        for (int i = 1; i <= q; i++) {
            scanf("%lld", &flag);
            if (flag == 1) {
                scanf("%lld %lld %lld", &l, &r, &x);
                upData(1, n, l, r, 1, x);
            } else {
                scanf("%lld %lld", &l, &r);
                printf("%lld\n", query(1, n, l, r, 1));
            }
        }
        return 0;
    }
    

    相关文章

      网友评论

        本文标题:线段树

        本文链接:https://www.haomeiwen.com/subject/vsncfhtx.html