HDU-1698 Just a Hook
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int N = 100005 , INF = 0x3f3f3f3f ;
int tree[ N << 2 ] , lazy[ N << 2 ] ;
#define ls(rt) (rt << 1)
#define rs(rt) (rt << 1 | 1)
void push_up ( int root) { tree[ root] = tree[ ls ( root) ] + tree[ rs ( root) ] ; }
void push_down ( int root, int len) {
if ( lazy[ root] ) {
lazy[ ls ( root) ] = lazy[ root] ;
lazy[ rs ( root) ] = lazy[ root] ;
tree[ ls ( root) ] = lazy[ root] * ( len - ( len >> 1 ) ) ;
tree[ rs ( root) ] = lazy[ root] * ( len >> 1 ) ;
lazy[ root] = 0 ;
}
}
void build_tree ( int l, int r, int root) {
lazy[ root] = 0 ;
if ( l == r) {
tree[ root] = 1 ;
return ;
}
int mid = ( l + r) >> 1 ;
build_tree ( l, mid, ls ( root) ) ;
build_tree ( mid + 1 , r, rs ( root) ) ;
push_up ( root) ;
}
void update ( int l, int r, int root, int L, int R, int val) {
if ( L <= l && r <= R) {
tree[ root] = ( r - l + 1 ) * val;
lazy[ root] = val;
return ;
}
push_down ( root, r - l + 1 ) ;
int mid = ( l + r) >> 1 ;
if ( L <= mid) update ( l, mid, ls ( root) , L, R, val) ;
if ( mid < R) update ( mid + 1 , r, rs ( root) , L, R, val) ;
push_up ( root) ;
}
int main ( ) {
int T;
scanf ( "%d" , & T) ;
for ( int Case = 1 ; Case <= T; Case++ ) {
int n, q;
scanf ( "%d%d" , & n, & q) ;
build_tree ( 1 , n, 1 ) ;
for ( int i = 0 ; i < q; ++ i) {
int x, y, z;
scanf ( "%d%d%d" , & x, & y, & z) ;
update ( 1 , n, 1 , x, y, z) ;
}
printf ( "Case %d: The total value of the hook is %d.\n" , Case, tree[ 1 ] ) ;
}
return 0 ;
}