Queries for count of array elements with values in given range with updates | C ++ code for queries for number of elements that lie in range [ l , r ] ( with updates ) ; Function to set arr [ index ] = x ; Function to get count of elements that lie in range [ l , r ] ; Traverse array ; If element lies in the range [ L , R ] ; Increase count ; Function to solve each query ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void setElement ( int * arr , int n , int index , int x ) { arr [ index ] = x ; } int getCount ( int * arr , int n , int l , int r ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= l && arr [ i ] <= r ) { count ++ ; } } return count ; } void SolveQuery ( int arr [ ] , int n , vector < pair < int , pair < int , int > > > Q ) { int x ; for ( int i = 0 ; i < Q . size ( ) ; i ++ ) { if ( Q [ i ] . first == 1 ) { x = getCount ( arr , n , Q [ i ] . second . first , Q [ i ] . second . second ) ; cout << x << " ▁ " ; } else { setElement ( arr , n , Q [ i ] . second . first , Q [ i ] . second . second ) ; } } } int main ( ) { int arr [ ] = { 1 , 2 , 2 , 3 , 4 , 4 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; vector < pair < int , pair < int , int > > > Q = { { 1 , { 3 , 5 } } , { 1 , { 2 , 4 } } , { 1 , { 1 , 2 } } , { 2 , { 1 , 7 } } , { 1 , { 1 , 2 } } } ; SolveQuery ( arr , n , Q ) ; return 0 ; }