time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 l1 to
minute r1 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 l2 to
minute r2 inclusive.

Calculate the number of minutes they will be able to spend together.

Input

The only line of the input contains integers l1, r1, l2, r2 and k (1 ≤ l1, r1, l2, r2, k ≤ 1018, l1 ≤ r1, l2 ≤ r2),
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.

Examples
input
1 10 9 20 1
output
2
input
1 100 50 200 75
output
50
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.

【题解】

求两个区间的交集。

很明显的线段树问题

(滑稽)

把两个区间的左端点的位置确定一下就好。

然后再看两个区间的右端点。

再判断k在不在交集中。

吧嗒吧嗒。。

【代码】

#include <cstdio>

long long left1, right1, left2, right2, k,ans;

void input_data()
{
scanf("%I64d%I64d%I64d%I64d%I64d", &left1, &right1, &left2, &right2, &k);
} void get_ans()
{
if (left1 > left2) //固定两个区间的左端点为从左到右
{
long long t = left1;
left1 = left2;
left2 = t;
t = right1;
right1 = right2;
right2 = t;
}
if (right1 < left2) //这是没有交集的情况。
printf("0\n");
else
if (right2 <= right1)
{
ans = right2 - left2 + 1;
if (left2 <= k && k <= right2)
ans--;
printf("%I64d\n", ans);
}
else
{
ans = right1 - left2 + 1;
if (left2 <= k && k <= right1)
ans--;
printf("%I64d\n", ans);
} } int main()
{
//freopen("F:\\rush.txt", "r", stdin);
input_data();
get_ans();
return 0;
}

最新文章

  1. iOS开发:http中的get和post请求
  2. 使用Mybatis-Generator自动生成Dao、Model、Mapping代码
  3. winAPI 检查系统设备拔插使用 WM_DEVICECHANGE 消息
  4. Android 4.4 KitKat 新特性
  5. JSon 事件格式化
  6. Java设计模式系列1--原型模式(Prototype Method)
  7. Zero Requiem
  8. Java基础类库
  9. Cisco asa 5510升级IOS和ASDM
  10. ecshop以幻灯版调用首页主广告显示
  11. C#调用金数据API
  12. Object-c Block的使用及说明
  13. 导入数据到mysql服务器上,报错,以及停止的解决办法
  14. Asp.net mvc 中Action 方法的执行(一)
  15. nodejs的package.json依赖dependencies中 ^ 和 ~ 的区别
  16. hololens Vuforia新时期的开发注意事项
  17. Qt_颜色选择对话框(QColorDialog)
  18. 设计模式-行为型模式,python备忘录模式
  19. Owin中间件动手做
  20. SharePoint 2013 处理Promoted Links类型的List下的Tiles View的默认Webpart展示方式

热门文章

  1. 【JZOJ4772】【NOIP2016提高A组模拟9.9】运输妹子
  2. 公司mysql问题二
  3. H5视频播放自动全屏,暂停退出全屏等功能
  4. php表单的种类
  5. Java练习 SDUT-2053_整理音乐
  6. Java练习 SDUT-3081_谁是最强女汉子
  7. @游记@ CSP2019
  8. android 数据存储----android短信发送器之文件的读写(手机+SD卡)
  9. 因为对 Docker 不熟悉建了 N 多个 Nginx
  10. web移动开发小贴士