CodeForces 714A

Description

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 l1r1l2r2 and k (1 ≤ l1, r1, l2, r2, k ≤ 1018l1 ≤ r1l2 ≤ 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.

Sample Input

Input
1 10 9 20 1
Output
2
Input
1 100 50 200 75
Output
50

Hint

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.

算是一道非常非常简单的贪心做法题吧。

但我还是WA了无数发,只因为那个被我犯过好几次的错误,不是第一次犯这种错了。

只考虑到了两个区间相交和相离的情况,没有考虑到内含的情况。

这题应该注意的是k是在相交区间断点的问题。

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<math.h>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<stack>
 8 #include<deque>
 9 #include<iostream>
10 using namespace std;
11 typedef long long  LL;
12 
13 struct con{
14     LL head;
15     LL tail;
16 }con[10];
17 LL cmp(struct con a,struct con b)
18 {
19     if(a.head==b.head)
20         return a.tail<b.tail;
21     return a.head<b.head;
22 }
23 int main()
24 {
25     LL i,p,j,n;
26     LL k,ans=0;
27 //    scanf("%lld",&p);
28 //    printf("%lld
",p);
29     for(i=0;i<=1;i++)
30     {
31         scanf("%lld%lld",&con[i].head,&con[i].tail);
32     }
33     scanf("%lld",&k);
34     sort(con,con+2,cmp);
35 
36     if(con[1].head<=con[0].tail&&(k>con[0].tail||k<con[1].head))
37     {
38         ans=con[0].tail-con[1].head+1;
39     }
40     if(con[1].head<=con[0].tail&&(k<=con[0].tail&&k>=con[1].head))
41     {
42         ans=con[0].tail-con[1].head;
43     }
44     if(con[0].tail>=con[1].tail&&(k>con[1].tail||k<con[1].head))
45     {
46         ans=con[1].tail-con[1].head+1;
47     }
48     if(con[0].tail>=con[1].tail&&(k<=con[1].tail&&k>=con[1].head))
49     {
50         ans=con[1].tail-con[1].head;
51     }
52     printf("%lld
",ans);
53     return 0;
54 }
View Code
原文地址:https://www.cnblogs.com/daybreaking/p/9751392.html