zoukankan      html  css  js  c++  java
  • 【31.42%】【CF 714A】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 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.

    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.

    【题解】

    求两个区间的交集。

    很明显的线段树问题

    (滑稽)

    把两个区间的左端点的位置确定一下就好。

    然后再看两个区间的右端点。

    再判断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
    ");
    	else
    		if (right2 <= right1)
    		{
    			ans = right2 - left2 + 1;
    			if (left2 <= k && k <= right2)
    				ans--;
    			printf("%I64d
    ", ans);
    		}
    		else
    		{
    			ans = right1 - left2 + 1;
    			if (left2 <= k && k <= right1)
    				ans--;
    			printf("%I64d
    ", ans);
    		}
    
    }
    
    int main()
    {
    	//freopen("F:\rush.txt", "r", stdin);
    	input_data();
    	get_ans();
    	return 0;
    }


  • 相关阅读:
    seata 1.3.0 seata Global lock wait timeout
    Tika解析word文件
    我的第一款微信小程序:iteye附件下载器,希望大家好好爱惜
    读书《尸检报告》 [英]卡拉·瓦伦丁 / 中信出版集团2019-08
    读书《另一种选择》 [美] 谢丽尔·桑德伯格 / 中信出版集团2017-08
    读书《不朽的失眠》 张晓风 / 四川人民出版社2018-09
    Uniapp 修改内置组件样式无效解决方法
    Android studio中.9图片的含义及制作教程
    Diff算法
    js new一个对象的过程,实现一个简单的new方法
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7632248.html
Copyright © 2011-2022 走看看