1. add a number to the whole range, and perform single-point calculation: This is a very common method. It is regarded as how many line segments are covered. For a [l, the r] k operation is converted to the secondary array B [l] k, B [r1]-k. The tree array maintains the B [I] prefix and just fine ...... Specifically, a [I] difference is generated into a new array B [I], so that B [I] a [I]-a [I-1], so that when the segment is modified: for il or ir1,
1. add a number to the whole range, and perform single-point calculation: This is a very common method. It is regarded as how many line segments are covered. For a [l, the r] k operation is converted to the secondary array B [l] k, B [r1]-k. The tree array maintains the B [I] prefix and just fine ...... Specifically, after a [I] difference is generated into a new array B [I], so that B [I] = a [I]-a [I-1], so that when the segment is modified: for il or ir1,
1. Increase the number of integers in the interval. Single Point evaluation:
This is a very common method, which is used as the number of line segments covered. The operations on a [l, r] + k are converted to the operations on the secondary array B [l] + k, B [r + 1]-k, the tree array maintains B [I] prefix and good ......
Specifically, after a [I] difference is generated into a new array B [I], so that B [I] = a [I]-a [I-1], so that when the segment is modified:
To I R + 1, the value of a [I] does not change, so B [I] does not change; l But B [l] '= (a [l] + k)-a L-1] = B [l] + k; B [r + 1] '= a [r + 1]-(a [r] + k) = B [r + 1]-k.
Search for the prefix of B [I] And find that:
Sum (p) = B [1] + B [2] +... + B [p] = (a [1]-a [0]) + (a [2]-a [1]) +... + (B [p]-B [P-1]) = a [p]-a [0] = a [p]
In this way, the single point of value has also come out, and the code (applies the original BIT ):
struct BIT_ex { BIT t; void init(int s) {t.init(s);} void change(int l, int r, _int k) {t.change(l,k); t.change(r+1,-k);} _int get(int p) {return t.sum(p);}};
2. Add a number to the total range, and calculate the Range Sum (prefix and ):
It seems not very common. Popularize it ......
The overall modification of the interval has been made, and it must be further concluded ......
The difference array above obtains sum (p) = a [p]. Here, the prefix and, of course, a [0] + a [1] +... + a [p ~ The rest is arithmetic.
A [0] + a [1] + a [2] + a [3] +... + a [p] = 0 + sum (1) + sum (2) + sum (3) +... + a [p] = (B [1]) + (B [1] + B [2]) + (B [1] + B [2] + B [3]) +... + (B [1] +... + B [p])
B [1] appears in sum (1. p), p in total, B [2] appears in sum (2. p) (1), and so on.
Original formula = p * B [1] + (p-1) * B [2] + (P-2) * B [3] +... + 1 * B [p]
I wanted to treat each item as a whole and use BIT, but I found that for different p values, each item will also change, obviously there is no way ......
Here we can see that both the subscript and the subscript of the preceding coefficient and B [] are (p + 1). Use the inverted order addition method to calculate the inverse order:
Original formula = (p + 1) * (B [1] + B [2] + B [3] +... + B [p])-(1 * B [1] + 2 * B [2] + 3 * B [3] +... + p * B [p])
The prefix and the prefix obtained for B [I] are directly in the brackets. The following parentheses are the same for I * B [I!
Okay, so we can build two bits. One is to maintain the prefix sum of B [I], and the other is to maintain the prefix sum of I * B [I]. The maintenance method is the same as above.
In order to reduce the dependency, the original BIT is still set here, And BIT_ex will be better written. The code above:
struct BIT_im { BIT t1; BIT t2; void init(int s) {t1.init(s); t2.init(s);} void chage(_int l, _int r, _int k) { t1.change(l,k); t1.change(r+1,-k); t2.change(l,l*k); t2.change(r+1,-(r+1)*k); } _int sum(_int p) {return (p+1)*t1.sum(p)-t2.sum(p);}};
3. Two-dimensional (multi-dimensional) tree array:
One dimension is the prefix and sum (p) = sum {a [I], I <= p} calculates the range and
For two dimensions, sum (x, y) = sum {a [I] [j], I <= x & j <= y} calculates the rectangle and
Multi-dimensional similar, only two-dimensional
Static maintenance is simple:
for (int i=1; i<=n; i++) for (int j=1; j<=m; j++) s[i][j]=a[i][j]+s[i-1][j]+s[i][j-1]-s[i-1][j-1];
In this way, if the sum of the rectangle (x1, y1)-(x2, y2) is obtained, ans = s [x2] [y2]-s [x1-1] [y2]-s [x2, y1-1] + s [x1-1] [y1-1]
What about Dynamic Maintenance? First, you can use BIT to maintain a prefix and maintain the Array (a [I]) [] prefix and BIT prefix and ...... Haopuan
In short, you can use a set of bits to maintain the sum of multiple parallel lines, and then use a BIT orthogonal to hold them for maintenance, at this time, the meaning of the BIT group has actually changed ......
This idea is similar to the following code (thanks to mlzmlz95 ):
for(i=1;i<=n;++i) for(j=1;j<=m;++j) sum[i][j]=sum[i][j-1]+a[i][j];for(j=1;j<=m;++j) for(i=1;i<=n;++i) sum[i][j]=sum[i][j]+sum[i-1][j];
The Code is as follows:
struct BIT_2D { BIT c[N]; int n; void init(int s1,int s2) {n=s1; while (s1) c[s1--].init(s2);} void change(int x,int y,int k) {for (; x<=n; x+=x&-x) c[x].change(y,k);} int sum(int x,int y) {int s=0; for (; x; x-=x&-x) s+=c[x].sum(y); return s;}};
In this implementation, we use the outer BIT to determine the update of the layer BIT. during initialization, We need to cycle them again and set the size.
As for 3D, let's hold another 2D, but the formula for finding a cube is a bit complicated> _ <
Is it necessary to write all the preceding dimensions for multi-dimensional processing ...... It's too troublesome. Let's just click "inline" and make some modifications:
struct BIT_2D_in { int c[N][M],n,m; void init(int s1,int s2) {n=s1; m=s2; memset(c,0,sizeof(c));} void change(int xx,int yy,int k) { for (int x=xx; x<=n; x+=x&-x) for (int y=yy; y<=m; y+=y&-y) c[x][y]+=k; } int sum(int xx,int yy) { int s=0; for (int x=xx; x; x-=x&-x) for (int y=yy; y; y-=y&-y)s+=c[x][y]; return s; }};
In this implementation, we can see that these two re-cycles directly control the subscript, then the multi-dimensional directly add a few re-cycles to complete, xx, yy when the number of parameters can be in the next loop to write x, y, I'm lazy ......
No ...... In fact, the full text may not be fresh
Next forecast: the BIT of the evil track (write failure)