Problem p03203 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <set>
#include <vector>
using namespace std;
#define sz(a) ((int)(a).size())
#define pb push_back
#define mp make_pair
#define fs first
#define sc second

const int N = (int)3e5 + 10;
int h, w, n, x, y;
vector<int> bad[N];

int main() {
  scanf("%d%d%d", &h, &w, &n);
  for (int i = 0; i < h; ++i) {
    bad[i].pb(0);
    bad[i].pb(w + 1);
  }
  for (int i = 1; i <= w; ++i) {
    bad[h + 1].pb(i);
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d%d", &x, &y);
    bad[x].pb(y);
  }
  vector<pair<int, int>> good;
  good.pb(mp(1, 0));
  for (int x = 1; x <= h; ++x) {
    {
      for (int i = 0; i < sz(good); ++i) {
        good[i].sc = min(good[i].sc + 1, w);
      }
      vector<pair<int, int>> ngood;
      for (int i = 0; i < sz(good);) {
        int j = i;
        while ((j + 1 < sz(good)) && (good[j + 1].fs == good[j].sc + 1)) {
          ++j;
        }
        ngood.pb(mp(good[i].fs, good[j].sc));
        i = j + 1;
      }
      good = ngood;
    }
    {
      vector<pair<int, int>> ngood;
      sort(bad[x].begin(), bad[x].end());
      for (int i = 0, j = 0; i < sz(good); ++i) {
        int l = good[i].fs, r = good[i].sc;
        while (l <= r) {
          while ((j < sz(bad[x])) && (bad[x][j] < good[i].fs)) {
            ++j;
          }
          if ((j < sz(bad[x])) && (bad[x][j] >= l) && (bad[x][j] <= r)) {
            if (l < bad[x][j]) {
              ngood.pb(mp(l, bad[x][j] - 1));
            }
            l = bad[x][j] + 1;
          } else {
            ngood.pb(mp(l, r));
            break;
          }
        }
      }
      good = ngood;
    }
    for (int i = 0; i < sz(bad[x + 1]); ++i) {
      int y = bad[x + 1][i];
      int it =
          upper_bound(good.begin(), good.end(), mp(y, -1)) - good.begin() - 1;
      if ((it >= 0) && (good[it].fs <= y) && (y <= good[it].sc)) {
        printf("%d\n", x);
        return 0;
      }
    }
  }
  assert(false);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <set>
#include <vector>
using namespace std;
#define sz(a) ((int)(a).size())
#define pb push_back
#define mp make_pair
#define fs first
#define sc second

const int N = (int)3e5 + 10;
int h, w, n, x, y;
vector<int> bad[N];

int main() {
  scanf("%d%d%d", &h, &w, &n);
  for (int i = 0; i < h; ++i) {
    bad[i].pb(0);
    bad[i].pb(w + 1);
  }
  for (int i = 1; i <= w; ++i) {
    bad[h + 1].pb(i);
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d%d", &x, &y);
    bad[x].pb(y);
  }
  vector<pair<int, int>> good;
  good.pb(mp(1, 0));
  for (int x = 1; x <= h; ++x) {
    {
      for (int i = 0; i < sz(good); ++i) {
        good[i].sc = min(good[i].sc + 1, w);
      }
      vector<pair<int, int>> ngood;
      for (int i = 0; i < sz(good);) {
        int j = i;
        while ((j + 1 < sz(good)) && (good[j + 1].fs == good[j].sc + 1)) {
          ++j;
        }
        ngood.pb(mp(good[i].fs, good[j].sc));
        i = j + 1;
      }
      good = ngood;
    }
    {
      vector<pair<int, int>> ngood;
      sort(bad[x].begin(), bad[x].end());
      for (int i = 0, j = 0; i < sz(good); ++i) {
        int l = good[i].fs, r = good[i].sc;
        while (l <= r) {
          while ((j < sz(bad[x])) && (bad[x][j] < good[i].fs)) {
            ++j;
          }
          if ((j < sz(bad[x])) && (bad[x][j] >= l) && (bad[x][j] <= r)) {
            if (l < bad[x][j]) {
              ngood.pb(mp(l, bad[x][j] - 1));
            }
            l = bad[x][j] + 1;
          } else {
            ngood.pb(mp(l, r));
            break;
          }
        }
      }
      good = ngood;
    }
    for (int i = 0; i < sz(bad[x + 1]); ++i) {
      int y = bad[x + 1][i];
      int it =
          (upper_bound(good.begin(), good.end(), mp(y, w)) - good.begin()) - 1;
      if ((it >= 0) && (good[it].fs <= y) && (y <= good[it].sc)) {
        printf("%d\n", x);
        return 0;
      }
    }
  }
  assert(false);
  return 0;
}