Problem p03271 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define maxn 100020
#define rep(i, l, r) for (register int i = l; i <= r; i++)
#define repd(i, r, l) for (register int i = r; i >= l; i--)
#define rvc(i, S) for (register int i = 0; i < (int)S.size(); i++)
#define rvcd(i, S) for (register int i = ((int)S.size()) - 1; i >= 0; i--)
#define fore(i, x) for (register int i = head[x]; i; i = e[i].next)
#define pb push_back
#define prev prev_
#define stack stack_
#define mp make_pair
#define fi first
#define se second
#define inf 0x3f3f3f3f
typedef long long ll;
typedef pair<int, int> pr;

int p[maxn], n, tag[maxn], go[maxn], id[maxn];

bool check() {
  memset(go, -1, sizeof(go));
  memset(tag, -1, sizeof(tag));
  rep(i, 1, n) {
    if (id[i] == i)
      go[i] = 0;
    else if (i > id[i])
      go[i] = 1;
    else
      go[i] = 2;
  }
  repd(i, n, 1) {
    if (!go[i])
      continue;
    if (abs(id[i] - i) & 1)
      return 0;
    if (id[i] > i) {
      go[i] = 2;
      for (int j = id[i]; j > i; j -= 2) {
        if (tag[j] != -1 && tag[j] != 0)
          return 0;
        if (go[j] == 0)
          return 0;
        if (tag[j] != -1)
          break;
        tag[j] = 0;
        tag[j - 1] = 1;
      }
      if (go[i] == 0 || tag[i] == 1)
        return 0;
    } else {
      go[i] = 1;
      for (int j = id[i]; j < i; j += 2) {
        if (tag[j] != -1 && tag[j] != 0)
          return 0;
        if (go[j] == 0)
          return 0;
        if (tag[j] != -1)
          break;
        tag[j] = 0;
        tag[j + 1] = 1;
      }
      if (go[i] == 0 || tag[i] == 1)
        return 0;
    }
  }
  int last = 0;
  rep(i, 1, n) if (go[p[i]] == 1) {
    if (last > p[i])
      return 0;
    last = p[i];
  }
  last = 0;
  rep(i, 1, n) if (go[p[i]] == 2) {
    if (last > p[i])
      return 0;
    last = p[i];
  }
  return 1;
}
int main() {
  scanf("%d", &n);
  rep(i, 1, n) scanf("%d", &p[i]), id[p[i]] = i;
  if (check())
    printf("Yes\n");
  else
    printf("No\n");
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define maxn 300020
#define rep(i, l, r) for (register int i = l; i <= r; i++)
#define repd(i, r, l) for (register int i = r; i >= l; i--)
#define rvc(i, S) for (register int i = 0; i < (int)S.size(); i++)
#define rvcd(i, S) for (register int i = ((int)S.size()) - 1; i >= 0; i--)
#define fore(i, x) for (register int i = head[x]; i; i = e[i].next)
#define pb push_back
#define prev prev_
#define stack stack_
#define mp make_pair
#define fi first
#define se second
#define inf 0x3f3f3f3f
typedef long long ll;
typedef pair<int, int> pr;

int p[maxn], n, tag[maxn], go[maxn], id[maxn];

bool check() {
  memset(go, -1, sizeof(go));
  memset(tag, -1, sizeof(tag));
  rep(i, 1, n) {
    if (id[i] == i)
      go[i] = 0;
    else if (i > id[i])
      go[i] = 1;
    else
      go[i] = 2;
  }
  repd(i, n, 1) {
    if (!go[i])
      continue;
    if (abs(id[i] - i) & 1)
      return 0;
    if (id[i] > i) {
      go[i] = 2;
      for (int j = id[i]; j > i; j -= 2) {
        if (tag[j] != -1 && tag[j] != 0)
          return 0;
        if (go[j] == 0)
          return 0;
        if (tag[j] != -1)
          break;
        tag[j] = 0;
        tag[j - 1] = 1;
      }
      if (go[i] == 0 || tag[i] == 1)
        return 0;
    } else {
      go[i] = 1;
      for (int j = id[i]; j < i; j += 2) {
        if (tag[j] != -1 && tag[j] != 0)
          return 0;
        if (go[j] == 0)
          return 0;
        if (tag[j] != -1)
          break;
        tag[j] = 0;
        tag[j + 1] = 1;
      }
      if (go[i] == 0 || tag[i] == 1)
        return 0;
    }
  }
  int last = 0;
  rep(i, 1, n) if (go[p[i]] == 1) {
    if (last > p[i])
      return 0;
    last = p[i];
  }
  last = 0;
  rep(i, 1, n) if (go[p[i]] == 2) {
    if (last > p[i])
      return 0;
    last = p[i];
  }
  return 1;
}
int main() {
  scanf("%d", &n);
  rep(i, 1, n) scanf("%d", &p[i]), id[p[i]] = i;
  if (check())
    printf("Yes\n");
  else
    printf("No\n");
}