Problem p00946 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second

#define DUMP(x) cout << #x << ":" << (x) << endl
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}
template <class T> istream &operator>>(istream &is, vector<T> &xs) {
  for (auto &x : xs)
    is >> x;
  return is;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << p.FF << " " << p.SS;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &xs) {
  for (unsigned int i = 0; i < xs.size(); ++i)
    os << (i ? " " : "") << xs[i];
  return os;
}
template <class T> void maxi(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void mini(T &x, T y) {
  if (x > y)
    x = y;
}

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

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

  int N, M;
  cin >> N >> M;

  VI qs(M);
  cin >> qs;
  vector<bool> used(N + 1);

  VI ans;
  for (int i = M - 1; i >= 0; --i) {
    int x = qs[i];
    if (used[x])
      continue;
    ans.PB(x);
    used[x] = true;
  }

  FOR(i, 1, N + 1)
  if (!used[i])
    ans.PB(i);
  for (int x : ans)
    cout << x << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int search(vector<int> data, int val, int startval) {
  int start = startval;
  int end = data.size() - 1;
  int center;
  int res = -2;
  if (val < data[start] || data[end] < val)
    return -1;
  while (res == -2) {
    center = (start + end) / 2;
    if (data[center] == val)
      res = center;
    else if (end - start == 1 && data[end] == val)
      res = end;
    else if (end - start == 1)
      res = -1;
    else if (data[center] < val)
      start = center;
  }
  return res;
}

int main() {
  int n, m;
  cin >> n >> m;
  int index = 0;
  vector<int> sequence;
  // vector< int > output(n);

  for (int i = 0; i < n; i++) {
    sequence.push_back(i + 1);
  }

  for (int i = 0; i < m; i++) {
    int e;
    cin >> e;
    int val = search(sequence, e, index);
    if (val == -1) {
      for (int j = 0; j < n; ++j) {
        if (sequence[j] == e) {
          val = j;
          break;
        }
      }
    }
    // cout << "val:" << val << endl;
    sequence.erase(sequence.begin() + val);
    sequence.insert(sequence.begin(), e);
    // for (int i = 0; i < n; i++) {
    //     cout << sequence[i] << endl;
    // }
    index++;
  }

  for (int i = 0; i < n; i++) {
    cout << sequence[i] << endl;
  }

  return 0;
}