Problem p03271 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define vel vector<long long>
#define vvel vector<vel>
#define veb vector<bool>
#define int long long
#define rep(i, n) for (long long i = 0; i < n; i++)
#define sor(v) sort(v.begin(), v.end())
#define mmax(a, b) a = max(a, b)
#define mmin(a, b) a = min(a, b)
#define mkp make_pair
#define pin pair<int, int>
#define V vector
#define fin(fl, s)                                                             \
  if (fl) {                                                                    \
    cout << s << endl;                                                         \
    return -1;                                                                 \
  }
long long p = 1000000007;
vel kai(200001, 1);
vel ink(200001, 1);
int le = (1 << 20) + 1;
vel BIT(le, 0);
vel uni(vel &list) {
  if (list.size() == 0) {
    return list;
  }
  sort(list.begin(), list.end());
  vel ql(1, list[0]);
  rep(i, list.size() - 1) {
    if (list[i] != list[i + 1]) {
      ql.push_back(list[i + 1]);
    }
  }
  list = ql;
  return ql;
}
int ru(int a, int r) {
  if (r == 0) {
    return 1;
  }
  int ans = ru(a, r / 2);
  ans *= ans;
  ans %= p;
  if (r % 2 == 1) {
    ans *= a;
  }
  return ans % p;
}
int inv(int a) { return ru(a, p - 2); }
void make_kai() {
  rep(i, 200000) { kai[i + 1] = (kai[i] * (i + 1)) % p; }
  rep(i, 200001) { ink[i] = inv(kai[i]); }
}
int com(int n, int r) {
  int ans = kai[n] * ink[r];
  ans %= p;
  ans *= ink[n - r];
  ans %= p;
  return ans;
}
int per(int n, int r) {
  int ans = kai[n] * ink[n - r];
  ans %= p;
  return ans;
}
signed main() {
  int n;
  cin >> n;
  vel p(n + 6);
  veb same(n + 6, true);
  p[0] = 0;
  p[1] = 1;
  p[2] = 2;
  rep(i, n) {
    cin >> p[i + 3];
    p[i + 3] += 2;
    same[i + 3] = (p[i + 3] == i + 3);
  }
  n += 3;
  rep(i, 3) { p[n + i] = n + i; }
  n += 3;
  vel wall(0);
  rep(i, n - 1) {
    if (!(same[i] ^ same[i + 1])) {
      wall.push_back(i + 1);
    }
    if (i) {
      fin(!(same[i - 1] || same[i] || same[i + 1]), "No")
    }
  }
  for (int i = 0; i + 1 < wall.size(); i++) {
    int st = wall[i];
    int to = wall[i + 1];
    vel all(to - st);
    vel samp(to - st);
    vel up(0);
    vel dow(0);
    for (int i = st; i < to; i++) {
      all[i - st] = p[i];
      samp[i - st] = i;
      if (p[i] < i) {
        dow.push_back(p[i]);
      } else if (p[i] > i) {
        up.push_back(p[i]);
      }
    }
    sor(all);
    fin(all != samp, "No");
    for (int i = 0; i + 1 < up.size(); i++) {
      fin(up[i] > up[i + 1], "No");
    }
    for (int i = 0; i + 1 < dow.size(); i++) {
      fin(dow[i] > dow[i + 1], "No");
    }
  }
  cout << "Yes" << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define vel vector<long long>
#define vvel vector<vel>
#define veb vector<bool>
#define int long long
#define rep(i, n) for (long long i = 0; i < n; i++)
#define sor(v) sort(v.begin(), v.end())
#define mmax(a, b) a = max(a, b)
#define mmin(a, b) a = min(a, b)
#define mkp make_pair
#define pin pair<int, int>
#define V vector
#define fin(fl, s)                                                             \
  if (fl) {                                                                    \
    cout << s << endl;                                                         \
    return 0;                                                                  \
  }
long long p = 1000000007;
vel kai(200001, 1);
vel ink(200001, 1);
int le = (1 << 20) + 1;
vel BIT(le, 0);
vel uni(vel &list) {
  if (list.size() == 0) {
    return list;
  }
  sort(list.begin(), list.end());
  vel ql(1, list[0]);
  rep(i, list.size() - 1) {
    if (list[i] != list[i + 1]) {
      ql.push_back(list[i + 1]);
    }
  }
  list = ql;
  return ql;
}
int ru(int a, int r) {
  if (r == 0) {
    return 1;
  }
  int ans = ru(a, r / 2);
  ans *= ans;
  ans %= p;
  if (r % 2 == 1) {
    ans *= a;
  }
  return ans % p;
}
int inv(int a) { return ru(a, p - 2); }
void make_kai() {
  rep(i, 200000) { kai[i + 1] = (kai[i] * (i + 1)) % p; }
  rep(i, 200001) { ink[i] = inv(kai[i]); }
}
int com(int n, int r) {
  int ans = kai[n] * ink[r];
  ans %= p;
  ans *= ink[n - r];
  ans %= p;
  return ans;
}
int per(int n, int r) {
  int ans = kai[n] * ink[n - r];
  ans %= p;
  return ans;
}
signed main() {
  int n;
  cin >> n;
  vel p(n + 6);
  veb same(n + 6, true);
  p[0] = 0;
  p[1] = 1;
  p[2] = 2;
  rep(i, n) {
    cin >> p[i + 3];
    p[i + 3] += 2;
    same[i + 3] = (p[i + 3] == i + 3);
  }
  n += 3;
  rep(i, 3) { p[n + i] = n + i; }
  n += 3;
  vel wall(0);
  rep(i, n - 1) {
    if (!(same[i] ^ same[i + 1])) {
      wall.push_back(i + 1);
    }
    if (i) {
      fin(!(same[i - 1] || same[i] || same[i + 1]), "No")
    }
  }
  for (int i = 0; i + 1 < wall.size(); i++) {
    int st = wall[i];
    int to = wall[i + 1];
    vel all(to - st);
    vel samp(to - st);
    vel up(0);
    vel dow(0);
    for (int i = st; i < to; i++) {
      all[i - st] = p[i];
      samp[i - st] = i;
      if (p[i] < i) {
        dow.push_back(p[i]);
      } else if (p[i] > i) {
        up.push_back(p[i]);
      }
    }
    sor(all);
    fin(all != samp, "No");
    for (int i = 0; i + 1 < up.size(); i++) {
      fin(up[i] > up[i + 1], "No");
    }
    for (int i = 0; i + 1 < dow.size(); i++) {
      fin(dow[i] > dow[i + 1], "No");
    }
  }
  cout << "Yes" << endl;
  return 0;
}