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

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

#define MAX_N (100001)
#define MAX_R (1000000)
#define max(x, y) ((x) > (y) ? (x) : (y))

void init(int data[], int n);
void updata(int data[], int k, int a, int n);
int query(int data[]);
int such(int data[], int k, int n);

int n_;

typedef struct {
  int d, t, x;
} Rec;

int main() {
  int score[4 * MAX_N - 1], time[4 * MAX_N - 1];
  int r, l, n;

  scanf("%d %d %d", &n, &r, &l);

  n_ = n;
  n = 1;
  while (n < n_)
    n *= 2;

  init(score, n);
  init(time, n);

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

    scanf("%d %d %d", &d, &t_a, &x);

    k = such(score, 0, n);

    if (t_b == 0)
      t = t_a;
    else
      t = t_a - t_b;
    t_b = t_a;

    // printf("k=%d\\n",k);
    updata(time, k, time[k + n - 2] + t, n);

    // printf("b\\n");
    updata(score, d, score[d + n - 2] + x, n);

    /*printf("c");

    printf("\\n---------------\\n");

    printf("k = %d\\n",k);
    for(int i=0;i<n-1;i++){
      printf("%d : score %d  time %d\\n",i,score[i],time[i]);
    }
    printf("-  -  -  -\\n");
    for(int i=n-1;i<n*2-1;i++){
      printf("%d : score %d  time %d\\n",i,score[i],time[i]);
      }*/
  }

  k = such(score, 0, n);
  t = l - t_b;
  updata(time, k, time[k + n - 2] + t, n);

  /*
  printf("\\n---------------\\n");

  printf("k = %d\\n",k);
  for(int i=0;i<n;i++){
    printf("%d : score %d  time %d\\n",i,score[i],time[i]);
  }
  printf("-  -  -  -\\n");
  for(int i=n;i<n*2;i++){
    printf("%d : score %d  time %d\\n",i,score[i],time[i]);
    }*/

  printf("%d\\n", such(time, 0, n));
}

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

void updata(int data[], int k, int a, int n) {

  k = k + n - 2;
  data[k] = a;

  while (k > 0) {
    k = (k - 1) / 2;

    data[k] = max(data[k * 2 + 1], data[k * 2 + 2]);
  }

  return;
}

int query(int data[]) { return data[0]; }

int such(int data[], int k, int n) {

  if (k >= n - 1)
    return k - n + 2;

  if (data[k] == data[k * 2 + 1])
    return such(data, k * 2 + 1, n);
  else if (data[k] == data[k * 2 + 2])
    return such(data, k * 2 + 2, n);
}

Error: 0

Fixed submission:

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

#define MAX_N (100001)
#define MAX_R (1000000)
#define max(x, y) ((x) > (y) ? (x) : (y))

void init(int data[], int n);
void updata(int data[], int k, int a, int n);
int query(int data[]);
int such(int data[], int k, int n);

int n_;

typedef struct {
  int d, t, x;
} Rec;

int main() {
  int score[4 * MAX_N - 1], time[4 * MAX_N - 1];
  int r, l, n;

  scanf("%d %d %d", &n, &r, &l);

  n_ = n;
  n = 1;
  while (n < n_)
    n *= 2;

  init(score, n);
  init(time, n);

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

    scanf("%d %d %d", &d, &t_a, &x);

    k = such(score, 0, n);

    if (t_b == 0)
      t = t_a;
    else
      t = t_a - t_b;
    t_b = t_a;

    // printf("k=%d\\n",k);
    updata(time, k, time[k + n - 2] + t, n);

    // printf("b\\n");
    updata(score, d, score[d + n - 2] + x, n);

    /*printf("c");

    printf("\\n---------------\\n");

    printf("k = %d\\n",k);
    for(int i=0;i<n-1;i++){
      printf("%d : score %d  time %d\\n",i,score[i],time[i]);
    }
    printf("-  -  -  -\\n");
    for(int i=n-1;i<n*2-1;i++){
      printf("%d : score %d  time %d\\n",i,score[i],time[i]);
      }*/
  }

  k = such(score, 0, n);
  t = l - t_b;
  updata(time, k, time[k + n - 2] + t, n);

  /*
  printf("\\n---------------\\n");

  printf("k = %d\\n",k);
  for(int i=0;i<n;i++){
    printf("%d : score %d  time %d\\n",i,score[i],time[i]);
  }
  printf("-  -  -  -\\n");
  for(int i=n;i<n*2;i++){
    printf("%d : score %d  time %d\\n",i,score[i],time[i]);
    }*/

  printf("%d\\n", such(time, 0, n));
}

void init(int data[], int n) {
  for (int i = 0; i < n * 4 - 1; i++) {
    data[i] = 0;
  }
  return;
}

void updata(int data[], int k, int a, int n) {

  k = k + n - 2;
  data[k] = a;

  while (k > 0) {
    k = (k - 1) / 2;

    data[k] = max(data[k * 2 + 1], data[k * 2 + 2]);
  }

  return;
}

int query(int data[]) { return data[0]; }

int such(int data[], int k, int n) {

  if (k >= n_)
    return k - n + 2;

  if (data[k] == data[k * 2 + 1])
    return such(data, k * 2 + 1, n);
  if (data[k] == data[k * 2 + 2])
    return such(data, k * 2 + 2, n);
}