CodeForces

来源:互联网 发布:网络搜索引擎 007 编辑:程序博客网 时间:2024/06/01 23:41

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
Note

In the first sample, problems 1 and 2 should be used in division 2, while problems 4 and 5 in division 1. Problem 3 may be used either in division 1 or in division 2.

In the second sample, all pairs of problems are similar and there is no way to split problem between two divisions without breaking any rules.

Third sample reminds you that the similarity relation is not transitive. Problem 3 is similar to both 1 and 2, but 1 is not similar to 2, so they may be used together.

题意:cf出题,要求Div1的题要比Div2的题难,且相似的题目不能出在同以套题里。给出n道题,数字越大代表题目难度越大,给出m个相似关系。问能出多少种题。

思路:根据给出的关系找出Div1的最小数和Div2的最大数。

#include<stdio.h>#include<algorithm>using namespace std;int main(){    int n,m,MAX,MIN;    while(scanf("%d%d",&n,&m)!=EOF)    {        int MAX=1;        int MIN=n;        while(m--)        {            int x,y;            scanf("%d%d",&x,&y);            MAX=max(MAX,min(x,y));            MIN=min(MIN,max(x,y));        }        if(MIN-MAX>0)            printf("%d\n",MIN-MAX);//先把未分配的数都分到Div1 然后从Div1从小到大往Div2拿。        else            printf("0\n");    }}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 雪纺衬衫起静电怎么办 吃烤饼不松软是怎么办 1岁宝宝睡眠不好怎么办 3岁幼儿睡眠不好怎么办 2岁幼儿睡眠不好怎么办 2岁宝宝睡眠不好怎么办 9岁儿童睡眠不好怎么办 3岁宝宝老踢被子怎么办 4岁宝宝老踢被子怎么办 四线锁边机跳线怎么办 引流管伤口洞红怎么办 甘蔗卡在喉咙里怎么办 棉花被子生虫了怎么办 绗缝羽绒服钻毛怎么办 宝珠笔没墨水了怎么办 衣服上画的笔印怎么办 黑笔芯弄衣服上怎么办 圆珠笔油在皮上怎么办 不小心吞了水银怎么办 小孩吃了洗发露怎么办? 脸上被铅笔戳了怎么办 小孩吃了铅笔芯怎么办 小孩把橡皮吃了怎么办 用棉签掏耳朵里面疼怎么办 棉签头掉耳朵里怎么办 黑裤子老是粘毛怎么办 新买的裤子掉色怎么办 黑裤子容易粘毛怎么办 裤子粘了全部毛怎么办 纯棉裤子粘毛了怎么办 裤子粘毛怎么办怎么洗 黑裤子洗白了怎么办 新买床单有味道怎么办 新买的床单扎人怎么办 刚买的衣服皱了怎么办 橘子沾到衣服上怎么办 橘子水掉衣服上怎么办 菜汁滴在衣服上怎么办 饺子面和硬了怎么办 化纤衣服烫亮了怎么办 衣服穿久了发亮怎么办