A - On Segment's Own Points CodeForces - 397A

Our old friend Alexey has finally entered the University of City N — the Berland capital. Alexey expected his father to get him a place to live in but his father said it was high time for Alexey to practice some financial independence. So, Alexey is living in a dorm.

The dorm has exactly one straight dryer — a 100 centimeter long rope to hang clothes on. The dryer has got a coordinate system installed: the leftmost end of the dryer has coordinate 0, and the opposite end has coordinate 100. Overall, the university has n students. Dean's office allows i-th student to use the segment(li, ri) of the dryer. However, the dean's office actions are contradictory and now one part of the dryer can belong to multiple students!

Alexey don't like when someone touch his clothes. That's why he want make it impossible to someone clothes touch his ones. So Alexey wonders: what is the total length of the parts of the dryer that he may use in a such way that clothes of the others (n - 1) students aren't drying there. Help him! Note that Alexey, as the most respected student, has number 1.

Input

The first line contains a positive integer n (1 ≤ n ≤ 100). The (i + 1)-th line contains integers li and ri (0 ≤ li < ri ≤ 100) — the endpoints of the corresponding segment for the i-th student.

Output

On a single line print a single number k, equal to the sum of lengths of the parts of the dryer which are inside Alexey's segment and are outside all other segments.

Example
Input
3
0 5
2 8
1 6
Output
1
Input
3
0 10
1 5
7 15
Output
3
Note

Note that it's not important are clothes drying on the touching segments (e.g. (0, 1)and (1, 2)) considered to be touching or not because you need to find the length of segments.

In the first test sample Alexey may use the only segment (0, 1). In such case his clothes will not touch clothes on the segments (1, 6) and (2, 8). The length of segment (0, 1) is 1.

In the second test sample Alexey may dry his clothes on segments (0, 1) and (5, 7). Overall length of these segments is 3.

 题目大意:”输入一个数n   随后输入n行,第一行是本身自己所占的区间,下面的几行是其他同学所占的烘干机区间,要求是本身自己所在的区间尽量不被其他同学的烘衣服的区间所影响。求不被影响的区间块数(一个区间1米算是一块)

此题思路有很多中,重在细节上。区间块作为一个有效的值时,会出现很多的问题,判断的时候会较为复杂,我们可以直接标记区间块的末端,就像代码中的#变为*,最后再循环计算有多少*即可。

此题要想简化代码,必须思考如何把标记那部分做好。

首先先附上代码


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
using namespace std;
int a[1001],b[1001];
char c[1001];
int main()
{

    int n;
    scanf("%d",&n);
    int s=n;
    int sum=0;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    for(int i=0; i<n; i++)
    {
        scanf("%d%d",&a[i],&b[i]);
    }
    for(int i=a[0]; i<=b[0]; i++)
        c[i]='#';
    for(int i=1; i<n; i++)
    {
        for(int j=a[i]+1; j<=b[i]; j++)
        {
            c[j]='*';
        }
    }
    for(int i=a[0]+1; i<=b[0]; i++)
    {
if(c[i]=='#')
    sum++;
    }
    printf("%d\n",sum);

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值