Flipping Sign Problem | Lazy Propagation Segment Tree
Given an array of size N. There can be multiple queries of the following types.
- 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.
- 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 3Input : 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:
Approach :
Create a segment tree where every node store the sum of its left and right child unless it is a leaf node where the array is stored.
For update operation:
Create a tree named lazy of size same as the 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 needs 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 :
C++14
// 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 to 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; } |
Java
// Java implementation of the approach class GFG { static final int MAX = 15 ; static int tree[] = new int [MAX]; static boolean lazy[] = new boolean [MAX]; // Function to build the segment tree static 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 ]; } static 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] != false ) { // 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] = false ; } // 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 ]; } static 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] != false ) { 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] = false ; } // 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; } // Driver code public static void main(String[] args) { int arr[]={ 1 , 2 , 3 , 4 , 5 }; int n=arr.length; // Building segment tree build(arr, 1 , 0 , n - 1 ); // Array is { 1, 2, 3, 4, 5 } System.out.print(query_tree( 1 , 0 , n - 1 , 0 , 4 ) + "\n" ); // Flip range 0 to 4 update_tree( 1 , 0 , n - 1 , 0 , 4 ); // Array becomes { -1, -2, -3, -4, -5 } System.out.print(query_tree( 1 , 0 , n - 1 , 0 , 4 ) + "\n" ); // Flip range 0 t0 2 update_tree( 1 , 0 , n - 1 , 0 , 2 ); // Array becomes { 1, 2, 3, -4, -5 } System.out.print(query_tree( 1 , 0 , n - 1 , 0 , 4 ) + "\n" ); } } // This code contributed by Rajput-Ji |
Python3
# Python3 implementation of the approach MAX = 15 tree = [ 0 ] * MAX lazy = [ 0 ] * MAX # Function to build the segment tree def build(arr,node, a, 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 ] def update_tree(node, a,b, i, 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 ] = not (lazy[node * 2 ]) # flip value in lazy lazy[node * 2 + 1 ] = not (lazy[node * 2 + 1 ]) # Reset the node lazy value lazy[node] = 0 # Current segment is not # within range [i, j] if (a > b or a > j or b < i): return # Segment is fully # within range if (a > = i and 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 ] = not (lazy[node * 2 ]) lazy[node * 2 + 1 ] = not (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 ] def query_tree(node, a,b, i, j): # Out of range if (a > b or a > j or 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 ] = not (lazy[node * 2 ]) # flip value in lazy lazy[node * 2 + 1 ] = not (lazy[node * 2 + 1 ]) lazy[node] = 0 # Current segment is totally # within range [i, j] if (a > = i and b < = j): return tree[node] # Query left child q1 = query_tree(node * 2 , a,(a + b) / / 2 , i, j) # Query right child q2 = query_tree( 1 + node * 2 , 1 + (a + b) / / 2 , b, i, j) # Return final result return q1 + q2 # Driver code if __name__ = = '__main__' : arr = [ 1 , 2 , 3 , 4 , 5 ] n = len (arr) # Building segment tree build(arr, 1 , 0 , n - 1 ) # Array is { 1, 2, 3, 4, 5 print (query_tree( 1 , 0 , n - 1 , 0 , 4 )) # Flip range 0 to 4 update_tree( 1 , 0 , n - 1 , 0 , 4 ) # Array becomes { -1, -2, -3, -4, -5 print (query_tree( 1 , 0 , n - 1 , 0 , 4 )) # Flip range 0 t0 2 update_tree( 1 , 0 , n - 1 , 0 , 2 ) # Array becomes { 1, 2, 3, -4, -5 print (query_tree( 1 , 0 , n - 1 , 0 , 4 )) # This code is contributed by mohit kumar 29 |
C#
// C# implementation of the approach using System; class GFG { static readonly int MAX = 15; static int []tree = new int [MAX]; static bool []lazy = new bool [MAX]; // Function to build the segment tree static 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]; } static 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] != false ) { // 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] = false ; } // 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]; } static 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] != false ) { 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] = false ; } // 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 readonly result return q1 + q2; } // Driver code public static void Main(String[] args) { int []arr = {1, 2, 3, 4, 5}; int n = arr.Length; // Building segment tree build(arr, 1, 0, n - 1); // Array is { 1, 2, 3, 4, 5 } Console.Write(query_tree(1, 0, n - 1, 0, 4) + "\n" ); // Flip range 0 to 4 update_tree(1, 0, n - 1, 0, 4); // Array becomes { -1, -2, -3, -4, -5 } Console.Write(query_tree(1, 0, n - 1, 0, 4) + "\n" ); // Flip range 0 t0 2 update_tree(1, 0, n - 1, 0, 2); // Array becomes { 1, 2, 3, -4, -5 } Console.Write(query_tree(1, 0, n - 1, 0, 4) + "\n" ); } } // This code is contributed by Rajput-Ji |
Javascript
<script> // JavaScript implementation of the approach let MAX = 15; let tree = new Array(MAX).fill(0); let lazy = new Array(MAX).fill(0); // Function to build the segment tree function build(arr,node, a, b) { if (a == b) { tree[node] = arr[a]; return ; } // left child build(arr,2 * node, a, Math.floor((a + b) / 2)); // right child build(arr,2 * node + 1, 1 + Math.floor((a + b) / 2), b); tree[node] = tree[node * 2] + tree[node * 2 + 1]; } function update_tree(node, a, b, i, 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, Math.floor((a + b) / 2), i, j); // Updating right child update_tree(1 + node * 2, 1 + Math.floor((a + b) / 2), b, i, j); // Updating root with // sum of its child tree[node] = tree[node * 2] + tree[node * 2 + 1]; } function query_tree(node, a, b, i, 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 let q1 = query_tree(node * 2, a, Math.floor((a + b) / 2), i, j); // Query right child let q2 = query_tree(1 + node * 2, 1 + Math.floor((a + b) / 2), b, i, j); // Return final result return q1 + q2; } let arr = [ 1,2,3,4,5]; let n = arr.length; // Building segment tree build(arr,1, 0, n - 1); // Array is { 1, 2, 3, 4, 5 } document.write(query_tree(1, 0, n - 1, 0, 4) + "<br>" ); // Flip range 0 to 4 update_tree(1, 0, n - 1, 0, 4); // Array becomes { -1, -2, -3, -4, -5 } document.write(query_tree(1, 0, n - 1, 0, 4) + "<br>" ); // Flip range 0 t0 2 update_tree(1, 0, n - 1, 0, 2); // Array becomes { 1, 2, 3, -4, -5 } document.write(query_tree(1, 0, n - 1, 0, 4) + "<br>" ); // This code is contributed by gfgking </script> |
15 -15 -3
Time Complexity : O(log(N))
Auxiliary Space: O(log(N)), due to recursive call stacks.
Related Topic: Segment Tree