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

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种

 1 #include<bits/stdc++.h>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<queue>
 6 #include<stack>
 7 #include<map>
 8 #define ll __int64
 9 #define pi acos(-1.0)
10 using namespace std;
11 int n,m;
12 int exm1,exm2;
13 int l,r;
14 int main()
15 {
16     scanf("%d %d",&n,&m);
17     l=1;
18     r=n;
19     for(int i=1;i<=m;i++)
20     {
21         scanf("%d %d",&exm1,&exm2);
22         if(exm1>exm2)
23         swap(exm1,exm2);
24         l=max(l,exm1);
25         r=min(r,exm2);    
26     }
27     if(r-l<0)
28     cout<<"0"<<endl;
29     else
30     cout<<r-l<<endl;
31     return 0;
32 }
原文地址:https://www.cnblogs.com/hsd-/p/5469651.html