Cover query

AtCoder
IDabc435_e
Time2000ms
Memory256MB
Difficulty
There are $N$ cells arranged in a row from left to right. The $i$\-th cell from the left $(1\leq i\leq N)$ is called cell $i$. Initially, all cells are painted white. Process $Q$ queries in order. The $i$\-th query $(1\leq i\leq Q)$ is as follows: > Two integers $(L_i,R_i)$ $(1\leq L_i\leq R_i\leq N)$ are given. > Paint all cells $L_i, L_i+1, \ldots, R_i$ black. > Here, cells that were painted white are now painted black, and cells that were painted black remain as is. > After that, find the number of cells (among the $N$ cells) that are painted **white**. ## Constraints * $1\leq N\leq 10^9$ * $1\leq Q\leq 2\times 10^5$ * $1\leq L_i\leq R_i\leq N$ * All input values are integers. ## Input The input is given from Standard Input in the following format: $N$ $Q$ $L_1$ $R_1$ $L_2$ $R_2$ $\vdots$ $L_Q$ $R_Q$ [samples]
Samples
Input #1
10 5
3 5
8 9
5 8
2 9
6 6
Output #1
7
5
3
2
2

Initially, $10$ cells are arranged in a row from left to right.

*   The first query paints cells $3,4,5$ black. After the operation, the cells painted white are cells $1,2,6,7,8,9,10$, which is $7$ cells.
*   The second query paints cells $8,9$ black. After the operation, the cells painted white are $1,2,6,7,10$, which is $5$ cells.
*   The third query paints cells $5,6,7,8$ black. After the operation, the cells painted white are $1,2,10$, which is $3$ cells.
*   The fourth query paints cells $2,3,\ldots,9$ black. After the operation, the cells painted white are $1,10$, which is $2$ cells.
*   The fifth query paints cell $6$ black. After the operation, the cells painted white are cells $1,10$, which is $2$ cells.

Thus, output $7,5,3,2,2$ in this order on each line.
Input #2
1000000000 1
1 500000000
Output #2
500000000
API Response (JSON)
{
  "problem": {
    "name": "Cover query",
    "description": {
      "content": "There are $N$ cells arranged in a row from left to right.   The $i$\\-th cell from the left $(1\\leq i\\leq N)$ is called cell $i$.   Initially, all cells are painted white. Process $Q$ queries in order.",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc435_e"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "There are $N$ cells arranged in a row from left to right.  \nThe $i$\\-th cell from the left $(1\\leq i\\leq N)$ is called cell $i$.  \nInitially, all cells are painted white.\nProcess $Q$ queries in order....",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments