Initialize array for Prefix sum
int pre [ N ] = { 0 } ; pre [ 0 ] = check ( arr [ 0 ] ) ; for ( int i = 1 ; i < N ; i ++ ) { pre [ i ] = pre [ i - 1 ] + check ( arr [ i ] ) ; } int c = 0 ; while ( Q -- ) { int l = queries . first ; int r = queries . second ; c ++ ; cout << query ( l , r , pre ) << ' ▁ ' ; } }