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 l≤i≤r.
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 l≤i≤r.
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 1≤n,q≤105)
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 1≤n,q≤104)
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 1≤l≤r≤n)
Type 2 command is followed by 3 integers l l l, r r r, v a l val val. ( 1