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.
哥 5 5 5 5 Every day to sing a continuous flower every day, and then the pleasure of these flowers will increase one of the same value v ( v may be negative).
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 line, No. 1 i Line indicates that Brother Qiu Shi completed the first
After i Tian’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
pushup back to the renewal SUM array
;
#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;
If (add [rt])
{{
Add [rt << 1] += add [rt];
Add [rt << 1 | 1] += ADD [RT];
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)
{{
Add [rt] += c;
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;
}