Here are two operations for a series.
"C
A
B
C "Means adding
C To each
AA ,
AA + 1 ,...,
AB .-10000 ≤
C ≤ 10000. Add a value for a Range
"Q
A
B "Means querying the sum
AA ,
AA + 1 ,...,
AB . The question "query the value of a range" is a good example of lazy_tag. When the range to be added overwrites the current range, it is marked and returned. When you query the subrange next time, directly mark down. As you can imagine, this mark represents the mark of the entire interval represented by this interval, which is the nature of the entire subtree. When you do not need the son interval of this interval, you don't need to finish all the operations, but need to use them before uploading them! For details, see Code
# Include < Stdio. h >
# Include < Stdlib. h >
# Define L (t) <1)
# Define R (t) <1 | 1)
# Define Ll long
# Define Maxn 100000
Struct Segtree {
Int L, R;
Ll add, sum;
Int Getmid (){
Return (L + R) > 1 ;
}
Int Getdis (){
Return R - L + 1 ;
}
} Tree [maxn < 2 ];
Int Val [ 100010 ];
Void Bulid ( Int Left, Int Right, Int T ){
Tree [T]. L = Left;
Tree [T]. r = Right;
Tree [T]. Add = 0 ;
If (Left = Right ){
Tree [T]. Sum = Val [left];
Return ;
}
Int Mid = Tree [T]. getmid ();
Bulid (left, mid, L (t ));
Bulid (Mid + 1 , Right, r (t ));
Tree [T]. Sum = Tree [L (t)]. Sum + Tree [r (t)]. sum;
}
Void Update ( Int Left, Int Right, Int A, Int T ){
If (Left <= Tree [T]. L && Right > = Tree [T]. R ){
Tree [T]. Add + = A; // Overwrite this interval Add add and update sum. Then add indicates the increment to be added to the subtree.
Tree [T]. Sum + = A * Tree [T]. getdis ();
Return ;
}
If (Tree [T]. Add ){ // If the increment is not empty, pass it down, update the son's sum value, and finally clear the increment.
Tree [L (t)]. Sum + = Tree [L (t)]. getdis () * Tree [T]. Add;
Tree [r (t)]. Sum + = Tree [r (t)]. getdis () * Tree [T]. Add;
Tree [L (t)]. Add + = Tree [T]. Add;
Tree [r (t)]. Add + = Tree [T]. Add;
Tree [T]. Add = 0 ;
}
Int Mid = Tree [T]. getmid ();
If (Right <= Mid ){
Update (left, right, A, L (t ));
} Else If (Left > Mid ){
Update (left, right, A, r (t ));
} Else {
Update (left, mid, A, L (t ));
Update (Mid + 1 , Right, A, r (t ));
}
Tree [T]. Sum = Tree [L (t)]. Sum + Tree [r (t)]. sum; // After recursion, the sum value of the left and right children updates the sum value of the father.
}
Ll query ( Int Left, Int Right, Int T ){
If (Left <= Tree [T]. L && Right > = Tree [T]. R ){
Return Tree [T]. sum;
}
If (Tree [T]. Add ){ // Likewise
Tree [L (t)]. Sum + = Tree [L (t)]. getdis () * Tree [T]. Add;
Tree [r (t)]. Sum + = Tree [r (t)]. getdis () * Tree [T]. Add;
Tree [L (t)]. Add + = Tree [T]. Add;
Tree [r (t)]. Add + = Tree [T]. Add;
Tree [T]. Add = 0 ;
}
Int Mid = Tree [T]. getmid ();
If (Right <= Mid ){
Return Query (left, right, L (t ));
} Else If (Left > Mid ){
Return Query (left, right, r (t ));
} Else {
Return Query (left, mid, L (t )) +
Query (Mid + 1 , Right, r (t ));
}
}
Int Main (){
Char CMD [ 3 ];
Int N, Q;
Scanf ( " % D " , & N, & Q );
For ( Int I = 1 ; I <= N; ++ I ){
Scanf ( " % D " , & Val [I]);
}
Bulid ( 1 , N, 1 ); // Don't forget it every time!
For ( Int I = 1 ; I <= Q; ++ I ){
Scanf ( " % S " , CMD );
If (CMD [ 0 ] = ' Q ' ){
Int A, B;
Scanf ( " % D " , & A, & B );
Ll ans = Query (A, B, 1 );
Printf ( " % LLD \ n " , ANS );
} Else {
Int A, B, C;
Scanf ( " % D " , & A, & B, & C );
Update (a, B, c, 1 );
}
}
Return 0 ;
}