prithivMLmods/Coder-Stat huggingface dataset:
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  int l = 0, r = N;
  while (r - l > 1) {
    int c = (l + r) / 2;
    map<int, int> mp;
    for (int i = 1; i < N; i++) {
      if (A[i] >= 100000)
        continue;
      if (A[i - 1] >= A[i]) {
        int t = A[i];
        mp[t]++;
        auto it = mp.upper_bound(t);
        mp.erase(it, mp.end());
        while (t > 0 && mp[t] == c) {
          mp[t] = 0;
          mp[t - 1]++;
          t--;
        }
      }
    }
    if (mp[0] == 0)
      r = c;
    else
      l = c;
  }
  cout << r << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  int l = 0, r = N;
  while (r - l > 1) {
    int c = (l + r) / 2;
    map<int, int> mp;
    for (int i = 1; i < N; i++) {
      if (A[i] >= 100000)
        continue;
      if (A[i - 1] >= A[i]) {
        int t = A[i];
        mp[t]++;
        auto it = mp.upper_bound(t);
        mp.erase(it, mp.end());
        while (t > 0 && mp[t] == c) {
          mp[t] = 0;
          mp[t - 1]++;
          t--;
        }
      }
      if (mp[0] > 0)
        break;
    }
    if (mp[0] == 0)
      r = c;
    else
      l = c;
  }
  cout << r << endl;
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5;
int n, a[maxn + 3];

bool check(int k) {
  map<int, int> M;
  for (int i = 1; i <= n; i++) {
    if (a[i] <= a[i - 1]) {
      while (!M.empty() && M.rbegin()->first > a[i]) {
        map<int, int>::iterator it = M.end();
        M.erase(--it);
      }
      for (int j = a[i]; j >= 0; j--) {
        if (j == 0) {
          return false;
        }
        if (++M[j] == k) {
          M[j] = 0;
        } else {
          break;
        }
      }
    }
  }
  return true;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int l = 1, r = n, mid;
  while (l < r) {
    mid = (l + r) >> 1;
    if (check(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  printf("%d\n", l);
  return 0;
}

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

const int maxn = 2e5;
int n, a[maxn + 3];

bool check(int k) {
  map<int, int> M;
  for (int i = 1; i <= n; i++) {
    if (a[i] <= a[i - 1]) {
      if (k == 1) {
        return false;
      }
      while (!M.empty() && M.rbegin()->first > a[i]) {
        map<int, int>::iterator it = M.end();
        M.erase(--it);
      }
      for (int j = a[i]; j >= 0; j--) {
        if (j == 0) {
          return false;
        }
        if (++M[j] == k) {
          M[j] = 0;
        } else {
          break;
        }
      }
    }
  }
  return true;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int l = 1, r = n, mid;
  while (l < r) {
    mid = (l + r) >> 1;
    if (check(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  printf("%d\n", l);
  return 0;
}
Problem p03202 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 2e5 + 10;

int N, A[MAX_N];

bool check(int sigma) {
  static pair<int, int> stk[MAX_N];
  int top = 0;
  for (int i = 1; i <= N; i++) {
    int delta = A[i] - A[i - 1];
    if (delta > 0) {
      stk[++top] = make_pair(1, delta);
    } else {
      while (delta) {
        if (delta + stk[top].second <= 0) {
          delta += stk[top].second;
          top--;
        } else {
          stk[top].second += delta;
          delta = 0;
        }
      }
      int popped = 0;
      while (top && stk[top].first == sigma) {
        popped += stk[top].second;
        top--;
      }
      if (!top)
        return false;
      if (stk[top].second != 1) {
        stk[top + 1] = make_pair(stk[top].first, 1);
        stk[top].second--;
        top++;
      }
      stk[top].first++;
      stk[++top] = make_pair(1, popped);
    }
  }
  return true;
}

int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++)
    scanf("%d", &A[i]);
  int l = 1, r = N;
  while (l != r) {
    int m = (l + r) / 2;
    if (check(m))
      r = m;
    else
      l = m + 1;
  }
  printf("%d\n", l);
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 2e5 + 10;

int N, A[MAX_N];

bool check(int sigma) {
  static pair<int, int> stk[MAX_N];
  int top = 0;
  for (int i = 1; i <= N; i++) {
    int delta = A[i] - A[i - 1];
    if (delta > 0) {
      stk[++top] = make_pair(1, delta);
    } else {
      while (delta) {
        if (delta + stk[top].second <= 0) {
          delta += stk[top].second;
          top--;
        } else {
          stk[top].second += delta;
          delta = 0;
        }
      }
      int popped = 0;
      while (top && stk[top].first == sigma) {
        popped += stk[top].second;
        top--;
      }
      if (!top)
        return false;
      if (stk[top].second != 1) {
        stk[top + 1] = make_pair(stk[top].first, 1);
        stk[top].second--;
        top++;
      }
      stk[top].first++;
      if (popped) {
        stk[++top] = make_pair(1, popped);
      }
    }
  }
  return true;
}

int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++)
    scanf("%d", &A[i]);
  int l = 1, r = N;
  while (l != r) {
    int m = (l + r) / 2;
    if (check(m))
      r = m;
    else
      l = m + 1;
  }
  printf("%d\n", l);
}
Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
using namespace std;
int n;
int a[200005], l, r, tail;
struct que {
  int x, v;
} sta[200005];
void insert(int x, int v) {
  while (sta[tail].x > x)
    tail--;
  if (sta[tail].x == x)
    sta[tail].v++;
  else
    sta[++tail] = (que){x, 1};
  if (tail > 1 && sta[tail].v == v)
    tail--, insert(x - 1, v);
}
bool check(int v) {
  tail = 0;
  sta[++tail] = (que){0, 0};
  for (int i = 2; i <= n; i++)
    if (a[i] <= a[i - 1])
      insert(a[i], v);
  return !sta[1].v;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  bool flag = 1;
  for (int i = 2; i <= n; i++)
    if (a[i] <= a[i - 1])
      flag = 0;
  if (flag) {
    puts("1");
    return 0;
  }
  l = 1, r = n;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d", l);
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
using namespace std;
int n;
int a[200005], l, r, tail;
struct que {
  int x, v;
} sta[200005];
void insert(int x, int v) {
  while (sta[tail].x > x)
    tail--;
  if (sta[tail].x == x)
    sta[tail].v++;
  else
    sta[++tail] = (que){x, 1};
  if (tail > 1 && sta[tail].v == v)
    tail--, insert(x - 1, v);
}
bool check(int v) {
  tail = 0;
  sta[++tail] = (que){0, 0};
  for (int i = 2; i <= n; i++)
    if (a[i] <= a[i - 1])
      insert(a[i], v);
  return !sta[1].v;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  bool flag = 1;
  for (int i = 2; i <= n; i++)
    if (a[i] <= a[i - 1])
      flag = 0;
  if (flag) {
    puts("1");
    return 0;
  }
  l = 2, r = n;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d", l);
}
Problem p03203 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define range(i, m, n) for (int i = m; i < n; i++)
#define husk(i, m, n) for (int i = m; i > n; i--)

int n, h, w;

int main() {
  cin >> h >> w >> n;
  set<pair<int, int>> obs;
  vector<vector<int>> to(w);
  range(i, 0, n) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    if (x < y)
      continue;
    to[x].push_back(y);
    obs.insert(make_pair(x, y));
  }
  range(i, 0, w) {
    to[i].push_back(h);
    sort(to[i].begin(), to[i].end());
  }
  range(x, 0, h) obs.insert(make_pair(x, w));
  range(y, 0, w) obs.insert(make_pair(h, y));
  int x = 0;
  int y = 0;
  vector<pair<int, int>> cur;
  auto ted = [&]() {
    if (cur.size() < 3) {
      if (cur.size() == 2) {
        return (cur[0] == cur[1] && cur[0] == make_pair(0, 0));
      }
      return false;
    }
    int sz = cur.size();
    return (cur[sz - 1] == cur[sz - 2] && cur[sz - 2] == cur[sz - 3]);
  };
  while (true) {
    if (obs.find(make_pair(x + 1, y)) != obs.end()) {
      cur.emplace_back(x, y);
      if (ted())
        break;
      cur.emplace_back(x, y);
      break;
    }
    x++;
    cur.emplace_back(x, y);
    int c = obs.find(make_pair(x, y + 1)) == obs.end();
    y += c;
    cur.emplace_back(x, y);
  }
  int res = cur.size();
  for (int i = 0; i < (int)cur.size(); i += 2) {
    int u = cur[i].first, v = cur[i].second;
    int su = *lower_bound(to[v].begin(), to[v].end(), u);
    res = min(res, i + 1 + (su - u) * 2);
  }
  cout << (res + 1) / 2;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define range(i, m, n) for (int i = m; i < n; i++)
#define husk(i, m, n) for (int i = m; i > n; i--)

int n, h, w;

int main() {
  cin >> h >> w >> n;
  set<pair<int, int>> obs;
  vector<vector<int>> to(w);
  range(i, 0, n) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    if (x < y)
      continue;
    to[y].push_back(x);
    obs.insert(make_pair(x, y));
  }
  range(i, 0, w) {
    to[i].push_back(h);
    sort(to[i].begin(), to[i].end());
  }
  range(x, 0, h) obs.insert(make_pair(x, w));
  range(y, 0, w) obs.insert(make_pair(h, y));
  int x = 0;
  int y = 0;
  vector<pair<int, int>> cur;
  auto ted = [&]() {
    if (cur.size() < 3) {
      if (cur.size() == 2) {
        return (cur[0] == cur[1] && cur[0] == make_pair(0, 0));
      }
      return false;
    }
    int sz = cur.size();
    return (cur[sz - 1] == cur[sz - 2] && cur[sz - 2] == cur[sz - 3]);
  };
  while (true) {
    if (obs.find(make_pair(x + 1, y)) != obs.end()) {
      cur.emplace_back(x, y);
      if (ted())
        break;
      cur.emplace_back(x, y);
      break;
    }
    x++;
    cur.emplace_back(x, y);
    int c = obs.find(make_pair(x, y + 1)) == obs.end();
    y += c;
    cur.emplace_back(x, y);
  }
  int res = cur.size();
  for (int i = 0; i < (int)cur.size(); i += 2) {
    int u = cur[i].first, v = cur[i].second;
    int su = *lower_bound(to[v].begin(), to[v].end(), u);
    res = min(res, i + 1 + (su - u) * 2);
  }
  cout << (res + 1) / 2;
  return 0;
}
Problem p03203 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n, m, q;
#define Maxn 100010
int mins[Maxn];

inline void rd(int &x) {
  x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    ch = getchar();
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
}

int main() {
  memset(mins, 127, sizeof(mins));
  rd(n);
  rd(m);
  rd(q);
  int x, y;
  for (register int i = 1; i <= q; ++i) {
    rd(x);
    rd(y);
    x--;
    y--;
    mins[x] = min(mins[x], y);
  }
  int right = 0;
  for (register int i = 1; i < n; ++i)
    if (right >= mins[i]) {
      printf("%d\n", i);
      return 0;
    } else if (right + 1 < mins[i])
      right++;
  printf("%d\n", n);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n, m, q;
#define Maxn 200010
int mins[Maxn];

inline void rd(int &x) {
  x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    ch = getchar();
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
}

int main() {
  memset(mins, 127, sizeof(mins));
  rd(n);
  rd(m);
  rd(q);
  int x, y;
  for (register int i = 1; i <= q; ++i) {
    rd(x);
    rd(y);
    x--;
    y--;
    mins[x] = min(mins[x], y);
  }
  int right = 0;
  for (register int i = 1; i < n; ++i)
    if (right >= mins[i]) {
      printf("%d\n", i);
      return 0;
    } else if (right + 1 < mins[i])
      right++;
  printf("%d\n", n);
  return 0;
}
Problem p03203 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;

const double eps = 1e-8;

const ll INF = 1e15;
const ll MOD = 1e9 + 7;

int main() {
  ll H, W, N;
  cin >> H >> W >> N;
  vector<set<ll>> sets(H);
  for (int i = 0; i < N; i++) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    sets[y].insert(x);
  }
  for (int y = 0; y < H; y++)
    sets[y].insert(W);
  ll answer = INF;
  ll y = 0;
  ll x = 0;
  while (true) {
    if (sets[y].lower_bound(x) == sets[y].end())
      break;
    ll near = *sets[y].lower_bound(x);
    answer = min(answer, near);
    x++;
    if (x >= W)
      break;
    if (sets[y].find(x) != sets[y].end())
      break;
    if (y >= H - 1)
      break;
    if (sets[y + 1].find(x) == sets[y + 1].end())
      y++;
    if (y >= H || x >= W)
      break;
  }
  cout << answer << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;

const double eps = 1e-8;

const ll INF = 1e15;
const ll MOD = 1e9 + 7;

int main() {
  ll H, W, N;
  cin >> W >> H >> N;
  vector<set<ll>> sets(H);
  for (int i = 0; i < N; i++) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    sets[y].insert(x);
  }
  for (int y = 0; y < H; y++)
    sets[y].insert(W);
  ll answer = INF;
  ll y = 0;
  ll x = 0;
  while (true) {
    if (sets[y].lower_bound(x) == sets[y].end())
      break;
    ll near = *sets[y].lower_bound(x);
    answer = min(answer, near);
    x++;
    if (x >= W)
      break;
    if (sets[y].find(x) != sets[y].end())
      break;
    if (y >= H - 1)
      break;
    if (sets[y + 1].find(x) == sets[y + 1].end())
      y++;
    if (y >= H || x >= W)
      break;
  }
  cout << answer << endl;
  return 0;
}
Problem p03203 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int h, w, n;
  cin >> h >> w >> n;
  set<int> xs[w];
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    --y;
    --x;
    xs[y].insert(x);
  }
  for (int i = 0; i < w; i++) {
    xs[i].insert(h);
  }
  if (h == 1 || xs[1].find(0) != xs[1].end()) {
    cout << 1 << endl;
    return 0;
  }
  int ans = 1e6;
  int cur_w = 0;
  for (int i = 1; i < h; i++) {
    if (xs[cur_w].find(i) != xs[cur_w].end()) {
      ans = min(i, ans);
      break;
    }
    auto it = xs[cur_w].upper_bound(i);
    ans = min(*it, ans);
    if (cur_w < w - 1) {
      if (xs[cur_w + 1].find(i) == xs[cur_w + 1].end()) {
        ++cur_w;
      }
    }
  }
  cout << ans << endl;
}

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

int main() {
  int h, w, n;
  cin >> h >> w >> n;
  set<int> xs[w];
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    --y;
    --x;
    xs[y].insert(x);
  }
  for (int i = 0; i < w; i++) {
    xs[i].insert(h);
  }
  if (h == 1 || (w > 1 && xs[1].find(0) != xs[1].end())) {
    cout << 1 << endl;
    return 0;
  }
  int ans = 1e6;
  int cur_w = 0;
  for (int i = 1; i < h; i++) {
    if (xs[cur_w].find(i) != xs[cur_w].end()) {
      ans = min(i, ans);
      break;
    }
    auto it = xs[cur_w].upper_bound(i);
    ans = min(*it, ans);
    if (cur_w < w - 1) {
      if (xs[cur_w + 1].find(i) == xs[cur_w + 1].end()) {
        ++cur_w;
      }
    }
  }
  cout << ans << endl;
}
Problem p03203 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep2(i, a, b) for (ll i = (a); i < (b); ++i)
#define debug(x) cout << #x << '=' << x << endl
#define all(v) (v).begin(), (v).end()
const ll MOD = 1e9 + 7;
// const ll MOD=998244353;
const ll INF = 1e9;
const ll IINF = 1e18;
const double EPS = 1e-8;
const double pi = acos(-1);

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int H, W, N;
  cin >> H >> W >> N;
  set<Pii> s;
  vector<vector<int>> block(W);
  vector<Pii> P(N);
  rep(i, N) {
    int x, y;
    cin >> x >> y;
    P[i] = {--x, --y};
    s.insert({x, y});
  }
  sort(P.begin(), P.end());
  rep(i, N) block[P[i].second].push_back(P[i].first);
  rep(i, W) block[i].push_back(H);
  int now = 0, ans = H;
  rep(i, H) {
    chmin(ans, *upper_bound(block[now].begin(), block[now].end(), i));
    if (s.count({i + 1, now}))
      break;
    if (!s.count({i + 1, now + 1}))
      ++now;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep2(i, a, b) for (ll i = (a); i < (b); ++i)
#define debug(x) cout << #x << '=' << x << endl
#define all(v) (v).begin(), (v).end()
const ll MOD = 1e9 + 7;
// const ll MOD=998244353;
const ll INF = 1e9;
const ll IINF = 1e18;
const double EPS = 1e-8;
const double pi = acos(-1);

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int H, W, N;
  cin >> H >> W >> N;
  set<Pii> s;
  vector<vector<int>> block(W);
  vector<Pii> P(N);
  rep(i, N) {
    int x, y;
    cin >> x >> y;
    P[i] = {--x, --y};
    s.insert({x, y});
  }
  sort(P.begin(), P.end());
  rep(i, N) block[P[i].second].push_back(P[i].first);
  rep(i, W) block[i].push_back(H);
  int now = 0, ans = H;
  rep(i, H) {
    chmin(ans, *upper_bound(block[now].begin(), block[now].end(), i));
    if (s.count({i + 1, now}))
      break;
    if (!s.count({i + 1, now + 1}) && now < W - 1)
      ++now;
  }
  cout << ans << endl;
}
Problem p03203 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound

int shortest[200200];
set<int> obj[200200];

signed main() {
  int H, W, N;
  cin >> H >> W >> N;
  rep(i, N) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    obj[y].insert(x);
  }
  shortest[0] = 0;
  for (int y = 1; y < W; ++y) {
    if (shortest[y - 1] == INF) {
      shortest[y] = INF;
      continue;
    }
    for (int j = shortest[y - 1] + 1;; ++j) {
      if (!obj[y].count(j)) {
        shortest[y] = j;
        break;
      }
      if (j > H) {
        shortest[y] = INF;
        break;
      }
    }
  }
  int ans = H;
  for (int y = 0; y < W; ++y) {
    auto ite = obj[y].upper_bound(shortest[y]);
    if (ite == obj[y].end())
      continue;
    ans = min(ans, *ite);
  }
  if (ans == max(H, W))
    return 1;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound

int shortest[200200];
set<int> obj[200200];

signed main() {
  int H, W, N;
  cin >> H >> W >> N;
  rep(i, N) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    obj[y].insert(x);
  }
  shortest[0] = 0;
  for (int y = 1; y < W; ++y) {
    if (shortest[y - 1] == INF) {
      shortest[y] = INF;
      continue;
    }
    for (int j = shortest[y - 1] + 1;; ++j) {
      if (!obj[y].count(j)) {
        shortest[y] = j;
        break;
      }
      if (j > H) {
        shortest[y] = INF;
        break;
      }
    }
  }
  int ans = H;
  for (int y = 0; y < W; ++y) {
    auto ite = obj[y].upper_bound(shortest[y]);
    if (ite == obj[y].end())
      continue;
    ans = min(ans, *ite);
  }
  cout << ans << endl;
  return 0;
}
Problem p03203 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, K, Q, A, B;
string S;
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
const ll INF = 1LL << 60;
typedef pair<ll, ll> P;

int main() {
  cin >> H >> W >> N;
  vec x(N + 1), y(N + 1), ord(N + 1);
  rep(i, N) cin >> x[i] >> y[i], --x[i], --y[i], ord[i] = i;
  x[N] = H;
  y[N] = 0;
  sort(ALL(ord), [&](int a, int b) { return x[a] < x[b]; });
  int id = 0, depth = 0;
  reps(i, 1, H + 1) {
    while (x[ord[id]] < i)
      ++id;
    if (x[ord[id]] == i) {
      if (y[ord[id]] <= depth) {
        cout << i << endl;
        return 0;
      } else if (y[ord[id]] > depth + 1) {
        ++depth;
      }
    } else {
      ++depth;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, K, Q, A, B;
string S;
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
const ll INF = 1LL << 60;
typedef pair<ll, ll> P;

int main() {
  cin >> H >> W >> N;
  vec x(N + 1), y(N + 1), ord(N + 1);
  rep(i, N) cin >> x[i] >> y[i], --x[i], --y[i], ord[i] = i;
  x[N] = H;
  y[N] = 0;
  ord[N] = N;
  sort(ALL(ord), [&](int a, int b) {
    if (x[a] == x[b])
      return y[a] < y[b];
    return x[a] < x[b];
  });
  int id = 0, depth = 0;
  reps(i, 1, H + 1) {
    while (x[ord[id]] < i)
      ++id;
    if (x[ord[id]] == i) {
      if (y[ord[id]] <= depth) {
        cout << i << endl;
        return 0;
      } else if (y[ord[id]] > depth + 1) {
        ++depth;
      }
    } else {
      ++depth;
    }
  }
}
Problem p03203 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

set<int> Se[100001];

int main() {
  int n, m, k, X, Y, Mi = 20000000, t = 0;
  cin >> m >> n >> k;
  set<int>::iterator it;
  for (int i = 0; i < k; i++) {
    cin >> X >> Y;
    X--;
    Y--;
    Se[Y].insert(X);
  }
  for (int i = 0; i < n; i++) {
    Se[i].insert(m);
  }
  if (Se[0].find(1) != Se[0].end()) {
    cout << 1;
    return 0;
  }
  X = 1;
  Y = 0;
  t = 1;
  while (X < m && Y < n) {
    it = Se[Y].upper_bound(X);
    Mi = min(Mi, *it - X - 1 + t);
    if (Y == n - 1) {
      break;
    }
    if (Se[Y + 1].find(X) == Se[Y + 1].end()) {
      Y++;
    }
    it = Se[Y].upper_bound(X);
    Mi = min(Mi, *it - X - 1 + t);
    X++;
    t++;
  }
  cout << Mi + 1;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

set<int> Se[200001];

int main() {
  int n, m, k, X, Y, Mi = 20000000, t = 0;
  cin >> m >> n >> k;
  set<int>::iterator it;
  for (int i = 0; i < k; i++) {
    cin >> X >> Y;
    X--;
    Y--;
    Se[Y].insert(X);
  }
  for (int i = 0; i < n; i++) {
    Se[i].insert(m);
  }
  if (Se[0].find(1) != Se[0].end()) {
    cout << 1;
    return 0;
  }
  X = 1;
  Y = 0;
  t = 1;
  while (X < m && Y < n) {
    it = Se[Y].upper_bound(X);
    Mi = min(Mi, *it - X - 1 + t);
    if (Y == n - 1) {
      break;
    }
    if (Se[Y + 1].find(X) == Se[Y + 1].end()) {
      Y++;
    }
    it = Se[Y].upper_bound(X);
    Mi = min(Mi, *it - X - 1 + t);
    X++;
    t++;
  }
  cout << Mi + 1;
  return 0;
}
Problem p03203 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(it, st, en) for (int it = (st); it < (int)(en); ++it)
#define all(c) (c).begin(), (c).end()
#define debug(x) cerr << #x << " = " << x << endl
#define eb emplace_back
#define X first
#define Y second
typedef long long int ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pii;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w, n;
  cin >> h >> w >> n;
  int ans = w;
  vector<pii> p(n);
  set<pii> blocked;
  rep(i, 0, n) {
    cin >> p[i].X >> p[i].Y;
    blocked.insert(p[i]);
  }
  vi reach(w + 1);
  reach[1] = 1;
  rep(i, 2, w + 1) {
    reach[i] = reach[i - 1] + 1;
    if (blocked.count({i, reach[i]}))
      --reach[i];
  }
  for (auto q : p) {
    if (reach[q.X] >= q.Y) {
      ans = min(ans, q.X - 1);
    }
  }
  cout << ans << endl;
  return 0;
}
/*
 * T must ++x, otherwise A will pass to and end the game.
 * A has two choices:
 *  pass: y += 0
 *  move: ++y
 *
 * Wich block can we reach?
 *
 *  1234 x
 * 1....
 * 2 .X.
 * 3  ..
 * 4   .
 *
 * ...
 * ..X
 * ...
 *
 *
 *
 *
 */


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(it, st, en) for (int it = (st); it < (int)(en); ++it)
#define all(c) (c).begin(), (c).end()
#define debug(x) cerr << #x << " = " << x << endl
#define eb emplace_back
#define X first
#define Y second
typedef long long int ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pii;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w, n;
  cin >> w >> h >> n;
  int ans = w;
  vector<pii> p(n);
  set<pii> blocked;
  rep(i, 0, n) {
    cin >> p[i].X >> p[i].Y;
    blocked.insert(p[i]);
  }
  vi reach(w + 1);
  reach[1] = 1;
  rep(i, 2, w + 1) {
    reach[i] = reach[i - 1] + 1;
    if (blocked.count({i, reach[i]}))
      --reach[i];
  }
  for (auto q : p) {
    if (reach[q.X] >= q.Y) {
      ans = min(ans, q.X - 1);
    }
  }
  cout << ans << endl;
  return 0;
}
/*
 * T must ++x, otherwise A will pass to and end the game.
 * A has two choices:
 *  pass: y += 0
 *  move: ++y
 *
 * Wich block can we reach?
 *
 *  1234 x
 * 1....
 * 2 .X.
 * 3  ..
 * 4   .
 *
 * ...
 * ..X
 * ...
 *
 *
 *
 *
 */
Problem p03203 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int H, W, N;
int main() {
  cin >> H >> W >> N;
  vector<vector<int>> G(H + 1);
  vector<set<int>> A(H + 1);
  for (int i = 0; i <= H; i++) {
    G[i].push_back(W);
    A[i].insert(W);
  }
  for (int i = 0; i < N; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G[y].push_back(x);
    A[y].insert(x);
  }
  for (int i = 0; i < H; i++)
    sort(G[i].begin(), G[i].end());
  int a = 0;
  int ans = W;
  for (int i = 0; i < H; i++) {
    auto itr = upper_bound(G[i].begin(), G[i].end(), a);
    int k = distance(G[i].begin(), itr);
    if (G[i][k] < ans)
      ans = G[i][k];
    bool x = false;
    int b = a;
    for (int j = b + 1; j < G[i][k]; j++)
      if (!A[i + 1].count(j)) {
        a = j;
        x = true;
        break;
      }
    if (!x || ans < a)
      break;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int H, W, N;
int main() {
  cin >> W >> H >> N;
  vector<vector<int>> G(H + 1);
  vector<set<int>> A(H + 1);
  for (int i = 0; i <= H; i++) {
    G[i].push_back(W);
    A[i].insert(W);
  }
  for (int i = 0; i < N; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G[y].push_back(x);
    A[y].insert(x);
  }
  for (int i = 0; i < H; i++)
    sort(G[i].begin(), G[i].end());
  int a = 0;
  int ans = W;
  for (int i = 0; i < H; i++) {
    auto itr = upper_bound(G[i].begin(), G[i].end(), a);
    int k = distance(G[i].begin(), itr);
    if (G[i][k] < ans)
      ans = G[i][k];
    bool x = false;
    int b = a;
    for (int j = b + 1; j < G[i][k]; j++)
      if (!A[i + 1].count(j)) {
        a = j;
        x = true;
        break;
      }
    if (!x || ans < a)
      break;
  }
  cout << ans << endl;
  return 0;
}
Problem p03203 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int n, m;
int i, j, k;
string str;
int h, w;

int main() {
  cin >> h >> w >> n;
  vector<pair<int, int>> b(n);
  vector<vector<int>> f(w);
  for (i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    b[i] = make_pair(x, y);
  }
  sort(b.begin(), b.end());
  for (i = 0; i < n; i++) {
    f[b[i].first].push_back(b[i].second);
  }
  int s = 1;
  int a = w;
  for (i = 1; i < w; i++) {
    if (f[i].size() > 0) {
      if (f[i][0] < s) {
        a = i;
        break;
      }
      if (f[i][0] == s)
        s--;
    }
    s++;
  }
  cout << a << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int n, m;
int i, j, k;
string str;
int h, w;

int main() {
  cin >> w >> h >> n;
  vector<pair<int, int>> b(n);
  vector<vector<int>> f(w);
  for (i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    b[i] = make_pair(x, y);
  }
  sort(b.begin(), b.end());
  for (i = 0; i < n; i++) {
    f[b[i].first].push_back(b[i].second);
  }
  int s = 1;
  int a = w;
  for (i = 1; i < w; i++) {
    if (f[i].size() > 0) {
      if (f[i][0] < s) {
        a = i;
        break;
      }
      if (f[i][0] == s)
        s--;
    }
    s++;
  }
  cout << a << endl;
}
Problem p03203 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <set>
int const maxN = 21;
int op[maxN];
std::set<int> ops[maxN];
int main() {
  int h, w, n;
  scanf("%d%d%d", &h, &w, &n);
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    ops[y].emplace(x);
  }
  int offset = 0;
  for (int i = 1; i <= w; i++) {
    while (ops[i].count(i + offset) > 0)
      offset++;
    if (i + offset > h || ops[i].lower_bound(i + offset) == ops[i].end()) {
      op[i] = h;
    } else {
      op[i] = *(ops[i].lower_bound(i + offset)) - 1;
    }
  }
  std::sort(op + 1, op + w + 1);
  printf("%d", op[1]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <set>
int const maxN = 200001;
int op[maxN];
std::set<int> ops[maxN];
int main() {
  int h, w, n;
  scanf("%d%d%d", &h, &w, &n);
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    ops[y].emplace(x);
  }
  int offset = 0;
  for (int i = 1; i <= w; i++) {
    while (ops[i].count(i + offset) > 0)
      offset++;
    if (i + offset > h || ops[i].lower_bound(i + offset) == ops[i].end()) {
      op[i] = h;
    } else {
      op[i] = *(ops[i].lower_bound(i + offset)) - 1;
    }
  }
  std::sort(op + 1, op + w + 1);
  printf("%d", op[1]);
  return 0;
}
Problem p03203 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = 1LL << 50;
set<ll> s[100010];
int main() {
  ll h, w, n;
  cin >> h >> w >> n;

  // vector<vector<char>> v(h+1, vector<char>(w+1,'.') );
  map<pair<ll, ll>, bool> m;
  rep(i, n) {
    ll x, y;
    cin >> x >> y;
    // v[x-1][y-1]='#';
    m[{x - 1, y - 1}] = 1;
    s[y - 1].insert(x - 1);
  }
  // rep(i,w)v[i][w]='#';
  rep(i, h) m[{i, w}] = 1;
  // rep(i,h){v[h][i]='#';s[i].insert(h);}
  rep(i, w) {
    m[{h, i}] = 1;
    s[i].insert(h);
  }
  ll x = 0, y = 0;
  /* rep(i,h+1){
  rep(j,w+1)cout << v[i][j];
  cout << endl;
  } */
  rep(i, h) {
    ans = min(ans, *upper_bound(s[y].begin(), s[y].end(), x));
    // if(v[x+1][y]=='#')break;
    if (m.find({x + 1, y}) != m.end())
      break;
    x++;
    // if(v[x][y+1]=='.')y++;
    if (m.find({x, y + 1}) == m.end())
      y++;
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = 1LL << 50;
set<ll> s[200010];
int main() {
  ll h, w, n;
  cin >> h >> w >> n;

  // vector<vector<char>> v(h+1, vector<char>(w+1,'.') );
  map<pair<ll, ll>, bool> m;
  rep(i, n) {
    ll x, y;
    cin >> x >> y;
    // v[x-1][y-1]='#';
    m[{x - 1, y - 1}] = 1;
    s[y - 1].insert(x - 1);
  }
  // rep(i,w)v[i][w]='#';
  rep(i, h) m[{i, w}] = 1;
  // rep(i,h){v[h][i]='#';s[i].insert(h);}
  rep(i, w) {
    m[{h, i}] = 1;
    s[i].insert(h);
  }
  ll x = 0, y = 0;
  /* rep(i,h+1){
  rep(j,w+1)cout << v[i][j];
  cout << endl;
  } */
  rep(i, h) {
    ans = min(ans, *upper_bound(s[y].begin(), s[y].end(), x));
    // if(v[x+1][y]=='#')break;
    if (m.find({x + 1, y}) != m.end())
      break;
    x++;
    // if(v[x][y+1]=='.')y++;
    if (m.find({x, y + 1}) == m.end())
      y++;
  }

  cout << ans << endl;
  return 0;
}
Problem p03203 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using pii = pair<int, int>;

int main() {
  ios_base::sync_with_stdio(false);
  int h, w, n;
  cin >> h >> w >> n;
  vector<pii> objs;
  for (int i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    objs.emplace_back(y, x);
  }
  sort(objs.begin(), objs.end());
  const int init =
      lower_bound(objs.begin(), objs.end(), pii(1, 0)) - objs.begin();
  vector<int> leftmost(h, w);
  leftmost[0] = 0;
  for (int y = 1, i = init; y < h; ++y) {
    leftmost[y] = leftmost[y - 1] + 1;
    while (i < n && objs[i].first == y) {
      if (objs[i].second == leftmost[y]) {
        ++leftmost[y];
      }
      ++i;
    }
    leftmost[y] = min(w - 1, leftmost[y]);
  }
  int answer = w;
  for (const auto &p : objs) {
    const int y = p.first, x = p.second;
    if (x - 1 < leftmost[y]) {
      continue;
    }
    answer = min(answer, x);
  }
  cout << answer << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using pii = pair<int, int>;

int main() {
  ios_base::sync_with_stdio(false);
  int h, w, n;
  cin >> w >> h >> n;
  vector<pii> objs;
  for (int i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    objs.emplace_back(y, x);
  }
  sort(objs.begin(), objs.end());
  const int init =
      lower_bound(objs.begin(), objs.end(), pii(1, 0)) - objs.begin();
  vector<int> leftmost(h, w);
  leftmost[0] = 0;
  for (int y = 1, i = init; y < h; ++y) {
    leftmost[y] = leftmost[y - 1] + 1;
    while (i < n && objs[i].first == y) {
      if (objs[i].second == leftmost[y]) {
        ++leftmost[y];
      }
      ++i;
    }
    leftmost[y] = min(w - 1, leftmost[y]);
  }
  int answer = w;
  for (const auto &p : objs) {
    const int y = p.first, x = p.second;
    if (x - 1 < leftmost[y]) {
      continue;
    }
    answer = min(answer, x);
  }
  cout << answer << endl;
  return 0;
}
Problem p03203 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> kabe[200005];
int main() {
  ll h, w, n;
  cin >> h >> w >> n;
  ll ans = h;
  for (int i = 0; i < n; i++) {
    ll x, y;
    cin >> x >> y;
    kabe[x].push_back(y);
  }
  for (int i = 1; i <= h; i++) {
    kabe[i].push_back(17441744);
    sort(kabe[i].begin(), kabe[i].end());
  }
  ll c = 0;
  for (int i = 1; i <= w; i++) {
    if (i - c - 1 >= kabe[i][0]) {
      cout << i - 1 << endl;
      return 0;
    } else {
      ll ky = *(lower_bound(kabe[i].begin(), kabe[i].end(), i - c));
      if (ky == i - c)
        c++;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> kabe[200005];
int main() {
  ll h, w, n;
  cin >> h >> w >> n;
  ll ans = h;
  for (int i = 0; i < n; i++) {
    ll x, y;
    cin >> x >> y;
    kabe[x].push_back(y);
  }
  for (int i = 1; i <= h; i++) {
    kabe[i].push_back(17441744);
    sort(kabe[i].begin(), kabe[i].end());
  }
  ll c = 0;
  for (int i = 1; i <= h; i++) {
    if (i - c - 1 >= kabe[i][0]) {
      cout << i - 1 << endl;
      return 0;
    } else {
      ll ky = *(lower_bound(kabe[i].begin(), kabe[i].end(), i - c));
      if (ky == i - c)
        c++;
    }
  }
  cout << ans << endl;
}
Problem p03203 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <chrono>
#include <climits>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define P pair<int, int>
#define debug(x) cerr << #x << ": " << x << ", "
#define debugln(x) cerr << #x << ": " << x << '\n'

using namespace std;
using ll = long long;
const int mod = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int H, W, N;
  cin >> H >> W >> N;
  vector<int> vec(H + 5);

  int ans = H;
  if (H == 1) {
    cout << 1 << endl;
    return 0;
  }
  rep(i, N) {
    int x, y;
    cin >> x >> y;
    if (vec.at(x) == 0) {
      vec.at(x) = y;
    } else {
      vec.at(x) = min(vec.at(x), y);
    }
  }

  if (vec.at(2) == 1) {
    cout << 1 << endl;
    return 0;
  }

  int pos = 1;
  for (int i = 2; i <= W; i++) {
    if (vec.at(i) >= 1 && vec.at(i) <= pos) {
      cout << i - 1 << endl;
      return 0;
    }
    if (vec.at(i) == pos + 1)
      continue;
    pos++;
  }

  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <chrono>
#include <climits>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define P pair<int, int>
#define debug(x) cerr << #x << ": " << x << ", "
#define debugln(x) cerr << #x << ": " << x << '\n'

using namespace std;
using ll = long long;
const int mod = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int H, W, N;
  cin >> H >> W >> N;
  vector<int> vec(H + 5);

  int ans = H;
  if (H == 1) {
    cout << 1 << endl;
    return 0;
  }
  rep(i, N) {
    int x, y;
    cin >> x >> y;
    if (vec.at(x) == 0) {
      vec.at(x) = y;
    } else {
      vec.at(x) = min(vec.at(x), y);
    }
  }

  if (vec.at(2) == 1) {
    cout << 1 << endl;
    return 0;
  }

  int pos = 1;
  for (int i = 2; i <= H; i++) {
    if (vec.at(i) >= 1 && vec.at(i) <= pos) {
      cout << i - 1 << endl;
      return 0;
    }
    if (vec.at(i) == pos + 1)
      continue;
    pos++;
  }

  cout << ans << endl;
}
Problem p03203 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(c) (c).rbegin(), (c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll>>
#define fi first
#define se second
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
const ll inf = 998244353;
const ll mod = 998244353;
signed main() {
  ll h, w, n;
  cin >> h >> w >> n;
  vector<vector<ll>> a(w);
  set<pll> st;
  rep(i, n) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    st.insert(mp(x, y));
    a[y].pb(x);
  }
  rep(i, w) {
    a[i].pb(h);
    st.insert(mp(i, h));
    sort(all(a[i]));
  }
  ll nx = 0, ny = 0;
  ll ans = h;
  while (nx < h) {
    if (st.find(mp(nx + 1, ny)) != st.end()) {
      ans = min(ans, nx + 1);
      break;
    }
    ll p = *upper_bound(all(a[ny]), nx);
    ans = min(ans, p);
    nx++;
    if (st.find(mp(nx, ny + 1)) == st.end()) {
      ny++;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(c) (c).rbegin(), (c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll>>
#define fi first
#define se second
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
const ll inf = 998244353;
const ll mod = 998244353;
signed main() {
  ll h, w, n;
  cin >> h >> w >> n;
  vector<vector<ll>> a(w);
  set<pll> st;
  rep(i, n) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    st.insert(mp(x, y));
    a[y].pb(x);
  }
  rep(i, w) {
    a[i].pb(h);
    st.insert(mp(i, h));
    sort(all(a[i]));
  }
  ll nx = 0, ny = 0;
  ll ans = h;
  while (nx < h && ny < w) {
    if (st.find(mp(nx + 1, ny)) != st.end()) {
      ans = min(ans, nx + 1);
      break;
    }
    ll p = *upper_bound(all(a[ny]), nx);
    ans = min(ans, p);
    nx++;
    if (st.find(mp(nx, ny + 1)) == st.end()) {
      ny++;
    }
  }
  cout << ans << endl;
}
Problem p03203 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define MAXS 200000

int h, w, n, ans;
int x[MAXS + 3], y[MAXS + 3];
int vis[MAXS + 3];

int main() {
  scanf("%d %d %d", &h, &w, &n);
  ans = h;
  memset(vis, 0x3f, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &x[i], &y[i]);
    if (x[i] >= y[i])
      vis[x[i] - y[i]] = min(vis[x[i] - y[i]], x[i]);
  }
  for (int i = 1; i <= n; i++)
    if (vis[x[i] - y[i] - 1] > x[i])
      ans = min(ans, x[i] - 1);
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define MAXS 200000

int h, w, n, ans;
int x[MAXS + 3], y[MAXS + 3];
int vis[MAXS + 3];

int main() {
  scanf("%d %d %d", &h, &w, &n);
  ans = h;
  memset(vis, 0x3f, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &x[i], &y[i]);
    if (x[i] >= y[i])
      vis[x[i] - y[i]] = min(vis[x[i] - y[i]], x[i]);
  }
  for (int i = 1; i <= n; i++)
    if (x[i] > y[i])
      if (vis[x[i] - y[i] - 1] > x[i])
        ans = min(ans, x[i] - 1);
  printf("%d\n", ans);
  return 0;
}
Problem p03203 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
typedef long long LL;
typedef pair<LL, LL> P;
const LL mod = 1000000007;
const LL LINF = 1ll << 60;
const int INF = 1 << 30;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int main() {
  int h, w, n;
  cin >> h >> w >> n;
  vector<vector<int>> a(h);
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    a[y].pb(x);
  }
  int ans = w;
  int x = 0;
  for (int i = 0; i < h; i++) {
    if (x >= w)
      break;
    if (a[i].size()) {
      sort(ALL(a[i]));
      int t = 0;
      while (t < a[i].size() && a[i][t] < x) {
        t++;
      }
      if (t < a[i].size())
        ans = min(ans, a[i][t]);
    }
    if (i + 1 < h && a[i + 1].size()) {
      int t = 0;
      x++;
      sort(ALL(a[i + 1]));
      while (t < a[i + 1].size() && a[i + 1][t] < x) {
        t++;
      }
      if (t < a[i + 1].size()) {
        if (a[i + 1][t] == x) {
          while (t < a[i + 1].size() && a[i + 1][t] == x) {
            t++;
            x++;
          }
        }
      }
    } else {
      x++;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
typedef long long LL;
typedef pair<LL, LL> P;
const LL mod = 1000000007;
const LL LINF = 1ll << 60;
const int INF = 1 << 30;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int main() {
  int h, w, n;
  cin >> w >> h >> n;
  vector<vector<int>> a(h);
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    a[y].pb(x);
  }
  int ans = w;
  int x = 0;
  for (int i = 0; i < h; i++) {
    if (x >= w)
      break;
    if (a[i].size()) {
      sort(ALL(a[i]));
      int t = 0;
      while (t < a[i].size() && a[i][t] < x) {
        t++;
      }
      if (t < a[i].size())
        ans = min(ans, a[i][t]);
    }
    if (i + 1 < h && a[i + 1].size()) {
      int t = 0;
      x++;
      sort(ALL(a[i + 1]));
      while (t < a[i + 1].size() && a[i + 1][t] < x) {
        t++;
      }
      if (t < a[i + 1].size()) {
        if (a[i + 1][t] == x) {
          while (t < a[i + 1].size() && a[i + 1][t] == x) {
            t++;
            x++;
          }
        }
      }
    } else {
      x++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03203 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb emplace_back
#define mp make_pair
#define int long long
#define y second
#define x first

using namespace std;

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.precision(10);
  int h;
  cin >> h;
  int w;
  cin >> w;
  int n;
  cin >> n;
  std::set<pair<int, int>> v;
  for (int i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    v.insert({x, y});
  }
  std::vector<int> mw(h + 1, 0);
  int x = 1, y = 1;
  mw[1] = 1;
  while (true) {
    if (v.find({x + 1, y}) != v.end() || (x == h))
      break;
    x++;
    if (y == w)
      break;
    if (v.find({x, y + 1}) == v.end())
      y++;
    mw[x] = y;
  }
  for (int i = 1; i <= w; ++i)
    if (mw[i] == 0)
      mw[i] = max(mw[i - 1] + 1, i + 1);
  int mi = h;
  for (auto r : v)
    if (r.second < r.first && mw[r.first] >= r.second)
      mi = min(mi, r.first - 1);
  cout << mi;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb emplace_back
#define mp make_pair
#define int long long
#define y second
#define x first

using namespace std;

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.precision(10);
  int h;
  cin >> h;
  int w;
  cin >> w;
  int n;
  cin >> n;
  std::set<pair<int, int>> v;
  for (int i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    v.insert({x, y});
  }
  std::vector<int> mw(h + 1, 0);
  int x = 1, y = 1;
  mw[1] = 1;
  while (true) {
    if (v.find({x + 1, y}) != v.end() || (x == h))
      break;
    x++;
    if (y == w)
      break;
    if (v.find({x, y + 1}) == v.end())
      y++;
    mw[x] = y;
  }
  for (int i = 1; i <= h; ++i)
    if (mw[i] == 0)
      mw[i] = max(mw[i - 1] + 1, i + 1);
  int mi = h;
  for (auto r : v)
    if (r.second < r.first && mw[r.first] >= r.second)
      mi = min(mi, r.first - 1);
  cout << mi;
}