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;
}

【31.42%】【CF 714A】Meeting of Old Friends的更多相关文章

  1. 【codeforces】【比赛题解】#851 CF Round #432 (Div.2)

    cf真的难…… 点我浏览丧题. [A]Arpa和她对墨西哥人浪的研究 Arpa正在对墨西哥人浪进行研究. 有n个人站成一排,从1到n编号,他们从时刻0开始墨西哥人浪. 在时刻1,第一个人站起来.在时刻 ...

  2. LeetCode:下一个更大元素I【31】

    LeetCode:下一个更大元素I[31] 题目描述 给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的 ...

  3. LeetCode:下一个排列【31】

    LeetCode:下一个排列[31] 题目描述 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排 ...

  4. 剑指Offer:栈的压入、弹出序列【31】

    剑指Offer:栈的压入.弹出序列[31] 题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈 ...

  5. JAVA 基础编程练习题42 【程序 42 求数字】

    42 [程序 42 求数字] 题目:809*??=800*??+9*??+1 其中??代表的两位数,8*??的结果为两位数,9*??的结果为 3 位数.求??代表的两位数,及 809*??后的结 果. ...

  6. 【CF 453A】 A. Little Pony and Expected Maximum(期望、快速幂)

    A. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...

  7. 【实战Java高并发程序设计 3】带有时间戳的对象引用:AtomicStampedReference

    [实战Java高并发程序设计 1]Java中的指针:Unsafe类 [实战Java高并发程序设计 2]无锁的对象引用:AtomicReference AtomicReference无法解决上述问题的根 ...

  8. 【Android】【录音】Android录音--AudioRecord、MediaRecorder

    [Android][录音]Android录音--AudioRecord.MediaRecorder Android提供了两个API用于实现录音功能:android.media.AudioRecord. ...

  9. 【剑指Offer学习】【全部面试题汇总】

    剑指Offer学习 剑指Offer这本书已经学习完了.从中也学习到了不少的东西,如今做一个总的文件夹.供自已和大家一起參考.学如逆水行舟.不进则退.仅仅有不断地学习才干跟上时候.跟得上技术的潮流! 全 ...

随机推荐

  1. 【JZOJ4771】【NOIP2016提高A组模拟9.9】爬山

    题目描述 国家一级爬山运动员h10今天获得了一张有着密密麻麻标记的地图,在好奇心的驱使下,他又踏上了去爬山的路. 对于爬山,h10有一个原则,那就是不走回头路,于是他把地图上的所有边都标记成了有向边. ...

  2. 五.获得MYSQL数据库自动生成的主键

    测试脚本如下: 1 create table test1 2 ( 3 id int primary key auto_increment, 4 name varchar(20) 5 ); 测试代码: ...

  3. 【JZOJ4888】【NOIP2016提高A组集训第14场11.12】最近公共祖先

    题目描述 YJC最近在学习树的有关知识.今天,他遇到了这么一个概念:最近公共祖先.对于有根树T的两个结点u.v,最近公共祖先LCA(T,u,v)表示一个结点x,满足x是u.v的祖先且x的深度尽可能大. ...

  4. el标签 2016-06-05 21:39 477人阅读 评论(15) 收藏

    JSP EL语言定义 E L(Expression Language) 目的:为了使JSP写起来更加简单. 表达式语言的灵感来自于 ECMAScript 和 XPath 表达式语言,它提供了在 JSP ...

  5. postman post 数据格式

    postman post 数据的时候,选择raw的tab ,选择json. 然后在下面的提交json数据, {"photoPath":"bbbbbbbbbbbbbb&qu ...

  6. PHPExcel 设置表格边框

    //设置单元格边框 $style_array = array( 'borders' => array( 'allborders' => array( 'style' => \PHPE ...

  7. python-----堡垒机前戏paramiko模块及进阶

    堡垒机前戏 开发堡垒机之前,先来学习Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作 SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: i ...

  8. 2018-2-13-windows-10预览版升级win10-7月29-10240.16384

    title author date CreateTime categories windows 10预览版升级win10 7月29 10240.16384 lindexi 2018-2-13 17:2 ...

  9. Android 性感美图在线浏览APP

    周末无聊,遂整理了下近来常用的几个开源库,无意间又发现了一些开放接口,于是乎决定融合在一起,做个简单的"性感美图"浏览的APP,名字呢,就叫"性感沙滩",效果如 ...

  10. uva 12003 Array Transformer (线段树套平衡树)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...