思路:把每一个区间的左端点作为key值排序即可,然后更新l,r,注意这里是包括两端端点的,所以每次更新ans += (l - r + 1),同时注意长为n的路其实有n + 1个点,因为包括第0个点,更新右端点记得用max(r,b),在这里被坑了一发

#include<bits/stdc++.h>
using namespace std;

const int N=1000010;
#define int long long
#define x first
#define y second
typedef long long ll;
typedef pair<int,int> PII;

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	int n,m;
	cin >> n >> m;
	vector<PII> v(m);
	for(int i = 0; i < m; i ++){
		int a,b;
		cin >> a >> b;
		v[i] = {a,b};
	}
	sort(v.begin(),v.end());
	int l = v[0].x,r = v[0].y;

	ll ans = 0;
	for(int i = 1; i < m; i ++){
		int a = v[i].x;
		int b = v[i].y;
		if(a <= r) r = max(r,b);
		else{
//			cout << l << ' ' << r << '\n';
			ans += r - l + 1;
			l = a;
			r = b;
		}
	}
//	cout << l << ' ' << r << '\n';
	ans += (r - l + 1);

	cout << n - ans + 1;
	return 0;
}