Using differential, a differential sequence (e.g. (1,2,3,5) with a differential sequence of (1,1,1,2)) is obtained first.
When all the numbers in the [I,j] segment are added to M, the I position of the differential sequence plus m, minus m at the j+1 position
Sum using a tree-like array
The following is the Pascal program:
var a,fai:array[0..1000] of Longint; C:char; N,i,x,y,f,m:longint;function lowbit (x:longint): Longint;begin lowbit:=x and (-X); end;procedure Modify (k,d: Longint); begin while k<=n do begin Fai[k]:=fai[k]+d; K:=k+lowbit (k); End;end;function sum (x:longint): Longint;begin sum:=0; While x>0 do begin Sum:=sum+fai[x]; X:=x-lowbit (x); End;end;begin readln (n); For I:=1 to n do begin read (A[i]); Modify (I,a[i]-a[i-1]); End; READLN (m); For I:=1 to M do begin Read (c); If c= ' C ' then begin Readln (x,y,f); Modify (x,f); Modify (y+1,-f); End; If c= ' Q ' then begin readln (x); Writeln (sum (x)); End; End; Readln;end.
"Tree Array" segment modified, point query