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

#include <algorithm>
#include <climits>
#include <cstdio>
using namespace std;

#define MAX_SIZE (1 << 17)

struct team {
  int num, score, time;
  team *self;
  bool operator<(const team &t) const {
    if (this->score < t.score)
      return true;
    if (this->score == t.score && this->num > t.num)
      return true;
    return false;
  }
};

int n, r, l;
team teams[MAX_SIZE * 2 - 1];

void init() {
  int n_ = 1;

  while (n_ < n)
    n_ *= 2;
  for (int i = n; i < n_; i++) {
    teams[i + n_ - 1].score = INT_MIN;
  }
  n = n_;
  for (int i = 0; i < n; i++) {
    teams[i + n - 1].num = i + 1;
    teams[i + n - 1].self = teams + i + n - 1;
  }
  for (int i = n - 2; i >= 0; i--) {
    teams[i] = max(teams[i * 2 + 1], teams[i * 2 + 2]);
  }
#if 0
    int t = 2;
    for (int i = 0; i < n * 2 - 1; i++){
        printf("%d:%d ", teams[i].num, teams[i].score);
        if (i + 2 == t){
            puts("");
            t *= 2;
        }
    }
#endif
}

void update(int d, int t, int x) {
  static int last;

  teams[0].self->time += t - last;
  last = t;

  d += n - 2;
  teams[d].score += x;
  while (d > 0) {
    d = (d - 1) / 2;
    teams[d] = max(teams[d * 2 + 1], teams[d * 2 + 2]);
  }
#if 0
    t = 2;
    for (int i = 0; i < n * 2 - 1; i++){
        printf("%d:%d:%d ", teams[i].num, teams[i].score, teams[i].time);
        if (i + 2 == t){
            puts("");
            t *= 2;
        }
    }
#endif
}

int main() {
  scanf("%d %d %d", &n, &r, &l);
  init();

  for (int i = 0; i < r; i++) {
    int d, t, x;

    scanf("%d %d %d", &d, &t, &x);
    update(d, t, x);
  }
  update(1, l, 0);

  int maxi = 0;
  for (int i = 1; i < n; i++) {
    if (teams[maxi + n - 1].time < teams[i + n - 1].time)
      maxi = i;
  }
  printf("%d\\n", maxi + 1);

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

struct team {
  int id, score, time;
};
typedef pair<int, int> P;

P seg[100001 * 2];
team prog[100001];
int N, R, L;

void init();
void update(int key, int data, int t);

int main() {
  scanf("%d %d %d", &N, &R, &L);
  init();
  int last = 0;
  while (R-- > 0) {
    int d, t, x;

    scanf("%d %d %d", &d, &t, &x);
    update(d - 1, x, t);
  }
  update(0, 0, L);
  int maxi = 0;
  for (int i = 1; i < N; i++) {
    if (prog[maxi].time < prog[i].time)
      maxi = i;
  }
  printf("%d\\n", prog[maxi].id);
}

void init() {
  int n = 1;

  while (n < N)
    n *= 2;
  for (int i = 0; i < N; i++)
    prog[i].id = i + 1;
  for (int i = N; i < n; i++)
    prog[i].id = INT_MAX;
  for (int i = n - 1; i <= (n - 1) * 2; i++)
    seg[i].second = i - n + 2;
  N = n;
}

void update(int key, int data, int t) {
  static int last = 0;
  static int l_id = 1;

  seg[key + N - 1].first += data;
  prog[l_id - 1].time += t - last;

  for (int c = key + N - 1; c != 0; c = (c - 1) / 2) {
    int other = c % 2 == 0 ? c - 1 : c + 1;

    if (seg[c].first > seg[other].first)
      seg[(c - 1) / 2] = seg[c];
    else if (seg[c].first < seg[other].first)
      seg[(c - 1) / 2] = seg[other];
    else if (seg[c].second < seg[other].second)
      seg[(c - 1) / 2] = seg[c];
    else
      seg[(c - 1) / 2] = seg[other];
  }

  last = t;
  l_id = seg[0].second;
}