TopcoderArchive
Archive
Algorithm problem

WolfCarpet

Problem statement, definition, constraints, and public examples.

Problem Statement

Cat Snuke has a carpet of infinite size. The carpet is split into square cells. The top left cell has coordinates (0,0), and the cell in row i and column j has coordinates (i,j).

If either i or j is even, cell (i,j) is painted black; otherwise, it's painted white.

Wolf Sothe will buy a rectangular section of Snuke's carpet. The top left cell of this section is (r1,c1) and its bottom right cell is (r2,c2).

You're given ints r1, c1, r2 and c2. Compute and return the number of black cells in the rectangle that Sothe will buy.

Definition

Class:
WolfCarpet
Method:
count
Parameters:
int, int, int, int
Returns:
long
Method signature:
long count(int r1, int c1, int r2, int c2)
(be sure your method is public)

Constraints

  • r1 will be between 0 and 1,000,000,000, inclusive.
  • c1 will be between 0 and 1,000,000,000, inclusive.
  • r2 will be between r1 and 1,000,000,000, inclusive.
  • c2 will be between c1 and 1,000,000,000, inclusive.

Examples

  1. 1
    1
    2
    3
    Returns: 4
    Cells (1,2), (2,1), (2,2) and (2,3) are painted black.
  2. 0
    0
    0
    10
    Returns: 11
    All cells in the rectangle are located in even rows, so they are all painted black.
  3. 1
    1
    1
    1
    Returns: 0
    Cell (1,1) is white, so no cells in the rectangle are painted black.
  4. 5
    12
    40
    26
    Returns: 414
  5. 0
    0
    1000000000
    1000000000
    Returns: 750000002000000001
  6. 81695509
    209408464
    949118329
    220137366
    Returns: 6979871694045002
Back to all problems