首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【Codeforces】351B - Problems for Round(思维,好题)

【Codeforces】351B - Problems for Round(思维,好题)

作者头像
FishWang
发布2025-08-26 20:21:06
发布2025-08-26 20:21:06
1410
举报

题目链接:点击打开题目

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

Examples

input

代码语言:javascript
复制
5 2
1 4
5 2

output

代码语言:javascript
复制
2

input

代码语言:javascript
复制
3 3
1 2
2 3
1 3

output

代码语言:javascript
复制
0

input

代码语言:javascript
复制
3 2
3 1
3 2

output

代码语言:javascript
复制
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.

看了巨巨们的代码目瞪口呆啊,思维这么巧。

有些点需要注意,输入的两个数,大的肯定在2小的在1,如果一个数在第二次出现分到了不同的组,那么在更新 l 和 r 的时候肯定会使 l > r了,这样在最后输出的时候就发现有矛盾并输出0了。

再说明一下对 l 和 r 的更新问题,l 表示 div1 最大难度,r 表示 div2 最小难度。最后的实际方案输就为 div2 [ min ] - div1 [ max ] - 1 + 1 。

自己理解所写出的代码如下:

代码语言:javascript
复制
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define swap(x,y) if(x>y){int t;t=x;x=y;y=t;}
int main()
{
	int n,m;
	int l,r;
	while (~scanf ("%d %d",&n,&m))
	{
		l = 1;
		r = n;
		while (m--)
		{
			int x,y;
			scanf ("%d %d",&x,&y);
			swap(x,y);
			l = max (l,x);
			r = min (r,y);
		}
		printf ("%d\n",max(r-l,0));
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档