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

#include <algorithm>
#include <iostream>
#include <limits>
#include <queue>
#include <vector>

constexpr int MAX_N = 1000000;
constexpr auto INF = std::numeric_limits<int>::max();

using Pair = std::pair<int, int>;

struct segtree {
  int N;
  std::vector<Pair> dat{2 * MAX_N};
  segtree() {}
  segtree(int n) {
    N = 1;
    while (N < n)
      N *= 2;
    for (int i = 0; i < 2 * N - 1; i++)
      dat[i] = {INF, INF};
  }

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

  Pair query(int a, int b) { return query(a, b, 0, 0, N); }
  Pair query(int a, int b, int k, int l, int r) {
    if (r <= a or b <= l)
      return Pair{INF, INF};
    if (a <= l and r <= b)
      return dat[k];
    auto m = (l + r) / 2;
    return min(query(a, b, k * 2 + 1, l, m), query(a, b, k * 2 + 2, m, r));
  }
};

int n, r, l;
int scores[MAX_N];
int time_[MAX_N];

int main() {
  std::cin >> n >> r >> l;

  segtree st(n);

  for (int i = 0; i < n; ++i) {
    st.update(i, 0);
  }

  std::fill(scores, scores + n, 0);
  std::fill(time_, time_ + n, 0);

  int d, t, x;
  int prevTime = 0;
  for (int i = 0; i < r; ++i) {
    std::cin >> d >> t >> x;

    auto m = st.query(0, n);

    time_[m.second] += t - prevTime;

    --d;

    scores[d] += x;

    st.update(d, scores[d]);

    prevTime = t;
  }

  auto m = st.query(0, n);

  time_[m.second] += l - prevTime;

  /*for(int i = 0; i < n; ++i) {
      std::cout << scores[i] << " ";
  }
  std::cout << std::endl;

  for(int i = 0; i < n; ++i) {
      std::cout << time_[i] << " ";
  }
  std::cout << std::endl;
*/
  std::cout << (std::max_element(time_, time_ + n) - time_ + 1) << std::endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <limits>
#include <queue>
#include <vector>

constexpr int MAX_N = 100000;
constexpr auto INF = std::numeric_limits<int>::max();

using Pair = std::pair<int, int>;

struct segtree {
  int N;
  Pair dat[2 * MAX_N];
  segtree() {}
  segtree(int n) {
    N = 1;
    while (N < n)
      N *= 2;
    for (int i = 0; i < 2 * N - 1; i++)
      dat[i] = {INF, INF};
  }

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

  Pair query(int a, int b) { return query(a, b, 0, 0, N); }
  Pair query(int a, int b, int k, int l, int r) {
    if (r <= a or b <= l)
      return Pair{INF, INF};
    if (a <= l and r <= b)
      return dat[k];
    auto m = (l + r) / 2;
    return min(query(a, b, k * 2 + 1, l, m), query(a, b, k * 2 + 2, m, r));
  }
};

using Pair = std::pair<int, int>;

int n, r, l;
int scores[MAX_N];
int time_[MAX_N];

int main() {
  std::cin >> n >> r >> l;

  segtree st(n);

  for (int i = 0; i < n; ++i) {
    st.update(i, 0);
  }

  std::fill(scores, scores + n, 0);
  std::fill(time_, time_ + n, 0);

  int d, t, x;
  int prevTime = 0;
  for (int i = 0; i < r; ++i) {
    std::cin >> d >> t >> x;

    auto m = st.query(0, n);

    time_[m.second] += t - prevTime;

    --d;

    scores[d] += x;

    st.update(d, scores[d]);

    prevTime = t;
  }

  auto m = st.query(0, n);

  time_[m.second] += l - prevTime;

  std::cout << (std::max_element(time_, time_ + n) - time_ + 1) << std::endl;
}