Problem p03686 (C++)

Buggy submission (status: Runtime Error):

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

int n;
int m;

#define MAX 200002

vector<pair<int, int>> v;

bool cmp(pair<int, int> a, pair<int, int> b) { return a.first > b.first; }
deque<int> d;
int L() {
  for (int i = 0; i < v.size(); i++) {
    d.push_back(v[i].first);
  }
  sort(d.begin(), d.end());
  int siz = 0;
  for (int i = m; i >= 1; i--) {
    if (d.back() >= i) {
      d.pop_back();
      siz++;
    }
  }
  return siz;
}
int R() {
  d.clear();
  for (int i = 0; i < v.size(); i++) {
    d.push_back(v[i].second);
  }
  sort(d.begin(), d.end());
  int siz = 0;
  for (int i = m; i >= 1; i--) {
    while (d.size() && d.back() > i) {
      d.pop_back();
    }
    if (d.size()) {
      d.pop_back();
      siz++;
    }
  }
  return siz;
}
deque<pair<int, int>> dd;
multiset<int> mt;
int calc(int Len) {
  dd.clear();
  mt.clear();
  for (int i = 0; i < v.size(); i++) {
    dd.push_back(make_pair(v[i].second, v[i].first));
  }
  sort(dd.begin(), dd.end());
  int sz = 0;
  for (int i = Len + 1; i <= m; i++) {
    while (dd.size() && dd.front().first <= i) {
      mt.insert(dd.front().second);
      dd.pop_front();
    }
    if (mt.size()) {
      auto f = (mt.begin());
      mt.erase(f);
      sz++;
    }
  }
  while (dd.size()) {
    mt.insert(dd.front().second);
    dd.pop_front();
  }
  for (int i = Len; i >= 1; i--) {
    if (mt.size()) {
      int k = (*mt.rbegin());
      if (k >= i) {
        mt.erase(mt.lower_bound(k));
        sz++;
      }
    }
  }
  return sz;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    v.push_back(make_pair(a, b));
  }
  int szl = L();
  int szr = R();
  vector<int> S;
  S.push_back(0);
  S.push_back(m);
  S.push_back(szl);
  S.push_back(max(0, szl - 1));
  S.push_back(min(m, szl + 1));
  S.push_back(m - szr);
  S.push_back(max(0, m - szr - 1));
  int ans = 0;
  for (int i = 0; i < S.size(); i++) {
    int go = calc(S[i]);
    ans = max(ans, go);
  }
  cout << n - ans << endl;
  return 0;
}

Fixed submission:

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

int n;
int m;

#define MAX 200002

vector<pair<int, int>> v;

bool cmp(pair<int, int> a, pair<int, int> b) { return a.first > b.first; }
deque<int> d;
int L() {
  for (int i = 0; i < v.size(); i++) {
    d.push_back(v[i].first);
  }
  sort(d.begin(), d.end());
  int siz = 0;
  for (int i = m; i >= 1; i--) {
    if (d.size() && d.back() >= i) {
      d.pop_back();
      siz++;
    }
  }
  return siz;
}
int R() {
  d.clear();
  for (int i = 0; i < v.size(); i++) {
    d.push_back(v[i].second);
  }
  sort(d.begin(), d.end());
  int siz = 0;
  for (int i = m; i >= 1; i--) {
    while (d.size() && d.back() > i) {
      d.pop_back();
    }
    if (d.size()) {
      d.pop_back();
      siz++;
    }
  }
  return siz;
}
deque<pair<int, int>> dd;
multiset<int> mt;
int calc(int Len) {
  dd.clear();
  mt.clear();
  for (int i = 0; i < v.size(); i++) {
    dd.push_back(make_pair(v[i].second, v[i].first));
  }
  sort(dd.begin(), dd.end());
  int sz = 0;
  for (int i = Len + 1; i <= m; i++) {
    while (dd.size() && dd.front().first <= i) {
      mt.insert(dd.front().second);
      dd.pop_front();
    }
    if (mt.size()) {
      auto f = (mt.begin());
      mt.erase(f);
      sz++;
    }
  }
  while (dd.size()) {
    mt.insert(dd.front().second);
    dd.pop_front();
  }
  for (int i = Len; i >= 1; i--) {
    if (mt.size()) {
      int k = (*mt.rbegin());
      if (k >= i) {
        mt.erase(mt.lower_bound(k));
        sz++;
      }
    }
  }
  return sz;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    v.push_back(make_pair(a, b));
  }
  int szl = L();
  int szr = R();
  vector<int> S;
  S.push_back(0);
  S.push_back(m);
  S.push_back(szl);
  S.push_back(max(0, szl - 1));
  S.push_back(min(m, szl + 1));
  S.push_back(m - szr);
  S.push_back(max(0, m - szr - 1));
  int ans = 0;
  for (int i = 0; i < S.size(); i++) {
    int go = calc(S[i]);
    ans = max(ans, go);
  }
  cout << n - ans << endl;
  return 0;
}