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

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

// range MAXIMUM query
class RMQ {
private:
  static const int MAX_N = 100000 * 2;
  int n;
  // score, -team_id
  pii dat[2 * MAX_N - 1];
  pii rec(int a, int b, int k, int l, int r) {
    if (b <= l || r <= a) {
      return {0, 0};
    }

    if (a <= l && r <= b) {
      return dat[k];
    } else {
      pii rl = rec(a, b, 2 * k + 1, l, (l + r) / 2);
      pii rr = rec(a, b, 2 * k + 2, (l + r) / 2, r);
      return max(rl, rr);
    }
  }

public:
  void init(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;

    for (int i = 0; i < 2 * n - 1; i++) {
      dat[i] = {0, 0};
    }
  }

  void update(int k, int a) {
    int reaf = k + n - 1;
    dat[reaf] = {dat[reaf].first + a, -k};
    while (reaf > 0) {
      reaf = (reaf - 1) / 2;
      dat[reaf] = max(dat[reaf * 2 + 1], dat[2 * reaf + 2]);
    }
  }

  pii query(int a, int b) { return rec(a, b + 1, 0, 0, n); }
};

int main() {
  int team_n, record_n, end_time;
  cin >> team_n >> record_n >> end_time;

  RMQ r;
  r.init(team_n);
  REP(i, team_n) { r.update(i, 0); }

  vi top(team_n);
  int btime = 0;
  REP(i, record_n) {
    int team_id, time, point;
    cin >> team_id >> time >> point;
    team_id--;

    pii t = r.query(0, team_n - 1);
    top[-t.second] += time - btime;
    btime = time;
    //        LOG("--> %d [%d] (%d)\\n", t.first, -t.second, time - btime);

    r.update(team_id, point);
  }
  pii t = r.query(0, team_n - 1);
  top[-t.second] += end_time - btime;
  cout << (max_element(ALL(top)) - top.begin() + 1) << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

// range MAXIMUM query
class RMQ {
private:
  static const int MAX_N = 100000;
  int n;
  // score, -team_id
  pii dat[2 * MAX_N - 1];
  pii rec(int a, int b, int k, int l, int r) {
    if (b <= l || r <= a) {
      return {0, 0};
    }

    if (a <= l && r <= b) {
      return dat[k];
    } else {
      pii rl = rec(a, b, 2 * k + 1, l, (l + r) / 2);
      pii rr = rec(a, b, 2 * k + 2, (l + r) / 2, r);
      return max(rl, rr);
    }
  }

public:
  void init(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;

    for (int i = 0; i < 2 * n - 1; i++) {
      dat[i] = {0, 0};
    }
  }

  void update(int k, int a) {
    int reaf = k + n - 1;
    dat[reaf] = {dat[reaf].first + a, -k};
    while (reaf > 0) {
      reaf = (reaf - 1) / 2;
      dat[reaf] = max(dat[reaf * 2 + 1], dat[2 * reaf + 2]);
    }
  }

  pii query(int a, int b) { return rec(a, b + 1, 0, 0, n); }
};

int main() {
  int team_n, record_n, end_time;
  cin >> team_n >> record_n >> end_time;

  RMQ r;
  r.init(team_n);
  REP(i, team_n) { r.update(i, 0); }

  vi top(team_n);
  int btime = 0;
  REP(i, record_n) {
    int team_id, time, point;
    cin >> team_id >> time >> point;
    team_id--;

    pii t = r.query(0, team_n - 1);
    top[-t.second] += time - btime;
    btime = time;
    //        LOG("--> %d [%d] (%d)\\n", t.first, -t.second, time - btime);

    r.update(team_id, point);
  }
  pii t = r.query(0, team_n - 1);
  top[-t.second] += end_time - btime;
  cout << (max_element(ALL(top)) - top.begin() + 1) << endl;
}