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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10
#define MAX_N 200000

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, P> pii;
typedef vector<int> vi;
typedef queue<int> qi;

struct segtree {
  int N;
  P dat[2 * MAX_N - 1];
  segtree() {}
  segtree(int n) {
    N = 1;
    while (n > N)
      N *= 2;

    rep(i, 2 * N - 1) dat[i] = MP(-10000000, -10000000);
    for (int i = N - 1; i < 2 * N - 1; i++)
      dat[i] = MP(0, -1 * (i - N + 1));
  }

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

  P query(int a, int b) { return query(a, b, 0, 0, N); }
  P query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return P(-10000000, -10000000);

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

int res[MAX_N];

int main() {
  int N, R, L;
  scanf("%d%d%d", &N, &R, &L);
  segtree seg(N);
  rep(i, N) seg.update(i, 0);

  int time = 0;
  rep(i, R) {
    int d, t, x;
    scanf("%d%d%d", &d, &t, &x);
    d--;
    P s = seg.query(0, N);
    res[-s.second] += t - time;
    seg.update(d, x);
    time = t;
  }
  P s = seg.query(0, N);
  res[-s.second] += L - time;

  int ans = 0;
  rep(i, N) if (res[ans] < res[i]) ans = i;

  printf("%d\\n", ans + 1);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10
#define MAX_N 20000

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, P> pii;
typedef vector<int> vi;
typedef queue<int> qi;

struct segtree {
  int N;
  P dat[MAX_N * 2 - 1];
  segtree() {}
  segtree(int n) {
    N = 1;
    while (n > N)
      N *= 2;

    rep(i, 2 * N - 1) dat[i] = MP(-10000000, -10000000);
    for (int i = N - 1; i < 2 * N - 1; i++)
      dat[i] = MP(0, -1 * (i - N + 1));
  }

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

  P query(int a, int b) { return query(a, b, 0, 0, N); }
  P query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return P(-10000000, -10000000);

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

int main() {
  int N, R, L;
  int res[MAX_N];
  scanf("%d%d%d", &N, &R, &L);
  segtree seg(N);
  rep(i, N) seg.update(i, 0);
  int time = 0;
  rep(i, R) {
    int d, t, x;
    scanf("%d%d%d", &d, &t, &x);
    d--;
    P s = seg.query(0, N);
    res[-s.second] += t - time;
    seg.update(d, x);
    time = t;
  }

  P s = seg.query(0, N);
  res[-s.second] += L - time;

  int ans = 0;
  rep(i, N) if (res[ans] < res[i]) ans = i;

  printf("%d\\n", ans + 1);
  return 0;
}