模板题,区间更新,把l到r区间的每个数都换成z
#include<stdio.h>
using namespace std;
const int maxn=100005;
struct node
{
int l,r;
int lazy,tag;
int sum;
}segtree[maxn*4];
void pushdown(int i)
{
if(segtree[i].lazy!=0)
{
segtree[(i<<1)].lazy=1;
segtree[(i<<1)].tag=segtree[i].tag;
segtree[(i<<1)].sum=(segtree[(i<<1)].r-segtree[(i<<1)].l+1)*segtree[i].tag;
segtree[(i<<1)|1].lazy=1;
segtree[(i<<1)|1].tag=segtree[i].tag;
segtree[(i<<1)|1].sum=(segtree[(i<<1)|1].r-segtree[(i<<1)|1].l+1)*segtree[i].tag;
segtree[i].lazy=0;
segtree[i].tag=0;
}
}
void pushup(int i)
{
segtree[i].sum=segtree[(i<<1)].sum+segtree[(i<<1)|1].sum;
}
void build(int i,int l,int r)
{
segtree[i].l=l;
segtree[i].r=r;
segtree[i].lazy=0;
segtree[i].tag=0;
if(l==r)
{
segtree[i].sum=1;
return ;
}
int mid=(l+r)>>1;
build((i<<1),l,mid);
build((i<<1)|1,mid+1,r);
pushup(i);
}
void update(int i,int l,int r,int v)
{
if((segtree[i].l==l)&&(segtree[i].r==r))//³É¶Î¸üÐÂ
{
segtree[i].lazy=1;
segtree[i].tag=v;
segtree[i].sum=(r-l+1)*v;
return ;
}
int mid=(segtree[i].l+segtree[i].r)>>1;
pushdown(i);
if(r<=mid)
update((i<<1),l,r,v);
else
if(l>mid)
update((i<<1)|1,l,r,v);
else
{
update((i<<1),l,mid,v);
update((i<<1)|1,mid+1,r,v);
}
pushup(i);
}
int main()
{
int x,y,z;
int n,m,t;
scanf("%d",&t);
int mycase=0;
while(t--)
{
mycase++;
scanf("%d%d",&n,&m);
build(1,1,n);
while(m--)
{
scanf("%d%d%d",&x,&y,&z);
update(1,x,y,z);
}
printf("Case %d: The total value of the hook is %d.
",mycase,segtree[1].sum);
}
return 0;
}