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

Sample Input

5 2
1 4
5 2

Sample Output

2

题意

有n道题,他们的编号就是他们的难度,你需要把这些题目分到div1和div2去

div1的题目难度都应该比div2高

现在给你m个关系,a[i],b[i]表示,a[i]和b[i]应该在不同的div

问你一共有多少种分类的方式

题解:

枚举每一个位置,前面的全部扔到div2去,后面的全部扔到div1去

看是否合法就好了

前面的不能有div1的题目,后面的不能有div2的,就检查这个就好了

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+6;
int a[maxn],b[maxn],flag[maxn],n,m,ma[maxn],mi[maxn];
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&a[i],&b[i]);
        if(a[i]>b[i])swap(a[i],b[i]);
        if(flag[a[i]]==2)return puts("0");
        if(flag[b[i]]==1)return puts("0");
        flag[a[i]]=1,flag[b[i]]=2;
    }
    for(int i=1;i<=n+1;i++)
        ma[i]=0,mi[i]=100;
    for(int i=1;i<=n;i++)
        ma[i]=max(ma[i-1],flag[i]);
    for(int i=n;i>=1;i--)
    {
        if(flag[i]==0)mi[i]=mi[i+1];
        else
        {
            if(mi[i+1]==100)mi[i]=flag[i];
            else mi[i]=min(flag[i],mi[i+1]);
        }
    }
    int ans = 0;
    for(int i=1;i<n;i++)
        if(ma[i]<=1&&mi[i+1]>=2)ans++;
    cout<<ans<<endl;
}

你可能感兴趣的:(Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) B. Problems for Round 水题)