C. Crazy Dreamoon

Codeforces
IDCF10123C
Time1000ms
Memory512MB
Difficulty
English · Original
Formal · Original
Dreamoon likes algorithm competitions very much. But when he feels crazy because he cannot figure out any solution for any problem in a competition, he often draws many meaningless straight line segments on his calculation paper. Dreamoon's calculation paper is special: it can be imagined as the plane with Cartesian coordinate system with range [0, 2000 × [0, 2000]] for the coordinates. The grid lines are all lines of the form x = c or y = c for every integer c between 0 and 2000, inclusive. So, the grid contains 2000 × 2000 squares. Now, Dreamoon wonders how many grid squares are crossed by at least one of the lines he drew. Please help Dreamoon find the answer. Note that, to cross a square, a segment must contain an interior point of the square. The first line of input contains an integer N denoting the number of lines Dreamoon draw. The i-th line of following N lines contains four integers xi1, yi1, xi2, yi2, denoting that the i-th segment Dreamoon drew is a straight line segment between points (xi1, yi1) and (xi2, yi2). Output one integer on a single line: how many grid squares are crossed by at least one of the line segments which Dreamoon drew. ## Input The first line of input contains an integer N denoting the number of lines Dreamoon draw. The i-th line of following N lines contains four integers xi1, yi1, xi2, yi2, denoting that the i-th segment Dreamoon drew is a straight line segment between points (xi1, yi1) and (xi2, yi2). 1 ≤ N ≤ 2 × 103 0 ≤ xi1, yi1, xi2, yi2 ≤ 2 × 103 the lengths of all line segments in input are non-zero ## Output Output one integer on a single line: how many grid squares are crossed by at least one of the line segments which Dreamoon drew. [samples]
**Definitions** Let $ N \in \mathbb{Z}^+ $ be the number of line segments. For each $ i \in \{1, \dots, N\} $, let $ L_i $ be a line segment defined by endpoints $ (x_{i1}, y_{i1}) $ and $ (x_{i2}, y_{i2}) $, where $ x_{ij}, y_{ij} \in [0, 2000] \cap \mathbb{Z} $. Let $ \mathcal{S} = \{ [a, a+1] \times [b, b+1] \mid a, b \in \{0, 1, \dots, 1999\} \} $ be the set of all unit grid squares in the $ 2000 \times 2000 $ grid. **Constraints** 1. $ 1 \le N \le 1000 $ 2. For each $ i $, $ 0 \le x_{i1}, y_{i1}, x_{i2}, y_{i2} \le 2000 $ **Objective** Compute the cardinality of the set: $$ \left| \left\{ S \in \mathcal{S} \mid \exists\, i \in \{1, \dots, N\} \text{ such that } L_i \cap \text{int}(S) \neq \emptyset \right\} \right| $$ where $ \text{int}(S) $ denotes the interior of square $ S $.
API Response (JSON)
{
  "problem": {
    "name": "C. Crazy Dreamoon",
    "description": {
      "content": "Dreamoon likes algorithm competitions very much. But when he feels crazy because he cannot figure out any solution for any problem in a competition, he often draws many meaningless straight line segme",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 524288
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10123C"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Dreamoon likes algorithm competitions very much. But when he feels crazy because he cannot figure out any solution for any problem in a competition, he often draws many meaningless straight line segme...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ N \\in \\mathbb{Z}^+ $ be the number of line segments.  \nFor each $ i \\in \\{1, \\dots, N\\} $, let $ L_i $ be a line segment defined by endpoints $ (x_{i1}, y_{i1}) $ and $ (x_{i2}...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments