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



// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2242

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

struct Era {

  int from;
  int to;
  string name;

  Era(string era, int w, int g) : name(era), from(g - w), to(g) {}

  bool isEra(int g) { return from < g && to >= g; }

  void print(int g) { cout << name << " " << g - from << endl; }
};

int main() {
  int N, Q;

  while (cin >> N >> Q && N) {
    vector<Era> eras;

    string name;
    int year_w, year_g;
    rep(nth, N) {
      cin >> name >> year_w >> year_g;
      eras.push_back(Era(name, year_w, year_g));
    }

    int q;
    rep(nth, Q) {
      cin >> q;

      bool flag = false;
      rep(i, eras.size()) {
        if (eras[i].isEra(q)) {
          eras[i].print(q);
          flag = true;
          break;
        }
      }
      if (!flag) {
        cout << "Unknown" << endl;
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:


// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2242

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

struct Era {

  int from;
  int to;
  string name;

  Era(string era, int w, int g) : name(era), from(g - w), to(g) {}

  bool isEra(int g) { return from < g && to >= g; }

  void print(int g) { cout << name << " " << g - from << endl; }
};

int main() {
  int N, Q;

  while (cin >> N >> Q && N) {
    vector<Era> eras;

    string name;
    int year_w, year_g;
    rep(nth, N) {
      cin >> name >> year_w >> year_g;
      for (int i = 0; i < year_w; i++) {
        eras.push_back(Era(name, year_w, year_g));
      }
    }

    int q;
    rep(nth, Q) {
      cin >> q;

      bool flag = false;
      rep(i, eras.size()) {
        if (eras[i].isEra(q)) {
          eras[i].print(q);
          flag = true;
          break;
        }
      }
      if (!flag) {
        cout << "Unknown" << endl;
      }
    }
  }
  return 0;
}