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

#define _CRT_SECUREj_NO_WARNINGS 1
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF ((int)(1 << 25))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, n) REP(i, n)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << " :" << x << endl;
#define EPS 1e-12
template <class T> void debug(T a) {
  for (auto iiiiiiiiii : a)
    cout << iiiiiiiiii << endl;
}
typedef vector<int> vi;
typedef vector<string> vs;
#define int long long
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};

const int MAX_N = 1 << 18;
int n;
pii dat[2 * MAX_N - 1];

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

void update(int k, pii a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(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(INT_MAX, 0);
  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 min(vl, vr);
  }
}

pii calc(int a, int b) //[a,b)
{
  return query(a, b, 0, 0, n);
}
int ans[200000] = {};
signed main() {
  int r, l;
  cin >> n >> r >> l;

  init(n);
  for (int i = 0; i < n; i++)
    update(i, pii(0, i));
  int b = 0;
  while (r--) {
    int d, t, x;
    cin >> d >> t >> x;
    d--;
    ans[calc(0, n).second] += t - b;
    b = t;
    pii p = dat[d + n - 1];
    p.first -= x;
    update(d, p);
  }
  ans[calc(0, n).second] += l - b;
  int p = -1, id = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (ans[i] >= p) {
      p = ans[i];
      id = i + 1;
    }
  }

  cout << id << endl;
}

Error: 0

Fixed submission:

#define _CRT_SECUREj_NO_WARNINGS 1
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define INF ((int)(1 << 25))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, n) REP(i, n)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << " :" << x << endl;
#define EPS 1e-12
template <class T> void debug(T a) {
  for (auto iiiiiiiiii : a)
    cout << iiiiiiiiii << endl;
}
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};

const int MAX_N = 1 << 18;
int n;
pii dat[2 * MAX_N - 1];

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

void update(int k, pii a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(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(INT_MAX, 0);
  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 min(vl, vr);
  }
}

pii calc(int a, int b) //[a,b)
{
  return query(a, b, 0, 0, n);
}

signed main() {
  int r, l;
  cin >> n >> r >> l;
  int ans[100001] = {};
  init(n);
  for (int i = 0; i < n; i++)
    update(i, pii(0, i));
  int b = 0;
  while (r--) {
    int d, t, x;
    cin >> d >> t >> x;
    d--;
    ans[calc(0, n).second] += t - b;
    b = t;
    pii p = dat[d + n - 1];
    p.first -= x;
    update(d, p);
  }
  ans[calc(0, n).second] += l - b;
  int p = -1, id = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (ans[i] >= p) {
      p = ans[i];
      id = i + 1;
    }
  }

  cout << id << endl;
}