For each query calculate maxx for the answer and store it in ans array .
for ( int i = 0 ; i <= q ; ++ i ) { while ( arr [ curr ] . val <= queries [ i ] . x and curr < n ) { update ( Fenwick , arr [ curr ] . index + 1 , 1 , n ) ; curr ++ ; } ans [ queries [ i ] . index ] = query ( Fenwick , queries [ i ] . r + 1 , n ) - query ( Fenwick , queries [ i ] . l , n ) ; } for ( int i = 0 ; i <= q ; ++ i ) { mx = max ( mx , ans [ i ] ) ; }