Introduction to Lazy Propagation - Segment Tree (C语言)

这篇博客介绍了懒惰传播算法在线段树中的应用,通过C语言实现。文章首先定义了问题背景,包括数组更新和查询操作,然后讨论了线段树的基本概念和时间复杂度。接着详细解释了如何使用懒惰传播优化更新操作,通过实例展示了如何在树节点中存储待定更新,并在查询时适时应用。最后,文章总结了懒惰传播算法的优点,并指出在实际操作中需要注意的细节。
摘要由CSDN通过智能技术生成

Introduction to Lazy Propagation - Segment Tree (C语言)

简介

小白第一篇blog,以下乃本人浅见,如有疏漏,请各位不吝赐教。
本文主要讲述了lazy propagation算法在线段树中的应用。

例题

You are given an array of n n n integers a 1 a_1 a1, a 2 a_2 a2, …, a n a_n an, and q q q commands of two different types.
Type 1 (Query): given 2 integers l l l and r r r, find the sum of a i a_i ai, where l ≤ i ≤ r l \leq i \leq r lir.
Type 2 (Update): given 3 integers l l l, r r r and v a l val val, add v a l val val to all a i a_i ai, where l ≤ i ≤ r l \leq i \leq r lir.

Input
The first line contains 2 integers n n n and q q q. ( 1 ≤ n , q ≤ 1 0 5 1 \leq n,q \leq 10^5 1n,q105)
The second line contains n n n integers a 1 a_1 a1, a 2 a_2 a2, …, a n a_n an ( 1 ≤ n , q ≤ 1 0 4 1 \leq n,q \leq 10^4 1n,q104)
The next q q q lines each contains a command. The first integer in each line denotes the type of command (either 1 or 2).
Type 1 command is followed by 2 integers l l l, r r r. ( 1 ≤ l ≤ r ≤ n 1 \leq l \leq r \leq n 1lrn)
Type 2 command is followed by 3 integers l l l, r r r, v a l val val. ( 1

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值