Problem p03686 (C++)

Buggy submission (status: Time Limit Exceeded):
// marico el que lo lea
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> ii;

void fastIO() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

#define FOR(i, f, t) for (int i = f; i < (int)t; i++)
#define FORR(i, f, t) for (int i = f; i > (int)t; i--)
#define FORE(i, c) for (auto i = (c).begin(); i != (c).end(); i++)
#define pb push_back
#define all(obj) obj.begin(), obj.end()
#define ms(obj, val) memset(obj, val, sizeof(obj))
#define ms2(obj, val, sz) memset(obj, val, sizeof(obj[0]) * sz)

#define fst first
#define snd second

template <typename T, typename U> inline void mnze(T &x, U y) {
  if (y < x)
    x = y;
}
template <typename T, typename U> inline void mxze(T &x, U y) {
  if (x < y)
    x = y;
}

void _scan(int &x) { scanf("%d", &x); }
void _scan(long long &x) { scanf("%lld", &x); }
void _scan(double &x) { scanf("%lf", &x); }
void _scan(char &x) { scanf(" %c", &x); }
void _scan(char *x) { scanf("%s", x); }
void scan() {}
template <typename T, typename... U> void scan(T &head, U &...tail) {
  _scan(head);
  scan(tail...);
}

template <typename T> void _dbg(const char *sdbg, T h) {
  cerr << sdbg << "=" << h << "\n";
}
template <typename T, typename... U> void _dbg(const char *sdbg, T h, U... t) {
  while (*sdbg != ',')
    cerr << *sdbg++;
  cerr << "=" << h << ",";
  _dbg(sdbg + 1, t...);
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x)                                                              \
  {                                                                            \
    {                                                                          \
      cerr << #x << " = ";                                                     \
      FORE(_i, (x)) cerr << *_i << ", ";                                       \
      cerr << "\n";                                                            \
    }                                                                          \
  }
#define debuga(x, sz)                                                          \
  {                                                                            \
    {                                                                          \
      cerr << #x << " = ";                                                     \
      FOR(_i, 0, sz) cerr << x[_i] << ", ";                                    \
      cerr << "\n";                                                            \
    }                                                                          \
  }
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define debuga(x, sz)
#define cerr                                                                   \
  if (0)                                                                       \
  cout
#endif

///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////

struct seg {
  int L, R;
};
const int MAXN = 2e5 + 5;

int n, m;
seg s[MAXN];
vi Rs[MAXN];

ii solve(int p) {
  priority_queue<int> pq;
  FOR(i, p + 1, m + 1) for (auto r : Rs[i]) pq.push(r);
  int lr = 0, rr = 0;
  FORR(i, p, 0) {
    for (auto r : Rs[i])
      pq.push(r);
    if (pq.empty())
      continue;
    int r = pq.top();
    pq.pop();
    lr++;
  }
  for (auto r : Rs[0])
    pq.push(r);
  FORR(i, m, p) {
    while (!pq.empty() && pq.top() > i)
      pq.pop();
    if (pq.empty())
      continue;
    int r = pq.top();
    pq.pop();
    rr++;
  }
  return {lr, rr};
}

int main() {
  scan(n, m);
  FOR(i, 0, n) scan(s[i].L, s[i].R);
  FOR(i, 0, n) Rs[s[i].L].pb(s[i].R);
  int lo = 0, hi = m;
  while (lo < hi) {
    int mi = (lo + hi + 1) >> 1;
    debug(lo, mi, hi);
    ii ret = solve(mi);
    if (ret.fst + ret.snd == n)
      lo = hi = mi;
    else {
      if (ret.fst == mi)
        lo = mi;
      else
        hi = mi - 1;
    }
  }
  ii ret = solve(lo);
  int ans = ret.fst + ret.snd;
  // int ans = 0;
  FOR(p, 0, m) mxze(ans, solve(p).fst + solve(p).snd);
  FOR(p, 0, m) debug(p, solve(p).fst, solve(p).snd);
  printf("%d\n", n - ans);
}

Fixed submission:
// marico el que lo lea
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> ii;

void fastIO() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

#define FOR(i, f, t) for (int i = f; i < (int)t; i++)
#define FORR(i, f, t) for (int i = f; i > (int)t; i--)
#define FORE(i, c) for (auto i = (c).begin(); i != (c).end(); i++)
#define pb push_back
#define all(obj) obj.begin(), obj.end()
#define ms(obj, val) memset(obj, val, sizeof(obj))
#define ms2(obj, val, sz) memset(obj, val, sizeof(obj[0]) * sz)

#define fst first
#define snd second

template <typename T, typename U> inline void mnze(T &x, U y) {
  if (y < x)
    x = y;
}
template <typename T, typename U> inline void mxze(T &x, U y) {
  if (x < y)
    x = y;
}

void _scan(int &x) { scanf("%d", &x); }
void _scan(long long &x) { scanf("%lld", &x); }
void _scan(double &x) { scanf("%lf", &x); }
void _scan(char &x) { scanf(" %c", &x); }
void _scan(char *x) { scanf("%s", x); }
void scan() {}
template <typename T, typename... U> void scan(T &head, U &...tail) {
  _scan(head);
  scan(tail...);
}

template <typename T> void _dbg(const char *sdbg, T h) {
  cerr << sdbg << "=" << h << "\n";
}
template <typename T, typename... U> void _dbg(const char *sdbg, T h, U... t) {
  while (*sdbg != ',')
    cerr << *sdbg++;
  cerr << "=" << h << ",";
  _dbg(sdbg + 1, t...);
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x)                                                              \
  {                                                                            \
    {                                                                          \
      cerr << #x << " = ";                                                     \
      FORE(_i, (x)) cerr << *_i << ", ";                                       \
      cerr << "\n";                                                            \
    }                                                                          \
  }
#define debuga(x, sz)                                                          \
  {                                                                            \
    {                                                                          \
      cerr << #x << " = ";                                                     \
      FOR(_i, 0, sz) cerr << x[_i] << ", ";                                    \
      cerr << "\n";                                                            \
    }                                                                          \
  }
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define debuga(x, sz)
#define cerr                                                                   \
  if (0)                                                                       \
  cout
#endif

///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////

struct seg {
  int L, R;
};
const int MAXN = 2e5 + 5;

int n, m;
seg s[MAXN];
vi Rs[MAXN];

ii solve(int p) {
  priority_queue<int> pq;
  FOR(i, p + 1, m + 1) for (auto r : Rs[i]) pq.push(r);
  int lr = 0, rr = 0;
  FORR(i, p, 0) {
    for (auto r : Rs[i])
      pq.push(r);
    if (pq.empty())
      continue;
    int r = pq.top();
    pq.pop();
    lr++;
  }
  for (auto r : Rs[0])
    pq.push(r);
  FORR(i, m, p) {
    while (!pq.empty() && pq.top() > i)
      pq.pop();
    if (pq.empty())
      continue;
    int r = pq.top();
    pq.pop();
    rr++;
  }
  return {lr, rr};
}

int main() {
  scan(n, m);
  FOR(i, 0, n) scan(s[i].L, s[i].R);
  FOR(i, 0, n) Rs[s[i].L].pb(s[i].R);
  int lo = 0, hi = m;
  while (lo < hi) {
    int mi = (lo + hi + 1) >> 1;
    debug(lo, mi, hi);
    ii ret = solve(mi);
    if (ret.fst + ret.snd == n)
      lo = hi = mi;
    else {
      if (ret.fst == mi)
        lo = mi;
      else
        hi = mi - 1;
    }
  }
  ii ret = solve(lo);
  int ans = ret.fst + ret.snd;
  printf("%d\n", n - ans);
}