# .NET platform use Redis (5) [Stackexchange.redis and Protobuf-Net]

2023-01-22   ES

# Big Brother and Flower

##### Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)

Qiu Shi is a elegant person.

So Brother Qiu Shi took care of many flowers. Now all the flowers are rowed into a line, and each flower has a pleasure value.

At the same time, he wanted to know how much the pleasure value of this continuous flower after he sang the song.

## Input

The first line has an integer n, indicating the total number of flowers.
The second line contains n intersection AI, which means the initial pleasure value of the i -i flower.
The third line contains an integer M, indicating that Brother Qiu Shi sang M Tian’s song.
Next, each row contains three integer L R V, which means that the elder brother Qiu Shi sang in the flower in the interval of [L, R], and the pleasant value of each flower increased V.
1≤n，m，ai，|v|≤100000，1≤l≤r≤n。

Output

output together m mline, No. 1 i iLine indicates that Brother Qiu Shi completed the first

After i iTian’s singing, the joy of the flowers of the flowers.

## Sample input and output

Sample Input Sample Output
3
0 0 0
3
1 2 1
1 2 -1
1 3 1
2
0
3

## Source

meaning:
Thinking: interval update, sequential interval and;
Open a SUM array to record the sum of the corresponding range of each node;
Open an array as a role as lazy_tag; then what is lazy_tag?
1. When building a tree, it will be updated to complete the SUM array —
pushup back to the renewal SUM array
2. For example, C (2,5,2), from node 1, you have always found nodes with a complete range, and then add lazy_tag; when you encounter a node with lazy_tag along the road, Lazy_tag will be push_dow along the way;
until the end of the update; (lazy_tag is a general name)
3. Then Q (1,4), the C operation of the previous step will make the interval (2,2) (3,3) (4,5) there are lazy_tag marks, and this time Q (1,4) query Where, the interval (1,3), (4,4); (4,4) will pass (4,5), and (4,5)
Lazy_tag pushdown;
// Be sure to pay attention to the retrospective of pushup!
code:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

TypeDef long long ll;
const int n = 100010;

ll sum [n << 2]; // The interval of the node i;
ll add [n << 2]; // lazy_tag;

Struct Node
{{
Int L, R;
Int Mid ()
{{
Return (L+R) >> 1;
}
} Tree [n << 2];

void pushup (int RT) // Update SUM array
{{
Sum [rt] = sum [rt << 1]+Sum [rt << 1 | 1];
}

void pushdown (int RT, Int len) // Update the ADD array and the SUM array
{{
// Give the length of the parent interval, and get the length of the two sub -sections;
{{
SUM [RT << 1] += add [RT]*(Len- (Len >> 1)); // The length of the parent interval [1,3] is 3; The length of the right sub -section is 3/2;
Sum [rt << 1 | 1] += ADD [RT]*(Len >> 1);
Add [rt] = 0; // lazy_tag to pass, and it becomes 0;
}
}

void Buildtree (int L, int R, int RT)
{{
Tree [rt] .l = l;
Tree [rt] .r = r;
Add [rt] = 0, sum [rt] = 0;
If (l == r)
{{
Scanf ("%i64D", & SUM [RT]);
Return;
}

Int m = tree [rt] .mid ();
Buildtree (L, M, RT << 1);
Buildtree (m+1, r, rt << 1 | 1);
Pushup (rt); // retrospect, get the interval and harmony of the node RT;
}

void updateTree (int L, int R, int C, int RT)
{{
If (tree [rt] .l == l && tree [rt] .r == r)
{{
Sum [rt] += (ll) C*(R-L +1);
Return; // The interval is completely closed, and Return;
}
If (tree [rt] .l == tree [rt] .r) return;

Pushdown (RT, Tree [RT] .r-Tree [RT] .l+1); // Give the length of the parent interval, you can get the length of the two sub-interval;
Int m = tree [rt] .mid ();
If (r <= m) updatetree (l, r, c, rt << 1);
Else if (l> m) updatetree (L, R, C, RT << 1 | 1);
Else
{{
UpdateTree (L, M, C, RT << 1);
Updatetree (m+1, r, c, rt << 1 | 1);
}
Pushup (rt); // retrospective to get the updated interval value;
}

LL Query (int L, int R, int RT)
{{
If (tree [rt] .l == l && tree [rt] .r == r)
{{
Return Sum [RT];
}
Pushdown (RT, Tree [RT]. RTREE [RT] .l+1);
Int m = tree [rt] .mid ();

// ll res = 0;
If (r <= m) return query (l, r, rt << 1);
Else if (L> M) Return Query (L, R, RT << 1 | 1);
Else Return (query (l, m, rt << 1)+query (m+1, r, rt << 1 | 1));
}

int Main ()
{{
Int n, m;
While (scanf ("%d", & n)! = EOF)
{{
Buildtree (1, n, 1);

Scanf ("%d", & m);
While (m--)
{{
Int a, b, c;
Scanf ("%d%d%d", & a, & b, & c);
UpdateTree (a, B, C, 1);
Printf ("%lld \ n", query (a, b, 1));
}
}
Return 0;
}

source