1 10 2 1 5 2 5 9 3
Case 1: The total value of the hook is 24.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#define N 100010
#define Mod 10000007
#define lson l,mid,idx<<1
#define rson mid+1,r,idx<<1|1
typedef long long ll;
const int INF = 1000010;
using namespace std;
int add[N << 2], sum[N << 2];
void pushdown ( int idx, int m )
{
if ( add[idx] )
{
add[idx << 1] = add[idx];
add[idx << 1 | 1] = add[idx];
sum[idx << 1] = ( m - ( m >> 1 ) ) * add[idx];
sum[idx << 1 | 1] = ( m >> 1 ) * add[idx];
add[idx] = 0;
}
}
void build ( int l, int r, int idx )
{
add[idx] = 0;
if ( l == r )
{
sum[idx] = 1;///初始化为1
return;
}
int mid = ( l + r ) >> 1;
build ( lson );
build ( rson );
sum[idx] = sum[idx << 1] + sum[idx << 1 | 1];
}
void update ( int l, int r, int idx, int x, int y, int k )
{
if ( x <= l && r <= y )
{
sum[idx] = ( r - l + 1 ) * k;
add[idx] = k;
return;
}
pushdown ( idx, r - l + 1 );
int mid = ( l + r ) >> 1;
if ( x <= mid )
update ( lson, x, y, k );
if ( y > mid )
update ( rson, x, y, k );
sum[idx] = sum[idx << 1] + sum[idx << 1 | 1];
}
int main()
{
int t, n, m;
while ( ~scanf ( "%d", &t ) )
{
for ( int i = 1; i <= t; i++ )
{
scanf ( "%d%d", &n, &m );
build ( 1, n, 1 );
int x, y, z;
while ( m-- )
{
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", i, sum[1] );
}
}
return 0;
}
hdu 1698 Just a Hook(线段树,成段更新,懒惰标记)
原文:http://blog.csdn.net/acm_baihuzi/article/details/41554519