交互演示:区间求和

n = 5

原数组 a

前缀和数组 s

原数组从 a0 开始。s0 = 0,si = si−1 + ai−1,表示前 i 项之和。
绿色:取 sr+1,橙色:减去 sl,剩下的就是区间 [l, r] 的和。

利用前缀和