A. Meeting of Old Friends

Codeforces
IDCF714A
Time1000ms
Memory256MB
Difficulty
implementationmath
English · Original
Chinese · Translation
Formal · Original
Today an outstanding event is going to happen in the forest — hedgehog Filya will come to his old fried Sonya! Sonya is an owl and she sleeps during the day and stay awake from minute _l_1 to minute _r_1 inclusive. Also, during the minute _k_ she prinks and is unavailable for Filya. Filya works a lot and he plans to visit Sonya from minute _l_2 to minute _r_2 inclusive. Calculate the number of minutes they will be able to spend together. ## Input The only line of the input contains integers _l_1, _r_1, _l_2, _r_2 and _k_ (1 ≤ _l_1, _r_1, _l_2, _r_2, _k_ ≤ 1018, _l_1 ≤ _r_1, _l_2 ≤ _r_2), providing the segments of time for Sonya and Filya and the moment of time when Sonya prinks. ## Output Print one integer — the number of minutes Sonya and Filya will be able to spend together. [samples] ## Note In the first sample, they will be together during minutes 9 and 10. In the second sample, they will be together from minute 50 to minute 74 and from minute 76 to minute 100.
今天森林里将发生一件盛事——刺猬 Filya 将去拜访他的老朋友 Sonya! Sonya 是一只猫头鹰,她白天睡觉,从第 #cf_span[l1] 分钟到第 #cf_span[r1] 分钟(含)保持清醒。此外,在第 #cf_span[k] 分钟时,她正在梳理羽毛,无法接待 Filya。 Filya 工作繁忙,他计划从第 #cf_span[l2] 分钟到第 #cf_span[r2] 分钟(含)拜访 Sonya。 请计算他们能够一起度过的分钟数。 输入仅一行,包含五个整数 #cf_span[l1], #cf_span[r1], #cf_span[l2], #cf_span[r2] 和 #cf_span[k](#cf_span[1 ≤ l1, r1, l2, r2, k ≤ 1018],#cf_span[l1 ≤ r1],#cf_span[l2 ≤ r2]),分别表示 Sonya 和 Filya 的时间区间以及 Sonya 梳理羽毛的时刻。 请输出一个整数——Sonya 和 Filya 能够一起度过的分钟数。 在第一个样例中,他们将在第 #cf_span[9] 分钟和第 #cf_span[10] 分钟一起度过。 在第二个样例中,他们将在第 #cf_span[50] 分钟到第 #cf_span[74] 分钟,以及第 #cf_span[76] 分钟到第 #cf_span[100] 分钟一起度过。 ## Input 输入仅一行,包含五个整数 #cf_span[l1], #cf_span[r1], #cf_span[l2], #cf_span[r2] 和 #cf_span[k](#cf_span[1 ≤ l1, r1, l2, r2, k ≤ 1018],#cf_span[l1 ≤ r1],#cf_span[l2 ≤ r2]),分别表示 Sonya 和 Filya 的时间区间以及 Sonya 梳理羽毛的时刻。 ## Output 请输出一个整数——Sonya 和 Filya 能够一起度过的分钟数。 [samples] ## Note 在第一个样例中,他们将在第 #cf_span[9] 分钟和第 #cf_span[10] 分钟一起度过。在第二个样例中,他们将在第 #cf_span[50] 分钟到第 #cf_span[74] 分钟,以及第 #cf_span[76] 分钟到第 #cf_span[100] 分钟一起度过。
Let $ I_1 = [l_1, r_1] $ and $ I_2 = [l_2, r_2] $ be the time intervals during which Sonya is awake and Filya visits, respectively. Let $ k $ be the minute when Sonya is prinking and unavailable. Define the intersection interval: $$ I = [l_1, r_1] \cap [l_2, r_2] = [\max(l_1, l_2), \min(r_1, r_2)] $$ If $ \max(l_1, l_2) > \min(r_1, r_2) $, then $ I = \emptyset $, and the overlap is 0. Otherwise, the total overlapping minutes are: $$ |I| = \min(r_1, r_2) - \max(l_1, l_2) + 1 $$ If $ k \notin I $, then the time they spend together is $ |I| $. If $ k \in I $, then they lose one minute, so the time together is $ |I| - 1 $. **Final Answer:** $$ \begin{cases} 0 & \text{if } \max(l_1, l_2) > \min(r_1, r_2) \\ \min(r_1, r_2) - \max(l_1, l_2) & \text{if } \max(l_1, l_2) \leq \min(r_1, r_2) \text{ and } k \in [\max(l_1, l_2), \min(r_1, r_2)] \\ \min(r_1, r_2) - \max(l_1, l_2) + 1 & \text{if } \max(l_1, l_2) \leq \min(r_1, r_2) \text{ and } k \notin [\max(l_1, l_2), \min(r_1, r_2)] \end{cases} $$
Samples
Input #1
1 10 9 20 1
Output #1
2
Input #2
1 100 50 200 75
Output #2
50
API Response (JSON)
{
  "problem": {
    "name": "A. Meeting of Old Friends",
    "description": {
      "content": "Today an outstanding event is going to happen in the forest — hedgehog Filya will come to his old fried Sonya! Sonya is an owl and she sleeps during the day and stay awake from minute _l_1 to minute ",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF714A"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Today an outstanding event is going to happen in the forest — hedgehog Filya will come to his old fried Sonya!\n\nSonya is an owl and she sleeps during the day and stay awake from minute _l_1 to minute ...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "今天森林里将发生一件盛事——刺猬 Filya 将去拜访他的老朋友 Sonya!\n\nSonya 是一只猫头鹰,她白天睡觉,从第 #cf_span[l1] 分钟到第 #cf_span[r1] 分钟(含)保持清醒。此外,在第 #cf_span[k] 分钟时,她正在梳理羽毛,无法接待 Filya。\n\nFilya 工作繁忙,他计划从第 #cf_span[l2] 分钟到第 #cf_span[r2] 分钟(含)...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "Let $ I_1 = [l_1, r_1] $ and $ I_2 = [l_2, r_2] $ be the time intervals during which Sonya is awake and Filya visits, respectively. Let $ k $ be the minute when Sonya is prinking and unavailable.\n\nDef...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments