alexjercan/bugnet huggingface dataset:
Problem p00277 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// #define DEBUG

using namespace std;
int n, a[131100];
struct node {
  int id, point;
} node[1 << 18];

void add(int id, int x) {
  id = n - 1 + id;
  node[id].point += x;

  while (id > 0) {
    id = (id - 1) / 2;
    int c1 = 2 * id + 1, c2 = 2 * id + 2;
    if (node[c1].point >= node[c2].point)
      node[id] = node[c1];
    else
      node[id] = node[c2];
  }
}

void print() {
#ifdef DEBUG
  int k = 1;
  int x = 0;
  while (true) {
    for (int i = 0; i < k; ++i)
      printf("(%d %d)", node[x + i].id, node[x + i].point);
    puts("");
    x += k;
    k <<= 1;
    if (x >= n)
      break;
  }
  puts("");
#endif
}

int main() {
  int m, r, l;
  scanf("%d%d%d", &m, &r, &l);
  n = 1;
  while (n < m)
    n <<= 1;
  for (int i = 0; i < m; ++i)
    node[n - 1 + i].id = i;

  int pt = 0;
  for (int i = 0; i < r; ++i) {
    int d, t, x;
    scanf("%d%d%d", &d, &t, &x);

    if (pt < t) {
      int id = node[0].id;
      a[id] += t - pt;
    }
    add(d - 1, x);
    print();
    pt = t;
  }
  print();
  a[node[0].id] += l - pt;
#ifdef DEBUG
  for (int i = 0; i < n; ++i)
    printf("%d ", a[i]);
  puts("");
#endif
  printf("%d\\n", 1 + (max_element(a, a + n) - a));
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
// #define DEBUG

using namespace std;
int n, a[130625];
struct node {
  int id, point;
} node[1 << 18];

void add(int id, int x) {
  id = n - 1 + id;
  node[id].point += x;

  while (id > 0) {
    id = (id - 1) / 2;
    int c1 = 2 * id + 1, c2 = 2 * id + 2;
    if (node[c1].point >= node[c2].point)
      node[id] = node[c1];
    else
      node[id] = node[c2];
  }
}

void print() {
#ifdef DEBUG
  int k = 1;
  int x = 0;
  while (true) {
    for (int i = 0; i < k; ++i)
      printf("(%d %d)", node[x + i].id, node[x + i].point);
    puts("");
    x += k;
    k <<= 1;
    if (x >= n)
      break;
  }
  puts("");
#endif
}

int main() {
  int m, r, l;
  scanf("%d%d%d", &m, &r, &l);
  n = 1;
  while (n < m)
    n <<= 1;
  for (int i = 0; i < m; ++i)
    node[n - 1 + i].id = i;

  int pt = 0;
  for (int i = 0; i < r; ++i) {
    int d, t, x;
    scanf("%d%d%d", &d, &t, &x);

    if (pt < t) {
      int id = node[0].id;
      a[id] += t - pt;
    }
    add(d - 1, x);
    print();
    pt = t;
  }
  print();
  a[node[0].id] += l - pt;
#ifdef DEBUG
  for (int i = 0; i < n; ++i)
    printf("%d ", a[i]);
  puts("");
#endif
  printf("%d\\n", 1 + (max_element(a, a + n) - a));
}
Problem p00277 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <string.h>
using namespace std;

const int INF = 1 << 29;
typedef pair<int, int> P;

int n;
P team[2 * (1 << 20)];

void init(int _n) {
  n = 1;
  while (_n > n)
    n *= 2;
  for (int i = 0; i < 2 * n - 1; ++i) {
    team[i] = P(-INF, -INF);
  }
}

void update(int k, int a) {
  k += (n - 1);
  team[k] = P(a, k - n + 1);

  while (k > 0) {
    k = (k - 1) / 2;
    if (team[k * 2 + 1].first == team[k * 2 + 2].first) {
      if (team[k * 2 + 1].second < team[k * 2 + 2].second)
        team[k] = team[k * 2 + 1];
      else
        team[k] = team[k * 2 + 2];
    } else
      team[k] = max(team[k * 2 + 1], team[k * 2 + 2]);
  }
}

int main() {
  int p[100010] = {0};
  for (int i = 0; i < 100010; ++i)
    p[i] = -INF;
  int m, r, l;
  cin >> m >> r >> l;

  init(m);
  for (int i = 0; i < m; ++i) {
    update(i, 0);
    p[i] = 0;
  }
  int d, t, x, prev = 0;

  for (int i = 0; i < r; ++i) {
    scanf("%d %d %d", &d, &t, &x);
    p[team[0].second] += (t - prev);
    update(d - 1, team[d + n - 2].first + x);
    prev = t;
  }
  p[team[0].second] += (l - prev);

  int ans_max = -INF, ans;
  for (int i = 0; i < m; ++i) {
    if (ans_max < p[i]) {
      ans = i + 1;
      ans_max = p[i];
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

const int INF = 1 << 28;
typedef pair<long long, int> P;

int n;
P team[2 * 100001];

void init(int _n) {
  n = 1;
  while (_n > n)
    n *= 2;
  for (int i = 0; i < 2 * n - 1; ++i) {
    team[i] = P(-INF, -1000000);
  }
}

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

int main() {
  long long p[100010] = {0};
  int m, r, l;
  cin >> m >> r >> l;

  init(m);
  for (int i = 0; i < m; ++i)
    update(i, 0);
  int d, t, x, prev = 0;

  for (int i = 0; i < r; ++i) {
    scanf("%d %d %d", &d, &t, &x);
    p[-team[0].second] += (t - prev);
    update(d - 1, team[n + d - 2].first + x);
    prev = t;
  }
  p[-team[0].second] += (l - t);

  /*for(int i = 0; i < m; ++i)
      cout << p[i] << endl;*/
  int rank[100010] = {0}, ans;
  for (int i = 0; i < m; ++i)
    rank[i] = p[i];
  stable_sort(p, p + n, greater<int>());
  for (int i = 0; i < m; ++i)
    if (rank[i] == p[0])
      ans = i + 1;
  cout << ans << endl;
  return 0;
}
Problem p00277 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int a[400005][2];
int s[100005];
int main() {
  int i, j, k;
  int n, r, l;
  int d = 0;
  int t = 0;
  int x = 0;
  int nn = 0;
  int f = 0;
  memset(a, 0, sizeof(a));
  memset(s, 0, sizeof(s));
  cin >> n >> r >> l;
  nn = 1;
  while (nn < n) {
    nn *= 2;
  }
  nn--;
  for (i = 0; i < n; i++) {
    a[nn + i][1] = i;
  }
  d = nn;
  while (d != 0) {
    d--;
    if (a[d * 2 + 1][0] < a[d * 2 + 2][0]) {
      a[d][0] = a[d * 2 + 2][0];
      a[d][1] = a[d * 2 + 2][1];
    } else {
      a[d][0] = a[d * 2 + 1][0];
      a[d][1] = a[d * 2 + 1][1];
    }
  }
  for (i = 0; i < r; i++) {
    cin >> d >> t >> x;
    s[f] += t;
    d--;
    d += nn;
    a[d][0] += x;
    while (d != 0) {
      d = (d - 1) / 2;
      if (a[d * 2 + 1][0] < a[d * 2 + 2][0]) {
        a[d][0] = a[d * 2 + 2][0];
        a[d][1] = a[d * 2 + 2][1];
      } else {
        a[d][0] = a[d * 2 + 1][0];
        a[d][1] = a[d * 2 + 1][1];
      }
    }
    f = a[0][1];
    s[f] -= t;
  }
  s[f] += l;
  nn = 0;
  for (i = 0; i < n; i++) {
    if (s[nn] < s[i])
      nn = i;
  }
  nn++;
  cout << nn << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int a[200005][2];
int s[100005];
int main() {
  int i, j, k;
  int n, r, l;
  int d = 0;
  int t = 0;
  int x = 0;
  int nn = 0;
  int f = 0;
  memset(a, 0, sizeof(a));
  memset(s, 0, sizeof(s));
  cin >> n >> r >> l;
  nn = 1;
  while (nn < n) {
    nn *= 2;
  }
  nn--;
  for (i = 0; i < n; i++) {
    a[nn + i][1] = i;
  }
  d = nn;
  while (d != 0) {
    d--;
    if (a[d * 2 + 1][0] < a[d * 2 + 2][0]) {
      a[d][0] = a[d * 2 + 2][0];
      a[d][1] = a[d * 2 + 2][1];
    } else {
      a[d][0] = a[d * 2 + 1][0];
      a[d][1] = a[d * 2 + 1][1];
    }
  }
  for (i = 0; i < r; i++) {
    cin >> d >> t >> x;
    s[f] += t;
    d--;
    d += nn;
    a[d][0] += x;
    while (d != 0) {
      d = (d - 1) / 2;
      if (a[d * 2 + 1][0] < a[d * 2 + 2][0]) {
        a[d][0] = a[d * 2 + 2][0];
        a[d][1] = a[d * 2 + 2][1];
      } else {
        a[d][0] = a[d * 2 + 1][0];
        a[d][1] = a[d * 2 + 1][1];
      }
    }
    f = a[0][1];
    s[f] -= t;
  }
  s[f] += l;
  nn = 0;
  for (i = 0; i < n; i++) {
    if (s[nn] < s[i])
      nn = i;
  }
  nn++;
  cout << nn << endl;
}
Problem p00277 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <utility>
using namespace std;
int main() {
  int n, r, l, ans[100001] = {}, score[100001] = {};
  cin >> n >> r >> l;
  int last = 0;
  int m = 0; //????????£????????§?????????????????£?????????????????????ID
  for (int i = 0; i < r; i++) {
    int d, t, x;
    cin >> d >> t >> x;
    score[--d] += x;
    if (x >= 0) {
      if ((score[d] > score[m]) || ((score[d] == score[m]) && d < m)) {
        ans[m] += t - last;
        last = t;
        m = d;
      }
    } else if (m == d) {
      int tmp = m;
      for (int j = 0; j < n; j++)
        if (score[j] > score[tmp] || ((score[j] == score[tmp]) && j < tmp))
          tmp = j;
      if (m != tmp) {
        ans[m] += t - last;
        last = t;
        m = tmp;
      }
    }
  }
  ans[m] += l - last;
  int res1 = 0, res2 = 0;
  for (int i = 0; i < n; i++) {
    if (res1 < ans[i]) {
      res1 = ans[i];
      res2 = i;
    }
  }
  res2++;
  cout << res2 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <utility>
using namespace std;
int main() {
  int n, r, l, ans[100000] = {}, score[100000] = {};
  cin >> n >> r >> l;
  int last = 0;
  int m = 0; //????????£????????§?????????????????£?????????????????????ID
  for (int i = 0; i < r; i++) {
    int d, t, x;
    cin >> d >> t >> x;
    score[--d] += x;
    if (x >= 0 && m != d) {
      if (score[d] > score[m] || ((score[d] == score[m]) && d < m)) {
        ans[m] += t - last;
        last = t;
        m = d;
      }
    } else if (m == d) {
      int tmp = m;
      for (int j = 0; j < n; j++)
        if (score[j] > score[m] || ((score[j] == score[m]) && j < m))
          tmp = j;
      if (m != tmp) {
        ans[m] += t - last;
        last = t;
        m = tmp;
      }
    }
  }
  ans[m] += l - last;
  int res1 = 0, res2 = 0;
  for (int i = 0; i < n; i++) {
    if (res1 < ans[i]) {
      res1 = ans[i];
      res2 = i;
    }
  }
  res2++;
  cout << res2 << endl;
  return 0;
}
Problem p00277 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
using namespace std;

struct Point {
  int x, y;
  bool operator<(const Point &p) const {
    if (y != p.y)
      return y < p.y;
    return x > p.x;
  }
};
ostream &operator<<(ostream &os, const Point &p) { os << p.x << " " << p.y; }

int score[100001];
int t[1000000];
int time_[1000000];
int main() {
  priority_queue<Point> Q;
  priority_queue<Point> T;
  int N, R, L;
  int d, x;
  int a = 1, b = 0;

  cin >> N >> R >> L;

  for (int i = 0; i < R; i++) {
    cin >> d >> t[i] >> x;
    score[d] += x;
    Q.push((Point){d, score[d]});
    while (!Q.empty() && score[Q.top().x] != Q.top().y) {
      Q.pop();
    }
    time_[a] += t[i] - b;
    T.push((Point){a, time_[a]});
    while (Q.empty())
      ;
    a = Q.top().x;
    b = t[i];
    time_[a] += t[i] - b;
  }

  while (!Q.empty() && score[Q.top().x] != Q.top().y) {
    Q.pop();
  }
  time_[a] += L - b;
  if (!Q.empty())
    a = Q.top().x;
  else
    a = 1;
  b = L;
  T.push((Point){a, time_[a]});

  cout << T.top().x << endl;

  while (!T.empty()) {
    // cout <<T.top()<<endl;
    T.pop();
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;

struct Point {
  int x, y;
  bool operator<(const Point &p) const {
    if (y != p.y)
      return y < p.y;
    return x > p.x;
  }
};
ostream &operator<<(ostream &os, const Point &p) { os << p.x /* <<" "<< p.y*/; }

int score[100001];
int t[1000000];
int time_[1000000];
int main() {
  priority_queue<Point> Q;
  priority_queue<Point> T;
  int N, R, L;
  int d, x;
  int a = 1, b = 0;

  cin >> N >> R >> L;

  for (int i = 0; i < R; i++) {
    cin >> d >> t[i] >> x;
    score[d] += x;
    Q.push((Point){d, score[d]});

    while (!Q.empty() && score[Q.top().x] != Q.top().y) {
      Q.pop();
    }
    time_[a] += t[i] - b;
    T.push((Point){a, time_[a]});
    a = Q.top().x;
    b = t[i];
  }

  while (!Q.empty() && score[Q.top().x] != Q.top().y) {
    Q.pop();
  }
  time_[a] += L - b;
  if (!Q.empty())
    a = Q.top().x;
  else
    a = 1;
  b = L;
  T.push((Point){a, time_[a]});

  cout << T.top() << endl;
}
Problem p00277 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int max(long long int *p, int N) {
  int top = 1;
  for (int i = 2; i <= N; i++) {
    if (p[top] < p[i])
      top = i;
  }
  return top;
}
int main() {
  int N, R, L, d, t, x, old = 1, oldt = 0;
  cin >> N >> R >> L;
  long long int pt[N + 1], ptime[N + 1];
  for (int i = 0; i <= N; i++)
    pt[i] = ptime[i] = 0;
  for (int i = 0; i < R; i++) {
    cin >> d >> t >> x;
    ptime[old] += t - oldt;
    oldt = t;
    pt[d] += x;
    if (x >= 0) {
      if (pt[old] < pt[d])
        old = d;
      if (pt[old] == pt[d] && old > d)
        old = d;
    } else {
      if (old == d)
        old = max(pt, N);
    }
  }
  ptime[old] += L - oldt;
  int ans = 1;
  for (int i = 2; i <= N; i++) {
    if (ptime[ans] < ptime[i])
      ans = i;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int top_id(int *x, int N) {
  int top = x[1];
  int id = 1;
  for (int i = 2; i <= N; i++) {
    if (top < x[i]) {
      top = x[i];
      id = i;
    }
  }
  return id;
}
int main() {
  int N, R, L;
  cin >> N >> R >> L;
  int top = 1;
  int d, t, x;
  int oldt = 0;
  int tim[N + 1];
  int total[N + 1];
  oldt = 0;
  for (int i = 0; i <= N; i++) {
    tim[i] = 0;
    total[i] = 0;
  }
  for (int i = 0; i < R; i++) {
    cin >> d >> t >> x;
    tim[top] += (t - oldt);
    oldt = t;
    total[d] += x;
    top = top_id(total, N);
  }
  tim[top] += (L - oldt);
  int top_t = 0;
  int ans;
  for (int i = 1; i <= N; i++) {
    if (top_t < tim[i]) {
      top_t = tim[i];
      ans = i;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00277 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int max(long long int *p, int N) {
  int top = 1;
  for (int i = 2; i <= N; i++) {
    if (p[top] < p[i])
      top = i;
  }
  return top;
}
int main() {
  int N, R, L, d, t, x, old = 1, oldt = 0;
  cin >> N >> R >> L;
  long long int pt[N + 1], ptime[N + 1];
  for (int i = 0; i <= N; i++)
    pt[i] = ptime[i] = 0;
  for (int i = 0; i < R; i++) {
    cin >> d >> t >> x;
    ptime[old] += t - oldt;
    oldt = t;
    pt[d] += x;
    if (x >= 0) {
      if (pt[old] < pt[d])
        old = d;
      if (pt[old] == pt[d] && old > d)
        old = d;
    } else {
      if (old == d)
        old = max(pt, N);
    }
  }
  ptime[old] += L - oldt;
  int ans = 1;
  for (int i = 2; i <= N; i++) {
    if (ptime[ans] < ptime[i])
      ans = i;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int l, m, n;
  cin >> l >> m >> n;
  int b[l + 1], c[l + 1], top = 1, ans1 = 1;

  for (int i = 1; i <= l; i++)
    b[i] = c[i] = 0;

  int aa[m], bb[m], cc[m];

  for (int i = 1; i <= m; i++)
    cin >> aa[i] >> bb[i] >> cc[i];

  for (int i = 1; i <= m; i++) {

    for (int j = 1; j <= l; j++)
      if (aa[i] == j)
        c[j] += cc[i];

    if (i == 1)
      b[top] += bb[i];

    if (cc[i] >= 0) {
      if (c[top] < c[aa[i]])
        top = aa[i];
      else if (c[top] == c[aa[i]] && aa[i] <= top)
        top = aa[i];
    }

    else if (cc[i] < 0 && aa[i] == top) {
      top = 1;
      for (int j = 2; j <= l; j++)
        if (c[top] < c[j])
          top = j;
    }

    if (i < m)
      b[top] += bb[i + 1] - bb[i];
    else
      b[top] += n - bb[i];
  }

  for (int i = 2; i <= l; i++)
    if (b[ans1] < b[i])
      ans1 = i;

  cout << ans1 << endl;

  return 0;
}
Problem p00277 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define INF (1e9)
typedef pair<int, int> pii;
const int MAX_N = (1 << 18);

struct RMQ {
  int n;
  pii dat[MAX_N];
  void init(int n_) {
    n = 1;
    while (n < n_) {
      n *= 2;
    }
    for (int i = 0; i < 2 * n - 1; i++) {
      dat[i] = pii(-INF, -INF);
    }

    for (int i = 0; i < n_; i++) {
      update(i, 0);
    }
  }

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

  pii query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return pii(-INF, -INF);
    if (a <= l && r <= b) {
      return dat[k];
    } else {
      pii vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      pii vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return max(vl, vr);
    }
  }
};

int main() {
  int N, R, L;
  cin >> N >> R >> L;
  int d, t, x, pt = 0;
  int point[MAX_N] = {};
  RMQ r;
  r.init(N);
  for (int i = 0; i < R; i++) {
    cin >> d >> t >> x;
    d--;
    point[-r.query(0, N, 0, 0, r.n).second] += t - pt;
    r.update(d, x + r.dat[d + r.n - 1].first);
    pt = t;
  }
  point[-r.query(0, N, 0, 0, r.n).second] += L - pt;
  int mx = 0, num = -1;
  for (int i = 0; i < N; i++) {
    if (mx < point[i]) {
      mx = point[i];
      num = i + 1;
    }
  }
  cout << num << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define INF (1e9)
typedef pair<int, int> pii;
const int MAX_N = (1 << 17);

struct RMQ {
  int n;
  pii dat[MAX_N];
  void init(int n_) {
    n = 1;
    while (n < n_) {
      n *= 2;
    }
    for (int i = 0; i < 2 * n - 1; i++) {
      dat[i] = pii(-INF, -INF);
    }

    for (int i = 0; i < n_; i++) {
      update(i, 0);
    }
  }

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

  pii query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return pii(-INF, -INF);
    if (a <= l && r <= b) {
      return dat[k];
    } else {
      pii vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      pii vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return max(vl, vr);
    }
  }
};

int main() {
  int N, R, L;
  cin >> N >> R >> L;
  int d, t, x, pt = 0;
  int point[MAX_N] = {};
  RMQ r;
  r.init(N);
  for (int i = 0; i < R; i++) {
    cin >> d >> t >> x;
    d--;
    point[-r.query(0, N, 0, 0, r.n).second] += t - pt;
    r.update(d, x + r.dat[d + r.n - 1].first);
    pt = t;
  }
  point[-r.query(0, N, 0, 0, r.n).second] += L - pt;
  int mx = 0, num = -1;
  for (int i = 0; i < N; i++) {
    if (mx < point[i]) {
      mx = point[i];
      num = i + 1;
    }
  }
  cout << num << endl;
  return 0;
}
Problem p00277 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define MIN -11111111
#define MAX 11111111
typedef pair<int, int> P;
int n;
P dat[8 * 100000 - 1];
void update(int k, int x);
void init(int n_);
int main() {
  int N, R, L;
  P tim[4 * 100012];
  cin >> N >> R >> L;

  init(N);
  for (int i = 0; i < n; i++) {
    dat[i + (n - 1)] = P(0, i + 1);
    tim[i] = P(0, -i - 1);
  }
  dat[0] = P(MIN, 1);
  int d, t, x, bt = 0;
  for (int i = 0; i < R; i++) {
    cin >> d >> t >> x;
    tim[dat[0].second].first += t - bt;
    update(d, x);
    bt = t;
  }
  tim[dat[0].second].first += L - bt;
  sort(tim, tim + n, greater<P>());
  cout << -tim[0].second - 1 << endl;
}
void init(int n_) {
  n = 1;
  while (n < n_) {
    n *= 2;
  }
  for (int i = 0; i < 2 * n - 1; i++) {
    dat[i] = P(MIN, MAX);
  }
}
void update(int k, int a) {
  k += n - 2;
  dat[k] = P(dat[k].first + a, dat[k].second);
  while (k > 0) {
    k = (k - 1) / 2;
    if (dat[k * 2 + 1].first == dat[k * 2 + 2].first) {
      if (dat[k * 2 + 1].second < dat[k * 2 + 2].second) {
        dat[k] = dat[k * 2 + 1];
      } else {
        dat[k] = dat[k * 2 + 2];
      }
    } else {
      if (dat[k * 2 + 1].first > dat[k * 2 + 2].first) {
        dat[k] = dat[k * 2 + 1];
      } else {
        dat[k] = dat[k * 2 + 2];
      }
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MIN -111111
#define MAX 111111
typedef pair<int, int> P;
int n;
P dat[4 * 100000 - 1];
void update(int k, int x);
void init(int n_);
int main() {
  int N, R, L;
  P tim[100012];
  cin >> N >> R >> L;

  init(N);
  for (int i = 0; i < n; i++) {
    dat[i + (n - 1)] = P(0, i + 1);
    tim[i] = P(0, -i - 1);
  }

  int d, t, x, top = 0, bt = 0;
  for (int i = 0; i < R; i++) {
    cin >> d >> t >> x;
    tim[top].first += t - bt;
    update(d, x);
    top = dat[0].second;
    bt = t;
  }
  tim[top].first += L - bt;
  sort(tim, tim + n, greater<P>());
  cout << -tim[0].second - 1 << endl;
}
void init(int n_) {
  n = 1;
  while (n < n_) {
    n *= 2;
  }
  for (int i = 0; i < 2 * n - 1; i++) {
    dat[i] = P(MIN, MAX);
  }
}
void update(int k, int a) {
  k += n - 1;
  dat[k] = P(dat[k].first + a, k - (n - 1));
  while (k > 0) {
    k = (k - 1) / 2;
    if (dat[k * 2 + 1].first == dat[k * 2 + 2].first) {
      if (dat[k * 2 + 1].second < dat[k * 2 + 2].second) {
        dat[k] = dat[k * 2 + 1];
      } else {
        dat[k] = dat[k * 2 + 2];
      }
    } else {
      if (dat[k * 2 + 1].first > dat[k * 2 + 2].first) {
        dat[k] = dat[k * 2 + 1];
      } else {
        dat[k] = dat[k * 2 + 2];
      }
    }
  }
}
Problem p00277 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstdlib>
using namespace std;
int dat[1000000];
int max_idx[2 * 1000000 - 1];
int size;
void init(int _size) {
  size = 1;
  while (_size > size)
    size *= 2;
  for (int i = 0; i < size; i++) {
    if (i < _size)
      dat[i] = 0;
    else
      dat[i] = -(1 << 30);
  }
  for (int i = size * 2 - 2; i >= 0; i--) {
    if (i >= size - 1) {
      max_idx[i] = i - (size - 1);
    } else {
      max_idx[i] = max_idx[i * 2 + 1];
    }
  }
}
void update(int k, int a) {
  dat[k] += a;
  k += size - 1;
  while (k > 0) {
    k = (k - 1) / 2;
    if (dat[max_idx[k * 2 + 1]] >= dat[max_idx[k * 2 + 2]]) {
      max_idx[k] = max_idx[k * 2 + 1];
    } else {
      max_idx[k] = max_idx[k * 2 + 2];
    }
  }
}
int Time[1000000];
int main() {
  int N, R, L;
  scanf("%d %d %d", &N, &R, &L);
  init(N);
  int cur = 0;
  for (int i = 0; i < R; i++) {
    int d, t, x;
    scanf("%d %d %d", &d, &t, &x);
    d--;
    Time[max_idx[0]] += t - cur;
    update(d, x);
    cur = t;
  }
  Time[max_idx[0]] += L - cur;
  int max_time = 0, max_id = 0;
  for (int i = 0; i < N; i++) {
    if (Time[i] > max_time) {
      max_time = Time[i];
      max_id = i;
    }
  }
  printf("%d\\n", max_id + 1);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
using namespace std;
struct SegTree {
  int *dat;
  int *max_idx;
  int size;
  SegTree(int _size) {
    size = 1;
    while (_size > size)
      size *= 2;
    dat = (int *)malloc(sizeof(int) * size);
    max_idx = (int *)malloc(sizeof(int) * (size * 2 - 1));
    for (int i = 0; i < size; i++) {
      if (i < _size)
        dat[i] = 0;
      else
        dat[i] = -(1 << 30);
    }
    for (int i = size * 2 - 1; i >= 0; i--) {
      if (i >= size) {
        max_idx[i] = i - size;
      } else {
        max_idx[i] = max_idx[i * 2 + 1];
      }
    }
  }
  void update(int k, int a) {
    dat[k] += a;
    k += size - 1;
    while (k > 0) {
      k = (k - 1) / 2;
      if (dat[max_idx[k * 2 + 1]] >= dat[max_idx[k * 2 + 2]]) {
        max_idx[k] = max_idx[k * 2 + 1];
      } else {
        max_idx[k] = max_idx[k * 2 + 2];
      }
    }
  }
};
int Time[100000];
int main() {
  int N, R, L;
  scanf("%d %d %d", &N, &R, &L);
  SegTree S(N);
  int cur = 0;
  for (int i = 0; i < R; i++) {
    int d, t, x;
    scanf("%d %d %d", &d, &t, &x);
    d--;
    Time[S.max_idx[0]] += t - cur;
    S.update(d, x);
    cur = t;
  }
  Time[S.max_idx[0]] += L - cur;
  int max_time = 0, max_id = 0;
  for (int i = 0; i < N; i++) {
    if (Time[i] > max_time) {
      max_time = Time[i];
      max_id = i;
    }
  }
  printf("%d\\n", max_id + 1);
}