Array range queries over range queries | CPP program to perform range queries over range queries . ; Function to create the record array ; Driver Code ; Build query matrix ; If query is of type 2 then function call to record_sum ; If query is of type 1 then simply add 1 to the record array ; for type 1 queries adding the contains of record array to the main array record array ; printing the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void record_sum ( int record [ ] , int l , int r , int n , int adder ) { for ( int i = l ; i <= r ; i ++ ) record [ i ] += adder ; } int main ( ) { int n = 5 , m = 5 ; int arr [ n ] ; memset ( arr , 0 , sizeof arr ) ; int query [ 5 ] [ 3 ] = { { 1 , 1 , 2 } , { 1 , 4 , 5 } , { 2 , 1 , 2 } , { 2 , 1 , 3 } , { 2 , 3 , 4 } } ; int record [ m ] ; memset ( record , 0 , sizeof record ) ; for ( int i = m - 1 ; i >= 0 ; i -- ) { if ( query [ i ] [ 0 ] == 2 ) record_sum ( record , query [ i ] [ 1 ] - 1 , query [ i ] [ 2 ] - 1 , m , record [ i ] + 1 ) ; else record_sum ( record , i , i , m , 1 ) ; } for ( int i = 0 ; i < m ; i ++ ) { if ( query [ i ] [ 0 ] == 1 ) record_sum ( arr , query [ i ] [ 1 ] - 1 , query [ i ] [ 2 ] - 1 , n , record [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << ' ▁ ' ; return 0 ; }