Problem p03271 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
using namespace std;
const int inf = 0x7fffffff;
const double eps = 1e-10;
const double pi = acos(-1.0);
// char buf[1<<15],*S=buf,*T=buf;
// char getch(){return S==T&&(T=(S=buf)+fread(buf,1,1<<15,stdin),S==T)?0:*S++;}
inline int read() {
  int x = 0, f = 1;
  char ch;
  ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = 0;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch & 15);
    ch = getchar();
  }
  if (f)
    return x;
  else
    return -x;
}
using namespace std;
const int M = 3e5 + 10;
int idx[M], id[M], n;
vector<int> L, R;
bool cheak(int l, int r) {
  L.clear();
  R.clear();
  for (int i = l + 1; i <= r; i += 2)
    if (idx[i] != i)
      return 0;
  for (int i = l; i <= r; i += 2) {
    if (idx[i] <= i)
      R.push_back(idx[i]);
    else
      L.push_back(idx[i]);
  }
  for (int i = 0; i < L.size() - 1; i++)
    if (L[i] > L[i + 1])
      return 0;
  for (int i = 0; i < R.size() - 1; i++)
    if (R[i] > R[i + 1])
      return 0;
  return 1;
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    idx[i] = read();
    id[idx[i]] = i;
  }
  int nl = 1, nr = 1;
  for (int i = 1; i <= n;) {
    for (; i <= n; i++) {
      if (id[i] > nr)
        nr = id[i];
      if (i == nr)
        break;
    }
    if (!cheak(nl, nr)) {
      printf("No\n");
      return 0;
    }
    i = nl = nr + 1;
    ++nr;
  }
  printf("Yes\n");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
using namespace std;
const int inf = 0x7fffffff;
const double eps = 1e-10;
const double pi = acos(-1.0);
// char buf[1<<15],*S=buf,*T=buf;
// char getch(){return S==T&&(T=(S=buf)+fread(buf,1,1<<15,stdin),S==T)?0:*S++;}
inline int read() {
  int x = 0, f = 1;
  char ch;
  ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = 0;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch & 15);
    ch = getchar();
  }
  if (f)
    return x;
  else
    return -x;
}
using namespace std;
const int M = 3e5 + 10;
int idx[M], id[M], n;
vector<int> L, R;
bool cheak(int l, int r) {
  L.clear();
  R.clear();
  for (int i = l + 1; i <= r; i += 2)
    if (idx[i] != i)
      return 0;
  for (int i = l; i <= r; i += 2) {
    if (idx[i] <= i)
      R.push_back(idx[i]);
    else
      L.push_back(idx[i]);
  }
  if (L.size() >= 2)
    for (int i = 0; i < L.size() - 1; i++)
      if (L[i] > L[i + 1])
        return 0;
  if (R.size() >= 2)
    for (int i = 0; i < R.size() - 1; i++)
      if (R[i] > R[i + 1])
        return 0;
  return 1;
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    idx[i] = read();
    id[idx[i]] = i;
  }
  int nl = 1, nr = 1;
  for (int i = 1; i <= n;) {
    for (; i <= n; i++) {
      if (id[i] > nr)
        nr = id[i];
      if (i == nr)
        break;
    }
    if (!cheak(nl, nr)) {
      printf("No\n");
      return 0;
    }
    i = nl = nr + 1;
    ++nr;
  }
  printf("Yes\n");
  return 0;
}