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

#include <algorithm>
#include <iostream>
#include <map>
#define f first
#define s second
#define MAX_T 100005
using namespace std;

typedef pair<int, int> P;
P teamp[MAX_T * 4 - 1];
P teamt[MAX_T * 4 - 1];
void init(int n);
void updateP(int k, int a, int n);
void updateT(int k, int a, int n);

int main() {
  int N, R, L;
  int d, t, x;
  int befd = 0, beft = 0, nt, n = 1;
  cin >> N >> R >> L;
  while (n < N)
    n *= 2;
  init(n);
  for (int i = 0; i < R; i++) {
    cin >> d >> t >> x;
    updateP(d - 1, x, n);
    nt = t - beft;
    beft = t;
    updateT(befd, nt, n);
    befd = teamp[0].s;
  }
  nt = L - beft;
  updateT(befd, nt, n);
  cout << teamt[0].s + 1 << endl;
}

void init(int n) {
  for (int i = 0; i < n * 2 - 1; i++) {
    teamp[i].f = 0;
    teamt[i].f = 0;
  }
  for (int i = n - 1; i < n * 2 - 1; i++) {
    teamp[i].s = i - (n - 1);
    teamt[i].s = i - (n - 1);
  }
  return;
}

void updateP(int k, int a, int n) {
  k += n - 1;
  teamp[k].f += a;
  while (k > 0) {
    k = (k - 1) / 2;
    if (teamp[k * 2 + 1].f >= teamp[k * 2 + 2].f) {
      teamp[k].f = teamp[k * 2 + 1].f;
      teamp[k].s = teamp[k * 2 + 1].s;
    } else {
      teamp[k].f = teamp[k * 2 + 2].f;
      teamp[k].s = teamp[k * 2 + 2].s;
    }
  }
  return;
}

void updateT(int k, int a, int n) {
  k += n - 1;
  teamt[k].f += a;
  while (k > 0) {
    k = (k - 1) / 2;
    if (teamt[k * 2 + 1].f >= teamt[k * 2 + 2].f) {
      teamt[k].f = teamt[k * 2 + 1].f;
      teamt[k].s = teamt[k * 2 + 1].s;
    } else {
      teamt[k].f = teamt[k * 2 + 2].f;
      teamt[k].s = teamt[k * 2 + 2].s;
    }
  }
  return;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#define f first
#define s second
#define MAX_T 100005
using namespace std;

typedef pair<int, int> P;
P teamp[MAX_T * 2 - 1];
P teamt[MAX_T * 2 - 1];
void init(int n);
void updateP(int k, int a, int n);
void updateT(int k, int a, int n);

int main() {
  int N, R, L;
  int d, t, x;
  int befd = 0, beft = 0, nt, n = 1;
  cin >> N >> R >> L;
  while (n < N)
    n *= 2;
  init(n);
  for (int i = 0; i < R; i++) {
    cin >> d >> t >> x;
    updateP(d - 1, x, n);
    nt = t - beft;
    beft = t;
    updateT(befd, nt, n);
    befd = teamp[0].s;
  }
  nt = L - beft;
  updateT(befd, nt, n);
  cout << teamt[0].s + 1 << endl;
}

void init(int n) {
  for (int i = 0; i < n * 2 - 1; i++) {
    teamp[i].f = 0;
    teamt[i].f = 0;
  }
  for (int i = n - 1; i < n * 2 - 1; i++) {
    teamp[i].s = i - (n - 1);
    teamt[i].s = i - (n - 1);
  }
  return;
}

void updateP(int k, int a, int n) {
  k += n - 1;
  teamp[k].f += a;
  while (k > 0) {
    k = (k - 1) / 2;
    if (teamp[k * 2 + 1].f >= teamp[k * 2 + 2].f) {
      teamp[k].f = teamp[k * 2 + 1].f;
      teamp[k].s = teamp[k * 2 + 1].s;
    } else {
      teamp[k].f = teamp[k * 2 + 2].f;
      teamp[k].s = teamp[k * 2 + 2].s;
    }
  }
  return;
}

void updateT(int k, int a, int n) {
  k += n - 1;
  teamt[k].f += a;
  while (k > 0) {
    k = (k - 1) / 2;
    if (teamt[k * 2 + 1].f >= teamt[k * 2 + 2].f) {
      teamt[k].f = teamt[k * 2 + 1].f;
      teamt[k].s = teamt[k * 2 + 1].s;
    } else {
      teamt[k].f = teamt[k * 2 + 2].f;
      teamt[k].s = teamt[k * 2 + 2].s;
    }
  }
  return;
}