【CodeForces 397A 】On Segment's Own Points(水题)

来源:互联网 发布:腊肠犬 知乎 编辑:程序博客网 时间:2024/06/07 18:30

Description

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.

Sample Input

Input

3
0 5
2 8
1 6

Output

1

Input

3
0 10
1 5
7 15

Output

3

题目大意

Alexey新到一个学校,在宿舍有一根晾衣绳,给你一个n表示这个宿舍有几个人,第一行是Alexey想要占据的范围,接下来n-1行表示晾衣绳已被占据的范围,求Alexey在他想要占据范围内可以占据的长度。说白了就是求区间的长度。

思路

如果一个范围(l,r)被占据,那么就从l+1开始循环到r,并标记这些点使vis[i]=0,表示这些点已被占据,为什么要从l+1开始呢,因为这样所标记点的个数刚好就是这个区间的长度,但是这样做必须从1开始存数据,不能从0开始。之后只要跑一遍Alexey的区间,找出有几个点没有被访问输出就可以了。

代码

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <stack>using namespace std;const int maxn=200+5;struct proc{    int l;    int r;}stu[maxn];int n,vis[100];int main(){    while(~scanf("%d",&n))    {        for(int i=1;i<=n;i++)        {            scanf("%d %d",&stu[i].l,&stu[i].r);        }        for(int i=stu[1].l+1;i<=stu[1].r;i++)        {            vis[i]=1;        }        for(int i=2;i<=n;i++)        {            for(int j=stu[i].l+1;j<=stu[i].r;j++)            {                vis[j]=0;            }        }        int ans=0,sta=0,flag=0;        for(int i=stu[1].l+1;i<=stu[1].r;i++)        {            if(vis[i]) ans++;        }        printf("%d\n",ans);    }    return 0;}
0 0
原创粉丝点击