Problem p01449 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define sz size()
#define pb push_back
#define mp make_pair
#define ALL(X) (X).begin(), (X).end()

using namespace std;

const int INF = 1000000000;
const double eps = 1e-8;

int dijkstra(const vector<vector<int>> edge) {
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  vector<int> dist(edge.size(), INF);
  dist[0] = 0;
  q.push(mp(0, 0));
  while (!q.empty()) {
    auto p = q.top();
    q.pop();
    if (p.first > dist[p.second])
      break;
    for (int i = 0; i < edge[p.second].size(); ++i) {
      int to = edge[p.second][i];
      if (dist[to] > dist[p.second] + 1) {
        dist[to] = dist[p.second] + 1;
        q.push(mp(dist[to], to));
      }
    }
  }
  return dist[edge.size() - 1];
}

int main(void) {
  int n;
  cin >> n;
  vector<int> ary(n);
  vector<int> jmp(n);
  vector<int> ok(n, 0);
  REP(i, n) cin >> ary[i];
  REP(i, n) {
    if (ary[i] != 0) {
      if (ok[i] == 0) {
        set<int> s;
        s.insert(i);
        int j = i;
        bool f = true;
        while (f) {
          j += ary[j];
          f = false;
          switch (ok[j]) {
          case 0:
            if (s.find(j) != s.end()) {
              for (int k : s)
                ok[k] = -1;
            } else if (ary[j] == 0) {
              jmp[j] = j;
              ok[j] = 1;
              for (int k : s) {
                ok[k] = 1;
                jmp[k] = j;
              }
            } else {
              s.insert(j);
              f = true;
            }
            break;
          case 1:
            for (int k : s) {
              ok[k] = 1;
              jmp[k] = jmp[j];
            }
            break;
          case -1:
            for (int k : s)
              ok[k] = -1;
            break;
          }
        }
      }
    } else {
      ok[i] = 1;
      jmp[i] = i;
    }
  }
  vector<vector<int>> edge(n);
  REP(i, n) {
    if (ok[i] >= 0) {
      FOR(j, 1, 7) {
        if (i + j >= n)
          edge[i].push_back(n - 1);
        else if (ok[i + j] >= 0)
          edge[i].push_back(jmp[i + j]);
      }
    }
  }
  int l = dijkstra(edge);
  cout << l << endl;
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr: __pos (which is 3) > this->size() (which is 1)


Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define sz size()
#define pb push_back
#define mp make_pair
#define ALL(X) (X).begin(), (X).end()

using namespace std;

const int INF = 1000000000;
const double eps = 1e-8;

int dayid(string str) {
  int m = atoi(str.substr(0, 2).c_str());
  int d = atoi(str.substr(3, 2).c_str());
  return m * 40 + d;
}

int main(void) {
  while (1) {
    int n;
    cin >> n;
    if (!n)
      break;
    vector<int> times(1);
    map<int, int> pger;
    int gdsin = -1;
    REP(i, n) {
      string day, time, io;
      int id;
      cin >> day >> time >> io >> id;
      int timet = atoi(time.substr(0, 2).c_str()) * 60 +
                  atoi(time.substr(3, 2).c_str());
      if (id == 0) {
        if (io == "I") {
          gdsin = timet;
        } else {
          for (map<int, int>::iterator p = pger.begin(); p != pger.end(); ++p) {
            int in = max(p->second, gdsin);
            times[p->first] += timet - in;
          }
          gdsin = -1;
        }
      } else {
        if (io == "I") {
          if (id >= times.size()) {
            times.resize(id + 1);
            // times[id] = 0;
          }
          pger.insert(make_pair(id, timet));
        } else {
          map<int, int>::iterator itr = pger.find(id);
          if (gdsin >= 0) {
            int in = max(itr->second, gdsin);
            times[id] += timet - in;
          }
          pger.erase(itr);
        }
      }
    }
    int m = 0;
    REP(i, times.size()) { m = max(m, times[i]); }
    cout << m << endl;
  }
  return 0;
}