Description
There are n problems prepared for the next Codeforces round. They are arranged in ascending order by their difficulty, and no two problems have the same difficulty. Moreover, there are m pairs of similar problems. Authors want to split problems between two division according to the following rules:
Problemset of each division should be non-empty.
Each problem should be used in exactly one division (yes, it is unusual requirement).
Each problem used in division 1 should be harder than any problem used in division 2.
If two problems are similar, they should be used in different divisions.
Your goal is count the number of ways to split problem between two divisions and satisfy all the rules. Two ways to split problems are considered to be different if there is at least one problem that belongs to division 1 in one of them and to division 2 in the other.
Note, that the relation of similarity is not transitive. That is, if problem i is similar to problem j and problem j is similar to problem k, it doesn't follow that i is similar to k.
Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 0 ≤ m ≤ 100 000) — the number of problems prepared for the round and the number of pairs of similar problems, respectively.
Each of the following m lines contains a pair of similar problems ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi). It's guaranteed, that no pair of problems meets twice in the input.
Output
Print one integer — the number of ways to split problems in two divisions.
Sample Input
Input
5 2
1 4
5 2
Output
2
Input
3 3
1 2
2 3
1 3
Output
0
Input
3 2
3 1
3 2
Output
There are n problems prepared for the next Codeforces round. They are arranged in ascending order by their difficulty, and no two problems have the same difficulty. Moreover, there are m pairs of similar problems. Authors want to split problems between two division according to the following rules:
Problemset of each division should be non-empty.
Each problem should be used in exactly one division (yes, it is unusual requirement).
Each problem used in division 1 should be harder than any problem used in division 2.
If two problems are similar, they should be used in different divisions.
Your goal is count the number of ways to split problem between two divisions and satisfy all the rules. Two ways to split problems are considered to be different if there is at least one problem that belongs to division 1 in one of them and to division 2 in the other.
Note, that the relation of similarity is not transitive. That is, if problem i is similar to problem j and problem j is similar to problem k, it doesn't follow that i is similar to k.
Input
The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 0 ≤ m ≤ 100 000) — the number of problems prepared for the round and the number of pairs of similar problems, respectively.
Each of the following m lines contains a pair of similar problems ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi). It's guaranteed, that no pair of problems meets twice in the input.
Output
Print one integer — the number of ways to split problems in two divisions.
Sample Input
Input
5 2
1 4
5 2
Output
2
Input
3 3
1 2
2 3
1 3
Output
0
Input
3 2
3 1
3 2
Output
1
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
int max(int a,int b)
{
return a>b?a:b;
}
int min(int a,int b)
{
return a<b?a:b;
}
using namespace std;
int main()
{
int n,m,i;
while(scanf("%d%d",&n,&m)!=EOF)
{
int mi=n,ma=1,sum,x,y;
for(i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
int t;
if(x>y)
{
t=x;
x=y;
y=t;
}
ma=max(ma,x);
mi=min(mi,y);
}
sum=max(mi-ma,0);
printf("%d\n",sum);
}
return 0;
}
题意:oj上出一套题,分为两个难度,告诉你一共n道题,还告诉你有哪些题相似。相似的题不能放在相同难度中而且第二难度的题总比第一难度题大。问总共有多少种分法。
思路:对给出相似的题分类,小的在第一难度,大的在第二难度。最后找出第一难度最大值还有第二难度最小值,相减就得出结果了。注意有些数据中会出现第二难度最小小于第一难度最大,所以最后还要和0判断下大小值。(因为懒癌,从另一个文件中粘for循环时忘记把字母换了,错了两次 还以为是格式错误…………)