Can anyone explain this range update function of segment tree? It’s faster from normal range update but I can’t understand how it is using lazy propagation.
The question was to increment all values from ind1 to ind2 by 1.
void update(int st,int en,int ind1,int ind2,int node)
{
if(st>en)return;
if((st>ind2)||(en<ind1))return;
if((st==ind1)&&(en==ind2))
{
segNode[node]++;
return;
}
int mid=(st+en)/2;
if(ind2<=mid)update(st,mid,ind1,ind2,node*2);
else if(ind1>mid)update(mid+1,en,ind1,ind2,(node*2)+1);
else
{
update(st,mid,ind1,mid,node*2);
update(mid+1,en,mid+1,ind2,(node*2)+1);
}
return;
}