您的位置:首页 > 其它

Codeforces Round #371 (Div. 2) A. Meeting of Old Friends 数学、A题第三次被系统hack了 (┬_┬)、集合交集

2016-09-16 02:31 369 查看
A. Meeting of Old Friends

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.

Source

Codeforces Round #371 (Div. 2)

My Solution

数学、A题第三次被系统hack了 (┬_┬)

虽然比赛期间 趁机 hack 了一份C++的A题代码 和 两份 python的A题代码, 他们都是简单错误, 都是 l > r 出现负数了

笔者自己是分类讨论的, 所以不会出现 l > r 从而出现负数的情况, 但分类讨论中 有个地方 应该是 if(d < a) ans = 0; 写成了 if(d <= a), (┬_┬) 小于写成了小于等于

以后一定要仔细考虑 == 什么时候取到啊, 这里一个小忽视, 直接 从 371 名 稳涨分的名次调到了 846名 ⊙﹏⊙‖∣ 可惜了

分类讨论的情况比较多, 这样的小地方确实不容易检查的到, 没办法, 此外这个 A 题被我复杂化了, 不用这样麻烦分类讨论的, 

这个就是 2个集合(区间)求交集, 然后 判断 k 是否在那个交集里。 

1、分类讨论版本

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 8;

//2 4 1 2 5

int main()
{
#ifdef LOCAL
freopen("a.txt", "r", stdin);
//freopen("a.out", "w", stdout);
int T = 6;
while(T--){
#endif // LOCAL
ios::sync_with_stdio(false); cin.tie(0);

LL a, b, c, d, k, ans = 0;
cin >> a >> b >> c >> d >> k;
if(a == b){
if(a >= c && a <= d){
if(a != k) ans = 1;
else ans  = 0;
}
else ans = 0;
}
else if(c == d){
if(c >= a && c <= b){
if(k != c) ans = 1;
else ans = 0;
}
else ans = 0;
}
else if(c <= a){
if(d >= b){
ans = b - a + 1;
if(k >= a && k <= b) ans--;
}
else{
//WA29 if(d <= a) ans = 0;
if(d < a) ans = 0;
else{
ans = d - a + 1;
if(k >= a && k <= d) ans--;
}
}
}
else if(c <= b){
if(d >= b){
ans = b - c + 1;
if(k >= c && k <= b) ans--;
}
else{
ans = d - c + 1;
if(k >= c && k <= d) ans--;
}
}
else{
ans = 0;
}

cout << ans << endl;

#ifdef LOCAL
cout << endl;
}
#endif // LOCAL
return 0;
}


2、求交集版本,推荐版本

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 8;

//2 4 1 2 5

int main()
{
#ifdef LOCAL
freopen("a.txt", "r", stdin);
//freopen("a.out", "w", stdout);
int T = 7;
while(T--){
#endif // LOCAL
ios::sync_with_stdio(false); cin.tie(0);

LL a, b, c, d, k, ans = 0;
cin >> a >> b >> c >> d >> k;
LL l = max(a, c);
LL r = min(b, d);
if(l > r) ans = 0;
else{
ans = r - l + 1;
if(k >= l && k <= r) ans--;
}

cout << ans << endl;

#ifdef LOCAL
cout << endl;
}
#endif // LOCAL
return 0;
}


  Thank you!

                                                                                                           
                                   ------from ProLights
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: