CSES - Shared codeLink to this code:
https://cses.fi/paste/4d6196360ee8fd8e21094e/
//#pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fastio() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define endl '\n'
#define all(x) (x).begin(), (x).end() //greater<int>()
#define ll long long
#define PB push_back
#define MP make_pair
#define ff first
#define ss second
#define in insert
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)(x).size()
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define fr(n) for (int i = 0; i < n; i++)
#define fr1(n) for (int i = 1; i <= n; i++)
#define frj(n) for (int j = 0; j < n; j++)
#define frj1(n) for (int j = 1; j <= n; j++)
#define fi(a, b, v) for (int i = (a); i <= (b); i += v)
#define fj(a, b, v) for (int j = (a); j <= (b); j += v)
#define fir(a, b, v) for (int i = (a); i >= (b); i -= v)
#define fjr(a, b, v) for (int j = (a); j >= (b); j -= v)
#define trav(v) for (auto x : v)
#define LCM(a, b) (a / __gcd(a, b)) * b
#define GCD(a, b) __gcd(a, b)
#define UNIQUE(a) sort(all(a)), a.erase(unique(all(a)), a.end())
#define clock_start auto time_req = clock()
#define clock_stop cerr << "\nSolved, time = " << (float)(clock() - time_req) / CLOCKS_PER_SEC << "s\n"
#define testcase \
int t; \
cin >> t; \
while (t--)
typedef pair<int, int> PI;
typedef vector<int> VI;
typedef vector<ll> VLL;
typedef vector<string> VS;
typedef vector<PI> VII;
typedef unsigned long long ull;
typedef long double lld;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll mod = 1000000007;
const int N = 1e3 + 5;
int main()
{
clock_start;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
fastio();
int n, k, c = 0;
cin >> n >> k;
int tk = k;
VII ar(n);
fr(n)
{
cin >> ar[i].ss;
cin >> ar[i].ff;
}
sort(all(ar));
// debug(ar);
queue<int> q;
fr(n)
{
if (!q.empty() && q.front() <= ar[i].ss)
{
// cout << "pop ->" << q.front() << " " << i << endl;
q.pop();
tk++;
}
if (tk > 0)
{
c++;
tk--;
// cout << "push ->" << ar[i].ff << endl;
q.push(ar[i].ff);
}
// cout << tk << endl;
}
cout << c << endl;
clock_stop;
return 0;
}