Flipping Sign Problem | Lazy Propagation Segment Tree

Given an array of size N.There can be multiple queries of the following types.

  1. update(l, r) : On update, flip( multiply a[i] by -1) the value of a[i] where l <= i <= r . In simple terms, change the sign of a[i] for the given range.
  2. query(l, r): On query ,print the sum of the array in given range inclusive of both l and r .

Examples :

Input : arr[] = { 1, 2, 3, 4, 5 }
update(0, 2)
query(0, 4)
Output: 3
After applying update operation array becomes { -1, -2, -3, 4, 5 } .
So the sum is 3

Input : arr[] = { 1, 2, 3, 4, 5 }
update(0, 4)
query(0, 4)
Output: -15
After applying update operation array becomes { -1, -2, -3, -4, -5 } .
So the sum is -15

Prerequisites:

  1. Segment tree
  2. Lazy propagation in segment tree

Approach :
Create a segment tree where every node store the sum of its left and right child until unless it is a leaf node where the array is stored.

For update operation:
Create a tree named lazy of size same as above segment tree where tree store the sum of its child and lazy stores whether they have been asked to be flipped or not. If lazy is set to 1 for a range then all value under that range needs to be flipped. For update following operation are used –

  • If current segment tree has lazy set to 1 then update current segment tree node by changing the sign as value need to be flipped and also flip the value of lazy of its child and reset its own lazy to 0.
  • If current node’s range lies completely in update query range then update the current node by changing its sign and also flip value of lazy of its child if not leaf node.
  • If current node’s range overlaps with update range then do recursion for its children and update the current node using the sum of its children .

For query operation:
If lazy is set to 1 then change the sign of the current node and reset current node lazy to 0 and also flip the value of lazy of its child if not leaf node. And then do simple query as done in segment tree .

Below is the implementation of the above approach :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
#define MAX 15
   
int tree[MAX] = { 0 };
int lazy[MAX] = { 0 };
   
// Function to build the segment tree
void build(int arr[],int node, int a, int b)
{
    if (a == b) {
        tree[node] = arr[a];
        return;
    }
   
    // left child
    build(arr,2 * node, a, (a + b) / 2); 
  
    // right child
    build(arr,2 * node + 1, 1 + (a + b) / 2, b); 
   
    tree[node] = tree[node * 2] + 
                         tree[node * 2 + 1];
}
   
void update_tree(int node, int a, 
                int b, int i, int j)
{
   
    // If lazy of node is 1 then 
    // value in current node 
    // needs to be flipped
    if (lazy[node] != 0) {
  
        // Update it
        tree[node] = tree[node] * (-1); 
   
        if (a != b) {
  
            // flip value in lazy
            lazy[node * 2] =
                        !(lazy[node * 2]); 
  
             // flip value in lazy
            lazy[node * 2 + 1] = 
                    !(lazy[node * 2 + 1]);
        }
    
        // Reset the node lazy value
        lazy[node] = 0; 
    }
   
    // Current segment is not
    // within range [i, j]
    if (a > b || a > j || b < i)
        return;
   
    // Segment is fully
    // within range
    if (a >= i && b <= j) {
        tree[node] = tree[node] * (-1);
   
        // Not leaf node
        if (a != b) {
  
            // Flip the value as if lazy is 
            // 1 and again asked to flip 
            // value then without flipping 
            // value two times
            lazy[node * 2] = 
                         !(lazy[node * 2]);
            lazy[node * 2 + 1] = 
                    !(lazy[node * 2 + 1]);
        }
   
        return;
    }
   
    // Updating left child
    update_tree(node * 2, a,
                        (a + b) / 2, i, j);
  
    // Updating right child
    update_tree(1 + node * 2, 1 + 
                     (a + b) / 2, b, i, j); 
  
    // Updating root with 
    // sum of its child
    tree[node] = tree[node * 2] +
                     tree[node * 2 + 1];
}
   
int query_tree(int node, int a, 
                      int b, int i, int j)
{
    // Out of range 
    if (a > b || a > j || b < i)
        return 0; 
   
    // If lazy of node is 1 then value
    // in current node needs to be flipped
    if (lazy[node] != 0) {
  
     
        tree[node] = tree[node] * (-1); 
        if (a != b) {
            lazy[node * 2] = 
                        !(lazy[node * 2]); 
  
            // flip value in lazy
            lazy[node * 2 + 1] = 
                    !(lazy[node * 2 + 1]); 
        }
   
        lazy[node] = 0;
    }
   
    // Current segment is totally 
    // within range [i, j]
    if (a >= i && b <= j)
        return tree[node];
    
    // Query left child
    int q1 = query_tree(node * 2, a,
                        (a + b) / 2, i, j); 
  
    // Query right child
    int q2 = query_tree(1 + node * 2, 
                 1 + (a + b) / 2, b, i, j); 
   
    // Return final result
    return q1 + q2;
}
   
int main()
{
  
    int arr[]={1,2,3,4,5};
  
    int n=sizeof(arr)/sizeof(arr[0]);
  
    // Building segment tree
    build(arr,1, 0, n - 1);
  
    // Array is { 1, 2, 3, 4, 5 }
    cout << query_tree(1, 0, n - 1, 0, 4) << endl;
   
    // Flip range 0 to 4
    update_tree(1, 0, n - 1, 0, 4);
  
    // Array becomes { -1, -2, -3, -4, -5 }
    cout << query_tree(1, 0, n - 1, 0, 4) << endl;
   
    // Flip range 0 t0 2
    update_tree(1, 0, n - 1, 0, 2);
  
    // Array becomes { 1, 2, 3, -4, -5 }
    cout << query_tree(1, 0, n - 1, 0, 4) << endl;
  
}

chevron_right


Output:

15
-15
-3

Time Complexity : O(log(N))



My Personal Notes arrow_drop_up

A fallen star which will rise again

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.