题解 | #【模板】差分#
【模板】差分
https://www.nowcoder.com/practice/4bbc401a5df140309edd6f14debdba42?tpId=230&tqId=2022327&ru=/exam/oj&qru=/ta/dynamic-programming/question-ranking&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D230
import sys s = input().split() n,q =int(s[0]),int(s[1]) data =input().split() data = list(map(lambda x:int(x),data)) i=1 querylist=[] while i<=q: querylist.append(input().split() ) i+=1 difference = [i for i in range(len(data))] difference[0]=data[0] #计算出差分数组 for i in range(1,len(data)): difference[i]=data[i]-data[i-1] for x in querylist: #print(x) l,r,k=int(x[0]),int(x[1]),int(x[2]) #更新差分数组,l-1的值和r+1的值,如果r+1-1等于k,不用更新r了就 difference[l-1]+=k if r==len(difference): pass else: difference[r+1-1]-=k data[0] =difference[0] #print(difference) #原数列的第i项等于差分数列的前i项和 for i in range(1,len(difference)): data[i]=data[i-1]+difference[i] for x in data: print(x,end=" ")