[Cpp]/********************************** * ********** the subject is as follows: Q a B: query the sum of [a, B]; C a B x: update the range [a, B]; add x to all values of the range; algorithm idea: segment update of the Line Segment tree-delayed update; add a delayed node when querying and updating the interval; each time you want to query or update the interval to this interval; the node information is then transmitted to the left and right nodes of the child. This greatly reduces the overhead of Time and Space. algorithm process: Each update does not need to be updated to the leaf node; you only need to update it to the corresponding segment, and then record an add. When you update or query the next time, if you want to find the subnode of the segment, add the add to the subnode, set the add value to 0. In this way, the complexity of the subinterval query is the complexity of the update; **************************************** * ******/# include <iostream> # include <cstring> # include <cstdlib> # include <cstdio> # include <climits> # include <algorithm> using namespace std; # define L l, m, u <1 # define R m + 1, r, u <1 | 1 typedef long LL; const int N = 111111; LL add [N <2]; LL sum [N <2]; void PushUp (int u) // update the information of the current node to the parent node {sum [u] = sum [u <1] + sum [u <1 | 1];} void PushDown (int u, int m) // update the information of the current node to the son node {if (add [u]) {add [u <1] + = add [u]; add [u <1 | 1] + = add [u]; sum [u <1] + = add [u] * (m-(m> 1 )); sum [u <1 | 1] + = add [u] * (m> 1); add [u] = 0 ;}} void build (int l, int r, int u) {add [u] = 0; if (l = r) {scanf ("% lld", & sum [u]); return ;} int m = (l + r)> 1; build (L); build (R); PushUp (u);} void update (int l1, int r1, int c, int l, int r, int u) {if (l1 <= l & r <= r1) {add [u] + = c; sum [u] + = (LL) c * (r-l + 1); return;} PushDown (u, r-l + 1 ); int m = (l + r)> 1; if (l1 <= m) update (l1, r1, c, L); if (m <r1) update (l1, r1, c, R); PushUp (u);} LL query (int l1, int r1, int l, int r, int u) {if (l1 <= l & r <= r1) {return sum [u];} PushDown (u, r-l + 1 ); int m = (l + r)> 1; LL res = 0; if (l1 <= m) res + = query (l1, r1, L ); if (m <r1) res + = query (l1, r1, R); return res;} int main () {// freopen ("C: \ Users \ Administrator \ Desktop \ kd.txt "," r ", stdin); int N, Q; scanf (" % d ", & N, & Q); build (1, N, 1); while (Q --) {char op [2]; int a, B, c; scanf ("% s ", op); if (op [0] = 'q') {scanf ("% d", & a, & B ); printf ("% lld \ n", query (a, B, 1, N, 1);} else {scanf ("% d", &, & B, & c); update (a, B, c, 1, N, 1) ;}} return 0 ;}