Codeforces Round #232 (Div. 2) A

来源:互联网 发布:网站seo分析报告 编辑:程序博客网 时间:2024/06/13 00:56
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 coordinate0, and the opposite end has coordinate 100. Overall, the university has n students. Dean's office allowsi-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 thei-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.

#include <cstdio>#include <iostream>#include <cmath>#include <string>#include <cstring>#include <cstdlib>#include <algorithm>#include <stack>#include <map>#include <set>#include <vector>#include <queue>//#define ll long longusing namespace std;const int INF = 0x3f3f3f3f;const double PI = acos(-1.0);const int maxn=100+2;bool vis[maxn];int n,l,r;int ll,rr;int main(){//freopen("input.txt","r",stdin);while(~scanf("%d",&n)){memset(vis,false,sizeof(vis));cin>>l>>r;int num=r-l;for(int i=l;i<r;i++)vis[i]=true;for(int i=0;i<n;i++){cin>>ll>>rr;if(rr<=l||ll>=r) continue;for(int j=ll;j<rr;j++){if(j>=r) break;if(vis[j]==true){vis[j]=false;num--;}}}cout<<num<<endl;}return 0;}

0 0
原创粉丝点击