Language: C++

//include
//-------------------------
#include<vector>
#include<list>
#include<map>
#include<set>
#include<queue>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<string>
#include<cstring>
#include<ctime>

using namespace std;

//conversion
//-----------------------
inline int toInt(string s) { int v;istringstream sin(s);sin >> v;return v; }
template<class T>inline string toString(T x) { ostringstream sout;sout << x;return sout.str(); }

//math
//-----------------------
template<class T>inline T sqr(T x) { return x * x; }

//using
//-----------------------
using VI = vector<int>;
using VVI = vector<VI>;
using VS = vector<string>;
using PII = pair<int, int>;
using VPII = vector<PII>;
using TIII = tuple<int, int, int>;
using VTIII = vector<TIII>;
using LL = long long;

//function
enum priority_code {
	fs,
	sf,
};
template<class T = void, priority_code t = fs>
struct greater_pair {
	constexpr bool operator()(const T& _Left, const T& _Right)const {
		if (t == sf)
			return (_Left.second > _Right.second ||
			(!(_Right.second > _Left.second) && _Left.first > _Right.first));
		else
			return (_Left.first > _Right.first ||
			(!(_Right.first > _Left.first) && _Left.second > _Right.second));
	}
};
template<class T = void, priority_code t = fs>
struct less_pair {
	constexpr bool operator()(const T& _Left, const T& _Right)const {
		if (t == sf)
			return (_Left.second < _Right.second ||
			(!(_Right.second < _Left.second) && _Left.first < _Right.first));
		else
			return (_Left.first < _Right.first ||
			(!(_Right.first < _Left.first) && _Left.second < _Right.second));
	}
};


//container util
//-----------------------
#define WHOLE(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
#define RWHOLE(f,x,...) ([&](decltype((x)) whole) { return (f)(rbegin(whole), rend(whole), ##__VA_ARGS__); })(x)
#define CWHOLE(f,x,...) ([&](decltype((x)) whole) { return (f)(cbegin(whole), cend(whole), ##__VA_ARGS__); })(x)
#define CRWHOLE(f,x,...) ([&](decltype((x)) whole) { return (f)(crbegin(whole, crend(whole), ##__VA_ARGS__); })(x)
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(decltype((c).begin()) i=(c).begin();i!=(c).end();++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define sort(c) sort((c).begin(),(c).end())
#define RESET(c) int _c=(c).size();(c).clear();(c).resize(_c);
#define EACH_INPUT(x) for(decltype((x).begin()) itr = (x).begin();itr != (x).end();++itr)cin>>*itr
#define EACH_OUTPUT(x) for(decltype((x).begin()) itr = (x).begin();itr != (x).end();++itr)cout<<*itr<<endl


// repetition
//----------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define repeat(i,n) FOR(i,0,n)

//constant
//----------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

//clear memory
#define CLR(a) memset((a).0,sizeof(a))

//debug
//#define DEBUG
#ifdef DEBUG
#define dump(x) cerr<<#x<<"="<<(x)<<endl;
#define dump_pair(x) cerr<<#x<<"=["<<(x).first<<", "<<(x).second<<" ]"<<endl;
#define dump_tuple3(x) cerr<<#x<<"=["<<get<0>((x))<<", "<<get<1>((x))<<", "<<get<2>((x))<<" ]"<<endl;
#define debug(x) cerr<<#x<<"="<<(x)<<"(L"<<__LINE__<<")"<<""<<__FILE__<<endl;
#else
#define dump(x)
#define dump_pair(x)
#define dump_tuple(x)
#define debug(x)
#endif
#define STAY
#ifdef STAY
#define stay cout<<"Push any key and Enter..."; scanf_s("%*s");
#else
#define stay
#endif

int main() {

	int n, m;cin >> n >> m;
	vector<tuple<int, int, int>> border;//position, is end, id
	repeat(i, m) {
		int f, l;cin >> f >> l;
		border.emplace_back(make_tuple(f, 0, i));
		border.emplace_back(make_tuple(l, 1, i));
	}
	sort(border);

	queue<int> id;
	bool visited[200000] = {};
	int res = 0;
	repeat(i, (size(border))) {
		if (!get<1>(border[i])) {
			visited[get<2>(border[i])] = true;
			id.push(get<2>(border[i]));
		}
		else if(!empty(id)){
			while (!empty(id)) {
				visited[id.front()] = 0;
				id.pop();
			}
			++res;
		}

	}
	cout << res << endl;

	stay;
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:122:49: error: 'scanf_s' was not declared in this scope; did you mean 'scanf'?
  122 | #define stay cout<<"Push any key and Enter..."; scanf_s("%*s");
      |                                                 ^~~~~~~
a.cc:157:9: note: in expansion of macro 'stay'
  157 |         stay;
      |         ^~~~