Problem p03996 (C++)

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

using namespace std;

void show(vector<int> &v, string s = "") {
  if (s.size() > 0) {
    cout << s << endl;
  }
  for (auto e : v) {
    cout << e << " ";
  }
  cout << endl;
}

int main() {
  int n, m, q;

  scanf("%d %d %d", &n, &m, &q);
  vector<int> a(q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &a[i]);
    a[i]--;
  }

  reverse(a.begin(), a.end());

  vector<int> order;
  map<int, int> num_pos;
  vector<int> num_used(m, false);

  vector<int> fixed_index(n, 0);

  for (int i = 0; i < q; i++) {
    // printf("a[%d] = %d\n", i, a[i]);
    // show(fixed_index, "fixed_index");

    if (!num_used[a[i]]) {
      num_used[a[i]] = true;

      num_pos[a[i]] = order.size();
      order.emplace_back(a[i]);

      fixed_index.back()++;
      continue;
    }

    int pos = num_pos[a[i]];
    // printf("pos %d\n", pos);
    auto it_bound = upper_bound(fixed_index.begin(), fixed_index.end(), pos);
    it_bound = prev(it_bound);
    // printf("it_bound %d\n", *it_bound);
    if (*it_bound == pos) {
      *it_bound += 1;
    }
  }

  // show(order, "order");
  // show(fixed_index, "fixed_index");

  for (int i = 0; i < m; i++) {
    if (!num_used[i]) {
      order.emplace_back(i);
    }
  }
  // show(order, "order");

  bool ok = true;
  for (int i = fixed_index[0]; i < order.size() - 1; i++) {
    if (order[i] > order[i + 1]) {
      ok = false;
      break;
    }
  }

  cout << (ok ? "Yes" : "No") << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <utility>
#include <vector>

using namespace std;

void show(vector<int> &v, string s = "") {
  if (s.size() > 0) {
    cout << s << endl;
  }
  for (auto e : v) {
    cout << e << " ";
  }
  cout << endl;
}

int main() {
  int n, m, q;

  scanf("%d %d %d", &n, &m, &q);
  vector<int> a(q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &a[i]);
    a[i]--;
  }

  reverse(a.begin(), a.end());

  vector<int> order;
  map<int, int> num_pos;
  vector<int> num_used(m, false);

  vector<int> fixed_index(n, 0);

  for (int i = 0; i < q; i++) {
    // printf("a[%d] = %d\n", i, a[i]);
    // show(fixed_index, "fixed_index");

    if (!num_used[a[i]]) {
      num_used[a[i]] = true;

      num_pos[a[i]] = order.size();
      order.emplace_back(a[i]);

      fixed_index.back()++;
      continue;
    }

    int pos = num_pos[a[i]];
    // printf("pos %d\n", pos);
    auto it_bound = upper_bound(fixed_index.begin(), fixed_index.end(), pos);
    if (it_bound != fixed_index.begin()) {
      it_bound = prev(it_bound);
    }
    // printf("it_bound %d\n", *it_bound);
    if (*it_bound == pos) {
      *it_bound += 1;
    }
  }

  // show(order, "order");
  // show(fixed_index, "fixed_index");

  for (int i = 0; i < m; i++) {
    if (!num_used[i]) {
      order.emplace_back(i);
    }
  }
  // show(order, "order");

  bool ok = true;
  for (int i = fixed_index[0]; i < order.size() - 1; i++) {
    if (order[i] > order[i + 1]) {
      ok = false;
      break;
    }
  }

  cout << (ok ? "Yes" : "No") << endl;
  return 0;
}