Find the number of different numbers in the array after applying the given operation q times
Given an array of size N, initially consists of zeroes only. The task is to apply the given operation q times and find the number of different numbers in the array except for zeroes.
Operation Format: update(l, r, x):: update a[i] = x for all (l <= i <= r).
Examples:
Input : N = 5, Q = 3,
update(1, 3, 1)
update(0, 1, 2)
update(3, 3, 3)
Output : 3
Explanation : Initially array is {0, 0, 0, 0, 0}. After
applying the operation for the first time array becomes {0, 1, 1, 1, 0}.
After applying the operation for the second time the array becomes
{2, 2, 1, 1, 0}. After applying the operation for the third time the array
becomes {2, 2, 1, 3, 0}. So, a number of different numbers expect zero are 3.
Input : N = 5, Q = 3,
update(1, 1, 4)
update(0, 1, 2)
update(1, 4, 5)
Output : 2
Approach :
Each operation suggests a range update, hence try to update the array using lazy propagation. After applying the operation Q times using lazy propagation call a function that finds the number of different numbers in the array. This function uses a set to find the count of different numbers.
The update and query operations are similar to what they are in a segment tree with some changes. Whenever an update query gets executed in a segment tree, all the nodes associated with the current node also get updated whereas in lazy propagation those nodes will only get updated when required i.e. we create an array lazy[] of size equal to the given array all of whose elements will be initialized to 0 which means there are no updates for any node initially and any non-zero value at lazy[i] indicates that node i has an update pending which will only be updated while querying (when required).
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
#define N 100005
int lazy[4 * N];
set< int > se;
void update( int x, int y, int value, int id, int l, int r)
{
if (x >= r or l >= y)
return ;
if (x <= l && r <= y) {
lazy[id] = value;
return ;
}
int mid = (l + r) / 2;
if (lazy[id])
lazy[2 * id] = lazy[2 * id + 1] = lazy[id];
lazy[id] = 0;
update(x, y, value, 2 * id, l, mid);
update(x, y, value, 2 * id + 1, mid, r);
}
void query( int id, int l, int r)
{
if (lazy[id]) {
se.insert(lazy[id]);
return ;
}
if (r - l < 2)
return ;
int mid = (l + r) / 2;
query(2 * id, l, mid);
query(2 * id + 1, mid, r);
}
int main()
{
int n = 5, q = 3;
update(1, 4, 1, 1, 0, n);
update(0, 2, 2, 1, 0, n);
update(3, 4, 3, 1, 0, n);
query(1, 0, n);
cout << se.size() << endl;
return 0;
}
|
Java
import java.util.*;
class geeks
{
static int N = 100005 ;
static int [] lazy = new int [ 4 *N];
static Set<Integer> se = new HashSet<Integer>();
public static void update( int x, int y, int value,
int id, int l, int r)
{
if (x >= r || l >= y)
return ;
if (x <= l && r <= y)
{
lazy[id] = value;
return ;
}
int mid = (l + r) / 2 ;
if (lazy[id] != 0 )
lazy[ 2 * id] = lazy[ 2 * id + 1 ] = lazy[id];
lazy[id] = 0 ;
update(x, y, value, 2 * id, l, mid);
update(x, y, value, 2 * id + 1 , mid, r);
}
public static void query( int id, int l, int r)
{
if (lazy[id] != 0 )
{
se.add(lazy[id]);
return ;
}
if (r - l < 2 )
return ;
int mid = (l + r) / 2 ;
query( 2 * id, l, mid);
query( 2 * id + 1 , mid, r);
}
public static void main(String[] args)
{
int n = 5 , q = 3 ;
update( 1 , 4 , 1 , 1 , 0 , n);
update( 0 , 2 , 2 , 1 , 0 , n);
update( 3 , 4 , 3 , 1 , 0 , n);
query( 1 , 0 , n);
System.out.println(se.size());
}
}
|
Python3
N = 100005
lazy = [ 0 ] * ( 4 * N);
se = set ()
def update(x, y, value, id , l, r) :
if (x > = r or l > = y):
return ;
if (x < = l and r < = y) :
lazy[ id ] = value;
return ;
mid = (l + r) / / 2 ;
if (lazy[ id ]) :
lazy[ 2 * id ] = lazy[ 2 * id + 1 ] = lazy[ id ];
lazy[ id ] = 0 ;
update(x, y, value, 2 * id , l, mid);
update(x, y, value, 2 * id + 1 , mid, r);
def query( id , l, r) :
if (lazy[ id ]) :
se.add(lazy[ id ]);
return ;
if (r - l < 2 ) :
return ;
mid = (l + r) / / 2 ;
query( 2 * id , l, mid);
query( 2 * id + 1 , mid, r);
if __name__ = = "__main__" :
n = 5 ; q = 3 ;
update( 1 , 4 , 1 , 1 , 0 , n);
update( 0 , 2 , 2 , 1 , 0 , n);
update( 3 , 4 , 3 , 1 , 0 , n);
query( 1 , 0 , n);
print ( len (se));
|
C#
using System;
using System.Collections.Generic;
public class geeks
{
static int N = 100005;
static int [] lazy = new int [4*N];
static HashSet< int > se = new HashSet< int >();
public static void update( int x, int y, int value,
int id, int l, int r)
{
if (x >= r || l >= y)
return ;
if (x <= l && r <= y)
{
lazy[id] = value;
return ;
}
int mid = (l + r) / 2;
if (lazy[id] != 0)
lazy[2 * id] = lazy[2 * id + 1] = lazy[id];
lazy[id] = 0;
update(x, y, value, 2 * id, l, mid);
update(x, y, value, 2 * id + 1, mid, r);
}
public static void query( int id, int l, int r)
{
if (lazy[id] != 0)
{
se.Add(lazy[id]);
return ;
}
if (r - l < 2)
return ;
int mid = (l + r) / 2;
query(2 * id, l, mid);
query(2 * id + 1, mid, r);
}
public static void Main(String[] args)
{
int n = 5, q = 3;
update(1, 4, 1, 1, 0, n);
update(0, 2, 2, 1, 0, n);
update(3, 4, 3, 1, 0, n);
query(1, 0, n);
Console.WriteLine(se.Count);
}
}
|
Javascript
<script>
var N = 100005;
var lazy = Array(4*N).fill(0);
var se = new Set();
function update(x, y, value, id, l, r)
{
if (x >= r || l >= y)
return ;
if (x <= l && r <= y)
{
lazy[id] = value;
return ;
}
var mid = parseInt((l + r) / 2);
if (lazy[id] != 0)
lazy[2 * id] = lazy[2 * id + 1] = lazy[id];
lazy[id] = 0;
update(x, y, value, 2 * id, l, mid);
update(x, y, value, 2 * id + 1, mid, r);
}
function query(id, l, r)
{
if (lazy[id] != 0)
{
se.add(lazy[id]);
return ;
}
if (r - l < 2)
return ;
var mid = parseInt((l + r) / 2);
query(2 * id, l, mid);
query(2 * id + 1, mid, r);
}
var n = 5, q = 3;
update(1, 4, 1, 1, 0, n);
update(0, 2, 2, 1, 0, n);
update(3, 4, 3, 1, 0, n);
query(1, 0, n);
document.write(se.size);
</script>
|
Time Complexity: O(N*logN), as we are using two recursive calls and in each recursive call, we are decrementing mid by floor division of 2.
Auxiliary Space: O(N), as we are using the implicit extra space for the recursive stack for the recursive calls.
Last Updated :
25 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...