Round #232 (Div. 2)_A

来源:互联网 发布:windows phone官方 编辑:程序博客网 时间:2024/05/22 05:06
A. On Segment's Own Points
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 nstudents. 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 test(s)
input
30 52 81 6
output
1
input
30 101 57 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个可能会覆盖第一个区间的部分,然后让你求剩余区间的大小。

开始时我想的是用一个整型数组A[101]初始化为0,被覆盖的部分区间的整数全部赋值为1,然后统计非零的个数来算出剩余空间的大小,却发现3 [0 4][0 2] [3 4]测试无法解释,后来发现将其改为开区间(左闭右开)就可以解决问题。

这样的话[0 4)就可以区间内,a[0],a[1] ,a[3]值为1,剩余空间为一,问题得解。

这题给我们一个启示,可以通过开闭区间的方式将一个点抽象为一个线段区间,比如说[0, 4),0可以代表[0, 1)这个区间,1可以代表[1, 2)这个区间,这样的话就可以“以点代线”,合理的解决问题而无需分类考虑不同的情况。

以下是 AC代码:

#include <iostream>#include <algorithm>#include <cstdlib>#include <cstring>#include <cmath>using namespace std;int main(){    int n,a,b,i,j;    bool s[110]={false};    cin>>n;    cin>>a>>b;    for(i=1; i<n; i++){        int c,d;        cin>>c>>d;        for(j=c; j<d; j++){            s[j]=true;        }    }    int sum=0;    for(i=a; i<b; i++){        if(!s[i])   sum++;    }    cout<<sum<<endl;    return 0;}



0 0
原创粉丝点击