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

题意:n道题目区分为两部分 要求两部分不能为空 不能重复 div1的最低难度题目必须高于div2的最高难度     k对相似的题目 必须分在不同的部分 输出分类的方法数

题解: div1的最低难度题目必须高于div2的最高难度   对于k对相似的题目  保持u<v  从低到高记录 难度最高的题目l   从高到低记录难度最低的题目r

若l>r  则无法分类 输出0

若l<r  对于l~r的题目分到两个部分  共有r-l种

 #include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<stack>
#include<map>
#define ll __int64
#define pi acos(-1.0)
using namespace std;
int n,m;
int exm1,exm2;
int l,r;
int main()
{
scanf("%d %d",&n,&m);
l=;
r=n;
for(int i=;i<=m;i++)
{
scanf("%d %d",&exm1,&exm2);
if(exm1>exm2)
swap(exm1,exm2);
l=max(l,exm1);
r=min(r,exm2);
}
if(r-l<)
cout<<""<<endl;
else
cout<<r-l<<endl;
return ;
}

Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) B的更多相关文章

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

    B. Problems for Round 题目连接: http://www.codeforces.com/contest/673/problem/B Description There are n ...

  2. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition)只有A题和B题

    连接在这里,->点击<- A. Bear and Game time limit per test 2 seconds memory limit per test 256 megabyte ...

  3. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D Bear and Two Paths

    题目链接: http://codeforces.com/contest/673/problem/D 题意: 给四个不同点a,b,c,d,求是否能构造出两条哈密顿通路,一条a到b,一条c到d. 题解: ...

  4. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C - Bear and Colors

    题目链接: http://codeforces.com/contest/673/problem/C 题解: 枚举所有的区间,维护一下每种颜色出现的次数,记录一下出现最多且最小的就可以了. 暴力n*n. ...

  5. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D. Bear and Two Paths 构造

    D. Bear and Two Paths 题目连接: http://www.codeforces.com/contest/673/problem/D Description Bearland has ...

  6. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C. Bear and Colors 暴力

    C. Bear and Colors 题目连接: http://www.codeforces.com/contest/673/problem/C Description Bear Limak has ...

  7. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) A. Bear and Game 水题

    A. Bear and Game 题目连接: http://www.codeforces.com/contest/673/problem/A Description Bear Limak likes ...

  8. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition)

    A.暴力枚举,注意游戏最长为90分钟 B.暴力,c[l]++,c[r]--,记录中间有多长的段是大小为n的,注意特判m=0的情况 C.暴力枚举,我居然一开始没想出来!我一直以为每次都要统计最大的,就要 ...

  9. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D

    D. Bear and Two Paths time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

随机推荐

  1. Drupal 网站漏洞修复以及网站安全防护加固方法

    drupal是目前网站系统使用较多一个开源PHP管理系统,架构使用的是php环境+mysql数据库的环境配置,drupal的代码开发较为严谨,安全性较高,但是再安全的网站系统,也会出现网站漏洞,dru ...

  2. unity独立游戏开发日志2018/09/26

    最近太忙,今天吃饭的时候灵感一现...想到了随机地图生成的方法,不过可能实现的比较笨...还需要优化,大佬绕过. 注释没打,最后统一解释. using System.Collections; usin ...

  3. stm32+lwip(四):网页服务器测试

    我是卓波,很高兴你来看我的博客. 系列文章: stm32+lwip(一):使用STM32CubeMX生成项目 stm32+lwip(二):UDP测试 stm32+lwip(三):TCP测试 stm32 ...

  4. IDLE激活方法

    激活流程 一.通过Activation code 方式激活, 注册码获取地址为:http://idea.lanyus.com/ 在idea或者pycharm的Activation code中输入 注册 ...

  5. L009文件属性知识详解小节

    本堂课分为5部分内容 1.linux下重要目录详解 2.PATH变量路径内容 3.linux系统中文件类型介绍 4.linux系统中文件属性详细介绍 5.linux系统文件属性inode与block知 ...

  6. 虚拟现实-VR-UE4-LEAP-Motion手势识别

    点击打开链接今天到手一个新东西,LEAP手势识别仪. 关于LEAP Leap是一家面向PC以及Mac的体感控制器制造公司. 具体信息百度百科http://baike.baidu.com/link?ur ...

  7. Spring实战第九章学习笔记————保护Web应用

    保护Web应用 在这一章我们将使用切面技术来探索保护应用程序的方式.不过我们不必自己开发这些切面----我们将使用Spring Security,一种基于Spring AOP和Servlet规范的Fi ...

  8. 剑指offer-链表中倒数第K个结点14

    题目描述 输入一个链表,输出该链表中倒数第k个结点. class Solution: def FindKthToTail(self, head, k): # write code here res=[ ...

  9. Leetcode 684.冗余连接

    冗余连接 在本问题中, 树指的是一个连通且无环的无向图. 输入一个图,该图由一个有着N个节点 (节点值不重复1, 2, ..., N) 的树及一条附加的边构成.附加的边的两个顶点包含在1到N中间,这条 ...

  10. Python 3 学习笔记之——变量作用域、模块和包

    1. 变量作用域 Python 中,程序的变量并不是在哪个位置都可以访问的,访问权限决定于这个变量是在哪里赋值的.变量的作用域决定了在哪一部分程序可以访问哪个特定的变量名称.Python 的作用域一共 ...