C. Find a car

Codeforces
IDCF809C
Time4000ms
Memory256MB
Difficulty
combinatoricsdivide and conquerdp
English · Original
Chinese · Translation
Formal · Original
After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help. Formally the parking can be represented as a matrix 109 × 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (_x_, _y_) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (_i_, _y_) and (_x_, _j_), 1 ≤ _i_ < _x_, 1 ≤ _j_ < _y_. <center>![image](https://espresso.codeforces.com/b8c19814ed2984420031e827a5f67c82d4a6bbca.png) The upper left fragment 5 × 5 of the parking</center>Leha wants to ask the watchman _q_ requests, which can help him to find his car. Every request is represented as five integers _x_1, _y_1, _x_2, _y_2, _k_. The watchman have to consider all cells (_x_, _y_) of the matrix, such that _x_1 ≤ _x_ ≤ _x_2 and _y_1 ≤ _y_ ≤ _y_2, and if the number of the car in cell (_x_, _y_) does not exceed _k_, increase the answer to the request by the number of the car in cell (_x_, _y_). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7. However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests. ## Input The first line contains one integer _q_ (1 ≤ _q_ ≤ 104) — the number of Leha's requests. The next _q_ lines contain five integers _x_1, _y_1, _x_2, _y_2, _k_ (1 ≤ _x_1 ≤ _x_2 ≤ 109, 1 ≤ _y_1 ≤ _y_2 ≤ 109, 1 ≤ _k_ ≤ 2·109) — parameters of Leha's requests. ## Output Print exactly _q_ lines — in the first line print the answer to the first request, in the second — the answer to the second request and so on. [samples] ## Note Let's analyze all the requests. In each case the requested submatrix is highlighted in blue. In the first request (_k_ = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1. ![image](https://espresso.codeforces.com/d0cfce1ecfc6e43d21d60c211dfb7eea89989b30.png) In the second request (_k_ = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13. ![image](https://espresso.codeforces.com/a84f438ac75164f76b71d6a93cc1f943d1605578.png) In the third request (_k_ = 10000) Leha asks about the upper left frament 5 × 5 of the parking. Since _k_ is big enough, the answer is equal to 93. ![image](https://espresso.codeforces.com/d4e241bec4723df71cacdd81a4cdba426e434bd5.png) In the last request (_k_ = 2) none of the cur's numbers are suitable, so the answer is 0. ![image](https://espresso.codeforces.com/ffe732efa689a47d02e04fe03c2619fcb85c7768.png)
在餐厅度过一个美好的夜晚后,到了回家的时间。Leha 作为一个真正的绅士,主动提出送 Noora 一程。女孩当然愉快地同意了。但突然出现了一个问题:Leha 在餐厅附近巨大的停车场里找不到自己的车。于是他决定向保安求助。 形式上,停车场可以表示为一个 #cf_span[109 × 109] 的矩阵。矩阵中的每个单元格恰好有一辆车。所有车辆都有自己的编号,表示为一个正整数。我们从左到右用整数 #cf_span[1] 到 #cf_span[109] 对矩阵的列进行编号,从上到下用整数 #cf_span[1] 到 #cf_span[109] 对行进行编号。巧合的是,对于每个单元格 #cf_span[(x, y)],其中车辆的编号等于最小的正整数,该整数在单元格 #cf_span[(i, y)] 和 #cf_span[(x, j)](其中 #cf_span[1 ≤ i < x, 1 ≤ j < y])中均未出现。 Leha 想向保安提出 #cf_span[q] 个请求,以帮助他找到自己的车。每个请求由五个整数 #cf_span[x1, y1, x2, y2, k] 表示。保安需要考虑所有满足 #cf_span[x1 ≤ x ≤ x2] 且 #cf_span[y1 ≤ y ≤ y2] 的矩阵单元格 #cf_span[(x, y)],如果单元格 #cf_span[(x, y)] 中车辆的编号不超过 #cf_span[k],则将该车辆编号加到该请求的答案中。对于每个请求,Leha 要求保安告诉他最终的总和。由于总和可能非常大,Leha 要求对 #cf_span[109 + 7] 取模计算。 然而,这些请求对保安来说似乎难以处理。请帮助保安回答 Leha 的所有请求。 第一行包含一个整数 #cf_span[q] #cf_span[(1 ≤ q ≤ 104)] —— Leha 的请求数量。 接下来的 #cf_span[q] 行每行包含五个整数 #cf_span[x1, y1, x2, y2, k] #cf_span[(1 ≤ x1 ≤ x2 ≤ 109, 1 ≤ y1 ≤ y2 ≤ 109, 1 ≤ k ≤ 2·109)] —— Leha 请求的参数。 请输出恰好 #cf_span[q] 行——第一行输出第一个请求的答案,第二行输出第二个请求的答案,依此类推。 让我们分析所有请求。在每种情况下,所请求的子矩阵均用蓝色标出。 在第一个请求中 #cf_span[(k = 1)],Leha 仅询问左上角的停车单元格。该单元格中的车辆编号为 #cf_span[1]。因此答案为 #cf_span[1]。 在第二个请求中 #cf_span[(k = 5)],符合条件的编号为 #cf_span[4, 1, 2, 3, 2, 1]。因此答案为 #cf_span[4 + 1 + 2 + 3 + 2 + 1 = 13]。 在第三个请求中 #cf_span[(k = 10000)],Leha 询问停车场左上角 #cf_span[5 × 5] 的区域。由于 #cf_span[k] 足够大,答案等于 #cf_span[93]。 在最后一个请求中 #cf_span[(k = 2)],没有车辆编号符合条件,因此答案为 #cf_span[0]。 ## Input 第一行包含一个整数 #cf_span[q] #cf_span[(1 ≤ q ≤ 104)] —— Leha 的请求数量。接下来的 #cf_span[q] 行每行包含五个整数 #cf_span[x1, y1, x2, y2, k] #cf_span[(1 ≤ x1 ≤ x2 ≤ 109, 1 ≤ y1 ≤ y2 ≤ 109, 1 ≤ k ≤ 2·109)] —— Leha 请求的参数。 ## Output 请输出恰好 #cf_span[q] 行——第一行输出第一个请求的答案,第二行输出第二个请求的答案,依此类推。 [samples] ## Note 让我们分析所有请求。在每种情况下,所请求的子矩阵均用蓝色标出。在第一个请求中 #cf_span[(k = 1)],Leha 仅询问左上角的停车单元格。该单元格中的车辆编号为 #cf_span[1]。因此答案为 #cf_span[1]。在第二个请求中 #cf_span[(k = 5)],符合条件的编号为 #cf_span[4, 1, 2, 3, 2, 1]。因此答案为 #cf_span[4 + 1 + 2 + 3 + 2 + 1 = 13]。在第三个请求中 #cf_span[(k = 10000)],Leha 询问停车场左上角 #cf_span[5 × 5] 的区域。由于 #cf_span[k] 足够大,答案等于 #cf_span[93]。在最后一个请求中 #cf_span[(k = 2)],没有车辆编号符合条件,因此答案为 #cf_span[0]。
**Definitions** Let $ M(x, y) $ denote the car number in cell $ (x, y) $, defined as: $$ M(x, y) = \mathrm{mex}\left( \{ M(i, y) \mid 1 \le i < x \} \cup \{ M(x, j) \mid 1 \le j < y \} \right) $$ where $ \mathrm{mex}(S) $ is the minimum excluded positive integer from set $ S $. **Constraints** 1. $ 1 \le q \le 10^4 $ 2. For each request: $ 1 \le x_1 \le x_2 \le 10^9 $, $ 1 \le y_1 \le y_2 \le 10^9 $, $ 1 \le k \le 2 \cdot 10^9 $ **Objective** For each request $ (x_1, y_1, x_2, y_2, k) $, compute: $$ \sum_{\substack{x = x_1 \\ y = y_1}}^{x_2, y_2} M(x, y) \cdot \mathbf{1}_{M(x, y) \le k} \mod (10^9 + 7) $$
Samples
Input #1
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output #1
1
13
93
0
API Response (JSON)
{
  "problem": {
    "name": "C. Find a car",
    "description": {
      "content": "After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeare",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 4000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF809C"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeare...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "在餐厅度过一个美好的夜晚后,到了回家的时间。Leha 作为一个真正的绅士,主动提出送 Noora 一程。女孩当然愉快地同意了。但突然出现了一个问题:Leha 在餐厅附近巨大的停车场里找不到自己的车。于是他决定向保安求助。\n\n形式上,停车场可以表示为一个 #cf_span[109 × 109] 的矩阵。矩阵中的每个单元格恰好有一辆车。所有车辆都有自己的编号,表示为一个正整数。我们从左到右用整数 #c...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ M(x, y) $ denote the car number in cell $ (x, y) $, defined as:  \n$$\nM(x, y) = \\mathrm{mex}\\left( \\{ M(i, y) \\mid 1 \\le i < x \\} \\cup \\{ M(x, j) \\mid 1 \\le j < y \\} \\right)\n$$ ...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments