前缀和 差分

 前缀和

c = [0]  *  11

a = list(map(int, input().split()))

for i in range(1, 11):

    c[i] = c[i - 1] + a[i - 1]

print(c)

1 2 3 4 5 6 7 8 9 10
[0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55]

python 中自带前缀和函数,在itertools中的accumulate,请转到该模块


** Process exited - Return Code: 0 **

差分

假设 数组a = [1, 2, 3, 4, 5] 
对 数组a [1, 3]进行加 5 操作
设差分数组:
D[0] = a[0] = 1
D[1] = a[1] - a[0] = 1
D[2] = a[2] - a[1] = 1
D[3] = a[3] - a[2] = 1
D[4] = a[4] - a[3] = 1
进行操作D[L] += 5 D[R + 1] -= 5 
这里L, R = 1, 3 => D[1] = 6, D[4] = 1 - 5 = -4
a[0] = D[0] = 1
a[1] = D[1] + a[0] = 7
a[2] = a[1] + D[2] = 8
a[3] = a[2] + D[3] = 9
a[4] = a[3] + D[4] = 9 - 4 = 5
由此完成差分!




 

评论

此博客中的热门博文

binary_search

python 分支语句语法练习

素数筛