区间修改,整个区间的和。也就是sum[1],用线段树维护区间和,区间修改lazy标记。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = 111111;
using namespace std;
int sum[maxn<<2],lazy[maxn<<2];
void pushup(int rt)
{
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void build(int l , int r,int rt)
{
lazy[rt] = 0;
if(l == r)
{
sum[rt] = 1;
return ;
}
int m = (l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void pushdown(int rt ,int m)
{
if(lazy[rt])
{
lazy[rt<<1] = lazy[rt<<1|1] = lazy[rt];
sum[rt<<1] = (m-(m>>1))*lazy[rt];
sum[rt<<1|1] = (m>>1)*lazy[rt];
lazy[rt] = 0;
}
}
void update(int L,int R ,int c,int l ,int r,int rt)
{
if(L <= l && r <= R)
{
lazy[rt] = c;
sum[rt] = c*(r-l+1);
return ;
}
pushdown(rt,(r-l+1));
int m = (l+r)>> 1;
if(L <= m) update(L,R,c,lson);
if(R > m) update(L,R,c,rson);
pushup(rt);
}
int main()
{
int t;
scanf("%d",&t);
for(int cas = 1 ; cas <= t ; cas++)
{
int n , q ;
scanf("%d%d",&n,&q);
build(1,n,1);
for(int i = 0 ; i < q ; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
update(a,b,c,1,n,1);
}
printf("Case %d: The total value of the hook is %d.\n",cas,sum[1]);
}
return 0;
}