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


  • 相关阅读:
    AI芯片:高性能卷积计算中的数据复用
    矩阵乘法加速器的设计框架
    NVDLA中Winograd卷积的设计
    神经网络加速器应用实例:图像分类
    Simple TPU的设计和性能评估
    TPU中的指令并行和数据并行
    TPU中的脉动阵列及其实现
    动手写一个简单版的谷歌TPU
    利用Xilinx HLS实现LDPC译码器
    FPGA上如何求32个输入的最大值和次大值:分治
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7632248.html
Copyright © 2011-2022 走看看