To store the number of variables with ith bit set
if ( x1 == 0 and y1 == 0 ) : NEW_LINE INDENT p = prefix_count [ i ] [ x2 ] [ y2 ] NEW_LINE DEDENT elif ( x1 == 0 ) : NEW_LINE INDENT p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x2 ] [ y1 - 1 ] NEW_LINE DEDENT elif ( y1 == 0 ) : NEW_LINE INDENT p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x1 - 1 ] [ y2 ] NEW_LINE DEDENT else : NEW_LINE INDENT p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x1 - 1 ] [ y2 ] - prefix_count [ i ] [ x2 ] [ y1 - 1 ] + prefix_count [ i ] [ x1 - 1 ] [ y1 - 1 ] ; NEW_LINE DEDENT