Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) B.Problems for Round

来源:互联网 发布:mysql5.7 for mac下载 编辑:程序博客网 时间:2024/04/28 12:20

B. Problems for Round
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 0000 ≤ 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.

Examples
input
5 21 45 2
output
2
input
3 31 22 31 3
output
0
input
3 23 13 2
output
1
题意:把序号1-n难度递增的问题分为两堆,第二堆所有问题的难度小于第一堆,相似的不能再同一堆,问有多少种分法?

思路:给相似两边排序,找出难度小那边的最大难度和难度大那边的最小难度,中间的就是可移动的题目,最小难度都大于最大难度,则不可分割。

#include <stdio.h>#include <algorithm>#include <math.h>#include <iostream>using namespace std;#define maxn 100005int a[maxn];int b[maxn];int ans=0;int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        ans=0;        if(m==0)        {            printf("%d\n",n-1);            continue;        }        for(int i=0;i<m;i++)        {            scanf("%d%d",&a[i],&b[i]);            if(a[i]>b[i])                swap(a[i],b[i]);        }        sort(a,a+m);        sort(b,b+m);        if(a[m-1]>=b[0])        {            printf("0\n");            continue;        }          ans=b[0]-a[m-1];        printf("%d\n",ans);    }    return 0;}


1 0