Codeforces 714A. Meeting of Old Friends

题目链接:http://codeforces.com/problemset/problem/714/A

题意:

  一个猫头鹰可以在时间段 l1 到 r1 处于清醒状态, 且需要在 k 时为自己化妆,在 l2 到 r2 时间段去访问自己的朋友, 问它能和自己的朋友在一起待多久时间.

思路:

  设呆在一起的时间为 anst, 则 anst = min(r1, r2) - max(l1, l2) + 1,如果 k 处于 min(r1, r2) 和 max(l1, l2) 之间, 则还需要减去化妆的一刻: anst--.

代码:

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 typedef long long LL;
 5 const int MAXN = 44721;
 6 
 7 int main() {
 8      ios_base::sync_with_stdio(0); cin.tie(0);
 9      LL l1, r1, l2, r2, k; cin >> l1 >> r1 >> l2 >> r2 >> k;
10     LL r = min(r1, r2), l = max(l1, l2), ans = 0;
11     if(l <= r) ans = r - l + 1;
12     if(k <= r && k >= l ) ans -= 1;
13     cout << ans << endl;
14     return 0;
15 }
原文地址:https://www.cnblogs.com/Ash-ly/p/5874327.html