【31.42%】【CF 714A】Meeting of Old Friends
1 second
256 megabytes
standard input
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.
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.
Print one integer — the number of minutes Sonya and Filya will be able to spend together.
1 10 9 20 1
2
1 100 50 200 75
50
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;
}
最新文章
- iOS开发:http中的get和post请求
- 使用Mybatis-Generator自动生成Dao、Model、Mapping代码
- winAPI 检查系统设备拔插使用 WM_DEVICECHANGE 消息
- Android 4.4 KitKat 新特性
- JSon 事件格式化
- Java设计模式系列1--原型模式(Prototype Method)
- Zero Requiem
- Java基础类库
- Cisco asa 5510升级IOS和ASDM
- ecshop以幻灯版调用首页主广告显示
- C#调用金数据API
- Object-c Block的使用及说明
- 导入数据到mysql服务器上,报错,以及停止的解决办法
- Asp.net mvc 中Action 方法的执行(一)
- nodejs的package.json依赖dependencies中 ^ 和 ~ 的区别
- hololens Vuforia新时期的开发注意事项
- Qt_颜色选择对话框(QColorDialog)
- 设计模式-行为型模式,python备忘录模式
- Owin中间件动手做
- SharePoint 2013 处理Promoted Links类型的List下的Tiles View的默认Webpart展示方式