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. 1 10 9 20 1
output
  1. 2
input
  1. 1 100 50 200 75
output
  1. 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在不在交集中。

吧嗒吧嗒。。

【代码】

  1. #include <cstdio>
  2.  
  3. long long left1, right1, left2, right2, k,ans;
  4.  
  5. void input_data()
  6. {
  7. scanf("%I64d%I64d%I64d%I64d%I64d", &left1, &right1, &left2, &right2, &k);
  8. }
  9.  
  10. void get_ans()
  11. {
  12. if (left1 > left2) //固定两个区间的左端点为从左到右
  13. {
  14. long long t = left1;
  15. left1 = left2;
  16. left2 = t;
  17. t = right1;
  18. right1 = right2;
  19. right2 = t;
  20. }
  21. if (right1 < left2) //这是没有交集的情况。
  22. printf("0\n");
  23. else
  24. if (right2 <= right1)
  25. {
  26. ans = right2 - left2 + 1;
  27. if (left2 <= k && k <= right2)
  28. ans--;
  29. printf("%I64d\n", ans);
  30. }
  31. else
  32. {
  33. ans = right1 - left2 + 1;
  34. if (left2 <= k && k <= right1)
  35. ans--;
  36. printf("%I64d\n", ans);
  37. }
  38.  
  39. }
  40.  
  41. int main()
  42. {
  43. //freopen("F:\\rush.txt", "r", stdin);
  44. input_data();
  45. get_ans();
  46. return 0;
  47. }

【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. Leetcode747.Largest Number At Least Twice of Others至少是其他数字两倍的最大数

    在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 1: 输入: nums = [3, ...

  2. MVVM框架搭建

    以下是概要的目录结构,其中View,ViewModel,Model正代表的是MVVM的标识. View:页面window或者UserControl Model:数据模型对象 ViewModel:与Vi ...

  3. 6.12号整理(h5新特性-图片、文件上传)

    <input type="file" id='myFile' multiple> <ul> <li> <img src="&qu ...

  4. nodeJs学习-13 router

    const express=require('express'); var server=express(); //目录1:/user/ var routeUser=express.Router(); ...

  5. hdu4310 贪心

    考虑每次血口的要少 就按照一滴血多少伤害来计算.由于直接相除有小数.考虑x/y > a/b  =>  x*b >y*a; #include<stdio.h> #inclu ...

  6. Validation异常:No validator could be found for constraint '.....' validating type 'java.lang.Integer'.

    javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'java ...

  7. thinkphp5.0 路由规则配置

    开启路由‘url_route_on’ => true 首页路由'/' =>'home/index/index' 其它路由(1)'route' => 'home/index/route ...

  8. TIJ——Chapter Three:Operators

    Operators 本章节比较简单,所以简单的做一些笔记: 几个要点: 1.When the compiler sees a String followed by a "+" fo ...

  9. EL表达式多条件或判断用法

    简单记录一EL表达式的判断用法 <c:if test="${(order.status == '06'&& order.type=='02') || (order.st ...

  10. Python基础:19类和实例的内建函数

    1:issubclass() issubclass()布尔函数,判断一个类是否是另一个类的子类或子孙类.它有如下语法:issubclass(sub,sup) 这个函数也允许“不严格”的子类,意味着,一 ...