ZCMU-1772-区间交集

本文介绍了一个编程问题,两只动物在不同时间段活动,需计算它们共同在线的时间,考虑到其中一个时间段内的特殊分钟数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1772: Meeting of Old Friends

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 95   Solved: 28
[ Submit][ Status][ Web Board]

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 l1, r1, l2, r2 and k (1 ≤ l1, r1, l2, r2, k ≤ 10^18, 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.

Sample Input

1 10 9 20 1

Sample Output

2


【解析】

文章大意就是一个动物要去见另外一个动物,一个动物醒着的时间已知,另一个动物去的时间也知道,然后在k这

时间它要化妆不见客。这个其实就是求交集,这个我WA了很多次...没办法数学实在是....

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    long long a,b,c,d,k,count;
    while(~scanf("%lld%lld%lld%lld%lld",&a,&b,&c,&d,&k))
    {
        count=0;
        if(c<=b&&a<=d)//判断符不符合条件看看有没有交集
        {
            if(a<c)
            {
                a=c;//取两者中小的
            }
            if(b>d)
                b=d;//去两者中小的
            count=b-a+1;//两者相减加1算见面的时间
            if(k>=a&&k<=b)
              count--;//如果K在区间内,我们就减去1,因为在那个时间它要化妆
              printf("%lld\n",count);
        }
        else
            printf("0\n");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值