1. Pity Dorado (Single point update, Interval summation)
Code:
1#include <iostream>2 using namespacestd;3 4 Const intMAXN = 1e5 +Ten;5 intTREE[MAXN], N;6 7 intLowbit (intx) {//returns the POW (2, k), where K is the number of the end 0, which returns the value of the lowest bit 18 returnX &-x;9 }Ten One voidAddintXintD) {//Add D to the tree array corresponding position A while(x <=N) { -TREE[X] + =D; -X + =lowbit (x); the } - } - - intSumintx) {//sum up, return [1, x] all elements and + intAns =0; - while(X >0){ +Ans + =Tree[x]; AX-=lowbit (x); at } - returnans; - } - - intMainvoid){ - intx; inCIN >>N; - for(inti =1; I <= N; i++){ toCIN >>x; + Add (i, x); - } the for(inti =1; I <= N; i++){ *cout << sum (i) <<Endl; $ }Panax Notoginseng return 0; -}
View Code
2. Pity Dorado section (interval update, single point evaluation)
Use an array d to store the difference of the adjacent elements in the target array a, i.e. I > 1 o'clock, d[i] = a[i]-a[i-1]; i = = 1 o'clock, d[i] = A[i].
Then there is a[i] = d[1] + ... + d[i]. To add the elements of the A array interval [L, R] to key, it is obvious that d[l] + = key, D[r + 1]-= key are required.
Obviously, you just need to maintain the D array with a tree-like array.
Code:
1#include <iostream>2#include <stdio.h>3 using namespacestd;4 5 Const intMAXN = 1e5 +Ten;6 intA[MAXN], TREE[MAXN], D[MAXN], N;7 8 intLowbit (intx) {9 returnX &-x;Ten } One A voidAddintXintAD) { - while(x <=N) { -TREE[X] + =AD; theX + =lowbit (x); - } - } - + intSumintx) {//value of sum (x) is a[x] - intAns =0; + while(X >0){ AAns + =Tree[x]; atX-=lowbit (x); - } - returnans; - } - - intMainvoid){ in intx, Q, L, R; -CIN >>N; to for(inti =1; I <= N; i++){ +scanf"%d", &a[i]); - if(i = =1) D[i] =A[i]; the ElseD[i] = a[i]-a[i-1]; * } $ for(inti =1; I <= N; i++){Panax Notoginseng Add (i, d[i]); - } theCIN >>Q; + while(q--){ ACIN >> L >> r >> x;//Add x to the elements of the interval [L, R] the Add (l, x); +Add (R +1, -x); - for(inti =1; I <= N; i++){ $cout << sum (i) <<" "; $ } -cout <<Endl; - } the}
View Code
3. Change paragraph to seek paragraph
Similar to 2, first open a difference fraction Group D
Then there are:
A1 + A2 + ... + an
= D1 + (D1 + D2) + ... + (D1 + d2 + ... + dn)
= n * d1 + (n-1) * d2 + ... + DN
= n * (d1 + d2 + ... + DN)-(0 * d1 + 1 * d2 + ... (n-1) * dn)
Re-make c[i] = (i-1) * di
Then the primitive can be simplified as:
n * (d1 + d2 + ... + DN)-(c1 + C2 + ... cn)
It is clear that the sum of the D and C arrays can be solved with a tree array. In the case of a 2, it is only necessary to make a single point modification to the D and C arrays.
Example: http://codevs.cn/problem/1082/
Test instructions: Chinese question eh ~
Idea: Tree array interval update interval summation template
Code:
1#include <iostream>2#include <stdio.h>3 #definell Long Long4 using namespacestd;5 6 Const intMAXN = 2e5 +Ten;7ll A[MAXN], D[MAXN], C[MAXN];//D[i]=a[i]-a[i-1], c[i]= (i-1) *d[i]8 intN;9 Ten intLowbit (intx) { One returnX &-x; A } - - voidAdd (ll *tree,intx, ll AD) { the while(x <=N) { -TREE[X] + =AD; -X + =lowbit (x); - } + } - +ll sum (ll *tree,intx) { All ans =0; at while(X >0){ -Ans + =Tree[x]; -X-=lowbit (x); - } - returnans; - } in - intMainvoid){ to ll AD; + intq, OP, L, R; -scanf"%d", &n); the for(inti =1; I <= N; i++){ *scanf"%lld", &a[i]); $Add (d, I, a[i]-a[i-1]);Panax NotoginsengAdd (c, I, (i-1) * (A[i]-a[i-1])); - } thescanf"%d", &q); + while(q--){ Ascanf"%d", &op); the if(OP = =1){ +scanf"%d%d%lld", &l, &r, &AD); - Add (D, L, AD); $Add (d, R +1, -AD); $Add (C, L, (L-1) *AD); -Add (c, R +1,-ad *R); -}Else{ thescanf"%d%d", &l, &R); -ll sum1 = (L-1) * SUM (d, L-1)-sum (c, L-1);Wuyill sum2 = R * SUM (d, R)-sum (c, r); theprintf"%lld\n", Sum2-sum1); - } Wu } - return 0; About}
View Code
Tree-shaped array template (Pity Dorado/Segment/change segment)