codeforcs 351 B. Problems for Round

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 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.

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<math.h>
#include<algorithm>
using namespace std;
int min(int x,int y)
{
	if(x<=y)
	return x;
	else 
	return y;
}
int max(int x,int y)
{
	if(x>=y)
	return x;
	else
	return y;
}
int main()
{
	int n,m;
	int a,b;
	int t,x,y;
	
	while(scanf("%d%d",&m,&n)!=EOF)
	{
		x=m,y=1;
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",&a,&b);
			if(a<b)
			{
			t=a;
			a=b;
			b=t;	
			}
			x=min(x,a);
			y=max(y,b);
		}
		if(x-y>=0)
		printf("%d\n",x-y);
		else
		printf("0\n");
	}
 } 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值