懒惰标记
#include <cstdio>
struct node
{
int l,r,sum,lazy;
};
node segTree[400004];
void Build(int t, int l, int r)
{
segTree[t].l = l;
segTree[t].r = r;
segTree[t].lazy = 0;
if(l == r)
{
segTree[t].sum = 1;
return ;
}
int mid = (l+r) >> 1;
Build(t<<1,l,mid);
Build(t<<1|1,mid+1,r);
segTree[t].sum = segTree[t<<1].sum + segTree[t<<1|1].sum;
}
void pushDown(int t)
{
if(segTree[t].lazy)
{
segTree[t<<1].sum = (segTree[t<<1].r - segTree[t<<1].l + 1) * segTree[t].lazy;
segTree[t<<1].lazy = segTree[t].lazy;
segTree[t<<1|1].sum = (segTree[t<<1|1].r - segTree[t<<1|1].l + 1)*segTree[t].lazy;
segTree[t<<1|1].lazy = segTree[t].lazy;
segTree[t].lazy = 0;
}
}
void Update(int t, int l, int r, int c)
{
if(segTree[t].l >= l && segTree[t].r <= r)
{
segTree[t].sum = (segTree[t].r - segTree[t].l + 1) * c;
segTree[t].lazy = c;
return ;
}
pushDown(t);
if(r >= segTree[t<<1|1].l)
Update(t<<1|1,l,r,c);
if(l <= segTree[t<<1].r)
Update(t<<1,l,r,c);
segTree[t].sum = segTree[t<<1].sum + segTree[t<<1|1].sum;
}
int main()
{
int t,n,o;
int time = 0;
scanf("%d",&t);
while(t--)
{
++time;
scanf("%d",&n);
Build(1,1,n);
scanf("%d",&o);
int a,b,c;
for(int i = 0; i < o; ++i)
{
scanf("%d %d %d",&a,&b,&c);
Update(1,a,b,c);
}
printf("Case %d: The total value of the hook is %d.\n",time,segTree[1].sum);
}
return 0;
}