Problem p03686 (C++)

Buggy submission (status: Runtime Error):
// By Hacheylight

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
// #define int long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(it, v) for (auto it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define SC(t, x) static_cast<t>(x)
#define ub upper_bound
#define lb lower_bound
#define pqueue priority_queue
#define mp make_pair
#define pb push_back
#define pof pop_front
#define pob pop_back
#define fi first
#define se second
#define y1 y1_
#define Pi acos(-1.0)
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(x) freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout)
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, pii> tri;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef queue<int> qi;
typedef queue<pii> qii;
typedef set<int> si;
typedef map<int, int> mii;
typedef map<string, int> msi;
const int N = 100010;
const int INF = 0x3f3f3f3f;
const int iinf = 1 << 30;
const ll linf = 2e18;
const int mod = 1000000007;
const double eps = 1e-7;
void douout(double x) { printf("%lf\n", x + 0.0000000001); }
template <class T> void print(T a) {
  cout << a << endl;
  exit(0);
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
void add(int &a, int b) { a = a + b < mod ? a + b : a + b - mod; }
void sub(int &a, int b) { a = (a - b + mod) % mod; }
void mul(int &a, int b) { a = (ll)a * b % mod; }
int addv(int a, int b) { return (a += b) >= mod ? a -= mod : a; }
int subv(int a, int b) { return (a -= b) < 0 ? a += mod : a; }
int mulv(int a, int b) { return (ll)a * b % mod; }
int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int pw(int a, int b) {
  int s = 1;
  for (; b; b >>= 1, a = (ll)a * a % mod)
    if (b & 1)
      s = (ll)s * a % mod;
  return s;
}

int n, m, ans;
vi tmp;

struct point {
  int l, r;
  bool operator<(const point &a) const { return l != a.l ? l < a.l : r < a.r; }
} a[N];

pqueue<int, vi, greater<int>> q; // 升序

signed main() {
  scanf("%d%d", &n, &m);
  rep(i, 1, n) scanf("%d%d", &a[i].l, &a[i].r);
  sort(a + 1, a + n + 1);
  int hd = 1, tl = m;
  //	cout<<"---\n";
  //	rep(i, 1, n) printf("%d %d\n", a[i].l, a[i].r) ;
  //	cout<<"---\n";
  rep(i, 1, n) {
    q.push(a[i].r);
    if (hd <= tl && hd <= a[i].l)
      hd++;
    else {
      tmp.pb(q.top());
      q.pop();
    }
    //	cout << i << " " << hd << " " << tl << endl ;
  }
  //	cout<<"&&&\n";
  //	rep(i, 0, siz(tmp) - 1) printf("%d ", tmp[i]) ;
  //	cout<<"&&&\n";
  sort(all(tmp));
  per(i, siz(tmp) - 1, 0) {
    if (hd <= tl && tmp[i] <= tl)
      tl--;
    else
      ans++;
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
// By Hacheylight

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
// #define int long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(it, v) for (auto it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define SC(t, x) static_cast<t>(x)
#define ub upper_bound
#define lb lower_bound
#define pqueue priority_queue
#define mp make_pair
#define pb push_back
#define pof pop_front
#define pob pop_back
#define fi first
#define se second
#define y1 y1_
#define Pi acos(-1.0)
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(x) freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout)
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, pii> tri;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef queue<int> qi;
typedef queue<pii> qii;
typedef set<int> si;
typedef map<int, int> mii;
typedef map<string, int> msi;
const int N = 200010;
const int INF = 0x3f3f3f3f;
const int iinf = 1 << 30;
const ll linf = 2e18;
const int mod = 1000000007;
const double eps = 1e-7;
void douout(double x) { printf("%lf\n", x + 0.0000000001); }
template <class T> void print(T a) {
  cout << a << endl;
  exit(0);
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
void add(int &a, int b) { a = a + b < mod ? a + b : a + b - mod; }
void sub(int &a, int b) { a = (a - b + mod) % mod; }
void mul(int &a, int b) { a = (ll)a * b % mod; }
int addv(int a, int b) { return (a += b) >= mod ? a -= mod : a; }
int subv(int a, int b) { return (a -= b) < 0 ? a += mod : a; }
int mulv(int a, int b) { return (ll)a * b % mod; }
int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int pw(int a, int b) {
  int s = 1;
  for (; b; b >>= 1, a = (ll)a * a % mod)
    if (b & 1)
      s = (ll)s * a % mod;
  return s;
}

int n, m, ans;
vi tmp;

struct point {
  int l, r;
  bool operator<(const point &a) const { return l != a.l ? l < a.l : r < a.r; }
} a[N];

pqueue<int, vi, greater<int>> q; // 升序

signed main() {
  scanf("%d%d", &n, &m);
  rep(i, 1, n) scanf("%d%d", &a[i].l, &a[i].r);
  sort(a + 1, a + n + 1);
  int hd = 1, tl = m;
  //	cout<<"---\n";
  //	rep(i, 1, n) printf("%d %d\n", a[i].l, a[i].r) ;
  //	cout<<"---\n";
  rep(i, 1, n) {
    q.push(a[i].r);
    if (hd <= tl && hd <= a[i].l)
      hd++;
    else {
      tmp.pb(q.top());
      q.pop();
    }
    //	cout << i << " " << hd << " " << tl << endl ;
  }
  //	cout<<"&&&\n";
  //	rep(i, 0, siz(tmp) - 1) printf("%d ", tmp[i]) ;
  //	cout<<"&&&\n";
  sort(all(tmp));
  per(i, siz(tmp) - 1, 0) {
    if (hd <= tl && tmp[i] <= tl)
      tl--;
    else
      ans++;
  }
  printf("%d\n", ans);
  return 0;
}