First, I need to understand what's being asked: ## Editorial
Let's see how the number of affected cells changes depending on the column. For example 3, 2, 3, 2, 3. That is it alternates between the size of the first column and the size of the first column minus one. The amount of "minus ones" is the amount of columns divided by 2 rounded down. Wi
Key values given: 3,, 2,, 3,, 2,, 3, 2, 2, 1
My approach:
- cells with the "correct" parity have y with parity p as well
- As we scan down a column, valid cells alternate: yes, no, yes, no...
Example: If y ranges from 1 to 5:
- The valid cells in alternating
- column: {1,3,5} → 3 cells
- column: {2,4} → 2 cells
- column: {1,3,5} → 3 cells
Deriving the Formula
5. Count in
6. column
- When y₁ and y₂ have the same parity, count =
(y₂ - y₁)/2 + 1
- Number of columns
- Total columns from x₁ to x₂ =
x₂ - x₁ + 1
- How many "reduced" columns?
- If columns alternate: full, reduced, full, reduced...
- The number of reduced columns =
⌊(n-1)/2⌋ where n is column count
- Since n = x₂ - x₁ + 1, we get: `⌊(x₂ - x₁)/2
This gives us the final answer: columns from x₁ to x₂ = x₂ - x₁ + 1
Detailed Solution and Verification
This problem asks us to count cells on a chessboard-pattern grid where the sum of coordinates has a specific parity (like counting black squares).
Understanding the Pattern
On a chessboard, cells where (x + y) is even alternate with cells where (x + y) is odd. Let's analyze how this affects each column:
For any column x:
- If x has parity p, then cells with the "correct" parity have y with parity p as well
- As we scan down a column, valid cells alternate: yes, no, yes, no...
Example: If y ranges from 1 to 5:
- The valid cells in alternating columns would be: 3, 2, 3, 2, 3 cells
- First column: {1,3,5} → 3 cells
- Second column: {2,4} → 2 cells
- Third column: {1,3,5} → 3 cells
Deriving the Formula
Step 1: Count in first column
- When y₁ and y₂ have the same parity, count =
(y₂ - y₁)/2 + 1
Step 2: Number of columns
- Total columns from x₁ to x₂ =
x₂ - x₁ + 1
Step 3: How many "reduced" columns?
- If columns alternate: full, reduced, full, reduced...
- The number of reduced columns =
⌊(n-1)/2⌋ where n is column count
- Since n = x₂ - x₁ + 1, we get:
⌊(x₂ - x₁)/2⌋
Step 4: Final formula
Answer=(2y
Verification
Let's verify with a small example: x₁=1, x₂=3, y₁=1, y₂=5
Manual count:
- Column 1 (x=1): cells at y∈{1,3,5} where (1+1),(1+3),(1+5) are even → 3 cells
- Column 2 (x=2): cells at y∈{2,4} where (2+2),(2+4) are even → 2 cells
- Column 3 (x=3): cells at y∈{1,3,5} where (3+1),(3+3),(3+5) are even → 3 cells
- Total: 8 cells
Using formula:
- First column size: (5-1)/2 + 1 = 3
- Column count: 3 - 1 + 1 = 3
- Reduced columns: (3-1)/2 = 1
- Answer: 3 × 3 - 1 = 8 ✓
Implementation Note
Use 64-bit integers (long long in C++) to avoid overflow during multiplication, as the intermediate result could exceed 32-bit integer limits.
long long answer = ((y2 - y1) / 2 + 1) * (long long)(x2 - x1 + 1) - (x2 - x1) / 2;