0

Can I perform range updates in BIT with a condition. Lets say I have an array of negative and positive frequencies A[] = {1, -3, -4, 5, 9}. And I wanted to range update the values of the array with a condition : If the update value(x) is negative, update only negative elements and if the update value is positive, update only positive values in the range.

Example, in the above array, if the update query is 2 4 -2, (left right value), then update only 2nd(-3) and 3rd(-4) position. Leave 4th(5) position because its a positive integer.

Or should I use another data structure to accomplish this?

I used this to learn range updations.

RAGHU RAMAN
  • 537
  • 4
  • 16

1 Answers1

0

Yes,it is possible. here's a code for an update function,it changes values in the segment [i,j] to 'newval' if newval is less than any entry.

void update_tree(long long node,long long a,long long b,long long i,long long j,long long newval)
{
    if(a>b||a>j||b<i)
       return;

    if(a==b)
    {
        if(newval<tree[node])
        {
           tree[node]=newval;
        }
        return;
}
    update_tree(2*node,a,(a+b)/2,i,j,newval);
    update_tree(2*node+1,(a+b)/2+1,b,i,j,newval);
    tree[node]=min(tree[2*node],tree[2*node+1]);
}
ashiwn
  • 25
  • 8