This article is mainly about the interval modification of a tree-like array
Because a single point change, no doubt time-consuming too long
So, the wit of human yy out of using differential to represent the array
For ease of understanding, a little simpler array {an}:a[1]=0, a[2]=0, a[3]=0, a[4]=0, a[5]=0, a[6]=0, a[7]=0, a[8]=0, a[9]=0
Using differential thinking, delta[x] means a[x]-a[x-1]
Apparently, at first delta[]=0
We first calculate the first n and {sn} to
Then don't blink, here is the time to witness the miracle!
There's a dividing line that looks formal.
We want to a[3] to a[6] the whole paragraph to increase the length of 3 units, let delta[3]+=3, delta[7]-=3
Think of it as a ladder, one end lifted and the other end pressed down. (self-repairing of brain)
So now the a[x]+=delta[1]+delta[2]+......+delta[x-1]+delta[x]
I'm going to get the first 7 items and, s[7]+= each should be added differential
Jinan s[7]+= (delta[1]) + (delta[1]+delta[2]) + (delta[1]) +delta[2]+delta[3]) +......+ (delta[1]+delta[2]+delta[3]+delta[4]+ DELTA[5]+DELTA[6]+DELTA[7])
Let me draw a picture (for the sake of explaining that only the difference is drawn)
Obviously moved over the bricks all know that this brick is not good to move because it does not square
Then we'll change it.
(Why is it so ugly, because I can't draw and lazy)
My sister! Now, that's a lot of bricks,!!!!!!!!!.
(7+1) (∑delta "1~7")-(Delta "1" *1+delta "2" *2+......+delta "7" *7)
The Delta "n" behind the *n is hard to see, isn't it?
That means "Deltai".
Define Deltai "I" =delta "I" *i
Obviously because we're going to do a lot of sums, we maintain deltai and delta as tree arrays.
So
1 // Modify: Add the [L, R] interval x 2 update (Delta, L, X); 3 Update (Delta, R+1 ,-x); 4 Update (Deltai, L, x * L); 5 Update (Deltai, R+1 ,-X * (R+1 6 // Updata equivalent to the add
of the previous article
// query: [L, R] interval and Long Long 1 1 1 ); Long Long 1) * Query (Delta, R)- query (Deltai, R); Ans=sumr- suml; // query corresponds to the sum of the previous article
Isn't it amazing?
Thank
"A little bit every day" blog (I like the name)
Refer to the Post address
Tree-like array small white (2) and interval modification