Problem p03745 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const int INF = 1e9;
const int MODk = 1e9 + 7;
using LL = long long;
const LL LINF = 1e18;
const double EPS = 1e-10;
using namespace std;
#define COUT(v) cout << (v) << endl
#define COUTDOUBLE(v) cout << std::setprecision(10) << (v) << endl;
#define CIN(n)                                                                 \
  int(n);                                                                      \
  cin >> (n)
#define LCIN(n)                                                                \
  LL(n);                                                                       \
  cin >> (n)
#define SCIN(n)                                                                \
  string(n);                                                                   \
  cin >> (n)
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)

#define FOREACH(x, a) for (auto &(x) : (a))

#define ALL(obj) (obj).begin(), (obj).end()

#define P pair<LL, LL>
#define I vector<int>
#define pb(v) push_back(v)
#define V vector
#define rt return
#define rmsame(a) sort(ALL(a)), a.erase(unique(ALL(a)), a.end())
#define ENDL cout << endl

typedef string::const_iterator State;
class PalseError {};
class Edge {
public:
  int from, to, value;
  Edge(LL a, LL b, LL c) {
    from = a;
    to = b;
    value = c;
  }
  Edge(LL a, LL b) {
    from = a;
    to = b;
  }
};
int main() {
  CIN(N);
  I vec;
  REP(a, N) {
    CIN(b);
    vec.push_back(b);
  }
  int ans = 0;
  int hoge = -1;
  if (vec.at(0) < vec.at(1)) {
    hoge = 1;
  }
  if (vec.at(0) > vec.at(1)) {
    hoge = 2;
  }
  if (vec.at(0) == vec.at(1)) {
    hoge = -1;
  }
  REP(a, N - 1) {
    if (hoge == 1 && vec.at(a) > vec.at(a + 1)) {
      ans++;
      hoge = -1;
      continue;
    }
    if (hoge == 2 && vec.at(a) < vec.at(a + 1)) {
      ans++;
      hoge = -1;
      continue;
    }
    if (vec.at(a) < vec.at(a + 1)) {
      hoge = 1;
    }
    if (vec.at(a) > vec.at(a + 1)) {
      hoge = 2;
    }
  }
  COUT(ans + 1);
  rt 0;
}


Fixed submission:
#include <bits/stdc++.h>
const int INF = 1e9;
const int MODk = 1e9 + 7;
using LL = long long;
const LL LINF = 1e18;
const double EPS = 1e-10;
using namespace std;
#define COUT(v) cout << (v) << endl
#define COUTDOUBLE(v) cout << std::setprecision(10) << (v) << endl;
#define CIN(n)                                                                 \
  int(n);                                                                      \
  cin >> (n)
#define LCIN(n)                                                                \
  LL(n);                                                                       \
  cin >> (n)
#define SCIN(n)                                                                \
  string(n);                                                                   \
  cin >> (n)
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)

#define FOREACH(x, a) for (auto &(x) : (a))

#define ALL(obj) (obj).begin(), (obj).end()

#define P pair<LL, LL>
#define I vector<int>
#define pb(v) push_back(v)
#define V vector
#define rt return
#define rmsame(a) sort(ALL(a)), a.erase(unique(ALL(a)), a.end())
#define ENDL cout << endl

typedef string::const_iterator State;
class PalseError {};
class Edge {
public:
  int from, to, value;
  Edge(LL a, LL b, LL c) {
    from = a;
    to = b;
    value = c;
  }
  Edge(LL a, LL b) {
    from = a;
    to = b;
  }
};
int main() {
  CIN(N);
  I vec;
  REP(a, N) {
    CIN(b);
    vec.push_back(b);
  }
  if (vec.size() == 1) {
    COUT(1);
    return 0;
  }
  int ans = 0;
  int hoge = -1;
  if (vec.at(0) < vec.at(1)) {
    hoge = 1;
  }
  if (vec.at(0) > vec.at(1)) {
    hoge = 2;
  }
  if (vec.at(0) == vec.at(1)) {
    hoge = -1;
  }
  REP(a, N - 1) {
    if (hoge == 1 && vec.at(a) > vec.at(a + 1)) {
      ans++;
      hoge = -1;
      continue;
    }
    if (hoge == 2 && vec.at(a) < vec.at(a + 1)) {
      ans++;
      hoge = -1;
      continue;
    }
    if (vec.at(a) < vec.at(a + 1)) {
      hoge = 1;
    }
    if (vec.at(a) > vec.at(a + 1)) {
      hoge = 2;
    }
  }
  COUT(ans + 1);
  rt 0;
}