Problem p03841 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X)                                                                 \
  int(X);                                                                      \
  scanf("%d", &X)
#define DRII(X, Y)                                                             \
  int X, Y;                                                                    \
  scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z)                                                         \
  int X, Y, Z;                                                                 \
  scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET                                                                  \
  int ___T, case_n = 1;                                                        \
  scanf("%d ", &___T);                                                         \
  while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define VPII vector<pair<int, int>>
#define F first
#define S second
#define RF(x) freopen(x, "r", stdin)
#define WF(x) freopen(x, "w", stdout)
typedef long long LL;
using namespace std;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PII;
const LL MOD = 1e9 + 7;
const int SIZE = 1e6 + 5;
const LL INF = 1LL << 58;
const double eps = 1e-13;

int r[509], l[509];
PII x[509]; //
int ans[25009];
vector<int> lv, rv;
vector<int> pos[509];

int main() {
  DRI(n);
  REP(i, n) {
    RI(x[i].F);
    x[i].F--;
    ans[x[i].F] = i + 1;
    x[i].S = i;
  }
  sort(x, x + n);
  /*REP(i,n){
          l[i]=x[i].S;
          if(i)l[i]+=l[i-1];
          if(l[i]>x[i].F-i){
                  puts("No");return 0;
          }
  }
  for(int i=n-1;i>=0;i--){
          r[i]=n-1-x[i].S;
          if(i<n-1)r[i]+=r[i+1];
          if(r[i]>n*n-1-x[i].F-(n-1-i)){
                  puts("No");return 0;
          }
  }*/
  REP(i, n) { REP(j, x[i].S) lv.PB(x[i].S + 1); }
  for (int i = n - 1; i >= 0; i--) {
    REP(j, n - 1 - x[i].S) rv.PB(x[i].S + 1);
  }
  int li = 0, ri = 0;
  REP(i, n * n) {
    if (ans[i] == 0 && li < SZ(lv))
      ans[i] = lv[li++];
  }
  for (int i = n * n - 1; i >= 0; i--) {
    if (ans[i] == 0 && ri < SZ(rv))
      ans[i] = rv[ri++];
  }
  REP(i, n * n) {
    if (ans[i] - 1 < 0) {
      puts("No");
      return 0;
    }
    pos[ans[i] - 1].PB(i);
  }
  REP(i, n) {
    if (SZ(pos[x[i].S]) < n || pos[x[i].S][x[i].S] != x[i].F) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
  REP(i, n * n) { printf("%d ", ans[i]); }
}


Fixed submission:
#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X)                                                                 \
  int(X);                                                                      \
  scanf("%d", &X)
#define DRII(X, Y)                                                             \
  int X, Y;                                                                    \
  scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z)                                                         \
  int X, Y, Z;                                                                 \
  scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET                                                                  \
  int ___T, case_n = 1;                                                        \
  scanf("%d ", &___T);                                                         \
  while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define VPII vector<pair<int, int>>
#define F first
#define S second
#define RF(x) freopen(x, "r", stdin)
#define WF(x) freopen(x, "w", stdout)
typedef long long LL;
using namespace std;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PII;
const LL MOD = 1e9 + 7;
const int SIZE = 1e6 + 5;
const LL INF = 1LL << 58;
const double eps = 1e-13;

int r[509], l[509];
PII x[509]; //
int ans[250009];
vector<int> lv, rv;
vector<int> pos[509];

int main() {
  DRI(n);
  REP(i, n) {
    RI(x[i].F);
    x[i].F--;
    ans[x[i].F] = i + 1;
    x[i].S = i;
  }
  sort(x, x + n);
  /*REP(i,n){
          l[i]=x[i].S;
          if(i)l[i]+=l[i-1];
          if(l[i]>x[i].F-i){
                  puts("No");return 0;
          }
  }
  for(int i=n-1;i>=0;i--){
          r[i]=n-1-x[i].S;
          if(i<n-1)r[i]+=r[i+1];
          if(r[i]>n*n-1-x[i].F-(n-1-i)){
                  puts("No");return 0;
          }
  }*/
  REP(i, n) { REP(j, x[i].S) lv.PB(x[i].S + 1); }
  for (int i = n - 1; i >= 0; i--) {
    REP(j, n - 1 - x[i].S) rv.PB(x[i].S + 1);
  }
  int li = 0, ri = 0;
  REP(i, n * n) {
    if (ans[i] == 0 && li < SZ(lv))
      ans[i] = lv[li++];
  }
  for (int i = n * n - 1; i >= 0; i--) {
    if (ans[i] == 0 && ri < SZ(rv))
      ans[i] = rv[ri++];
  }
  REP(i, n * n) {
    if (ans[i] - 1 < 0) {
      puts("No");
      return 0;
    }
    pos[ans[i] - 1].PB(i);
  }
  REP(i, n) {
    if (SZ(pos[x[i].S]) < n || pos[x[i].S][x[i].S] != x[i].F) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
  REP(i, n * n) { printf("%d ", ans[i]); }
}