• 【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;
    }


  • 相关阅读:
    scala学习笔记1(表达式)
    TDD实践感悟
    Day 21:Docker 入门教程
    人类创造未来的思想先锋:这些 TED 演示深深震撼着我们
    Android开源项目第二篇——工具库篇
    提交表单
    MVC html.beginform & ajax.beginform
    MVC中的传参并在View中获取
    HTTP 教程
    ID和Name
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7632248.html
Copyright © 2020-2023  润新知