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
5 2 1 4 5 2
2
3 3 1 2 2 3 1 3
0
3 2 3 1 3 2
1
题目分析:
给出n道题,相同难度的题不能分在同一组,且题的难度必须要逐渐递增,给出m对题,每对题难度相同,求共有多少种分法,:
解题思路:
因为要把n道题共分进两组中,且题的难度要递增,所以先把相似题难度最小的一组,最大的一组,最大一组的最小值减去最小一组的最大值即为种数:
解题代码:
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; int main() { int n,m; while(~scanf("%d%d",&n,&m)) { int first=1,end=n, x[1000100],y[1000010]; for(int i=0;i<m;i++) { scanf("%d%d",&x[i],&y[i]); if(x[i]>y[i]) swap(x[i],y[i]); if(first<x[i]) first=x[i]; if(end>y[i]) end=y[i]; } if(end-first>=0) printf("%d\n",end-first); else printf("0\n"); } return 0; }