Problem p03841 (C++)

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

using namespace std;

typedef long long ll;

const int __SIZE = 1 << 18;
char ibuf[__SIZE], *iS, *iT;

#define ge                                                                     \
  (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),               \
               (iS == iT ? EOF : *iS++))                                       \
            : *iS++)
#define ri read_int()
#define rl read_ll()
#define FILE(s) freopen(s "in", "r", stdin), freopen(s "out", "w", stdout)

template <typename T> inline void read(T &x) {
  char ch, t = 0;
  x = 0;
  while (!isdigit(ch = ge))
    t |= ch == '-';
  while (isdigit(ch))
    x = x * 10 + (ch ^ 48), ch = ge;
  x = t ? -x : x;
}
inline int read_int() {
  int x;
  return read(x), x;
}
inline ll read_ll() {
  ll x;
  return read(x), x;
}

template <typename T> inline void chkmin(T &a, T b) { a = a < b ? a : b; }

const int MAXN = 300000;

int p[MAXN];
int vis[MAXN];
int ans[MAXN];
int cnt[MAXN];
int pos[MAXN];

int main() {
#ifndef ONLINE_JUDGE
  FILE("");
#endif
  int n = ri;
  for (int i = 1; i <= n; i++) {
    int x = p[i] = ri;
    if (vis[x]) {
      puts("No");
      return 0;
    }
    cnt[x] = i;
    vis[x] = 1;
    ans[x] = i;
    pos[x] = i;
  }
  int N = n * n;
  for (int i = 1; i <= N; i++)
    cnt[i] += cnt[i - 1];
  for (int i = 1; i <= N; i++)
    if (cnt[i] > i) {
      puts("No");
      return 0;
    }
  int p = 1;
  for (int i = 1; i <= N; i++) {
    if (ans[i]) {
      int c = ans[i] - 1;
      while (c--) {
        while (ans[p])
          ++p;
        if (p > i) {
          puts("No");
          return 0;
        }
        ans[p] = ans[i];
      }
    }
  }
  p = N;
  for (int i = N; i >= 1; i--) {
    if (pos[i]) {
      int c = n - pos[i];
      while (c--) {
        while (ans[p])
          --p;
        if (p < i) {
          puts("No");
          return 0;
        }
        ans[p] = ans[i];
      }
    }
  }
  puts("Yes");
  for (int i = 1; i <= N; i++)
    printf("%d ", ans[i]);
  return puts(""), 0;
}

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

using namespace std;

typedef long long ll;

const int __SIZE = 1 << 18;
char ibuf[__SIZE], *iS, *iT;

#define ge                                                                     \
  (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),               \
               (iS == iT ? EOF : *iS++))                                       \
            : *iS++)
#define ri read_int()
#define rl read_ll()
#define FILE(s) freopen(s "in", "r", stdin), freopen(s "out", "w", stdout)

template <typename T> inline void read(T &x) {
  char ch, t = 0;
  x = 0;
  while (!isdigit(ch = ge))
    t |= ch == '-';
  while (isdigit(ch))
    x = x * 10 + (ch ^ 48), ch = ge;
  x = t ? -x : x;
}
inline int read_int() {
  int x;
  return read(x), x;
}
inline ll read_ll() {
  ll x;
  return read(x), x;
}

template <typename T> inline void chkmin(T &a, T b) { a = a < b ? a : b; }

const int MAXN = 300000;

int p[MAXN];
int vis[MAXN];
int ans[MAXN];
int cnt[MAXN];
int pos[MAXN];

int main() {
  int n = ri;
  for (int i = 1; i <= n; i++) {
    int x = p[i] = ri;
    if (vis[x]) {
      puts("No");
      return 0;
    }
    cnt[x] = i;
    vis[x] = 1;
    ans[x] = i;
    pos[x] = i;
  }
  int N = n * n;
  for (int i = 1; i <= N; i++)
    cnt[i] += cnt[i - 1];
  for (int i = 1; i <= N; i++)
    if (cnt[i] > i) {
      puts("No");
      return 0;
    }
  int p = 1;
  for (int i = 1; i <= N; i++) {
    if (ans[i]) {
      int c = ans[i] - 1;
      while (c--) {
        while (ans[p])
          ++p;
        if (p > i) {
          puts("No");
          return 0;
        }
        ans[p] = ans[i];
      }
    }
  }
  p = N;
  for (int i = N; i >= 1; i--) {
    if (pos[i]) {
      int c = n - pos[i];
      while (c--) {
        while (ans[p])
          --p;
        if (p < i) {
          puts("No");
          return 0;
        }
        ans[p] = ans[i];
      }
    }
  }
  puts("Yes");
  for (int i = 1; i <= N; i++)
    printf("%d ", ans[i]);
  return puts(""), 0;
}