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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define mp make_pair
#define all(r) r.begin(), r.end()
#define fi first
#define se second
#define println(X) cout << X << endl;
#define DBG(X) cout << #X << " : " << X << endl;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-7;

const int MAX_N = 1 << 20;

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].fi = -INF;
    dat[i].se = -(i - n + 1);
  }
}

void update(int k, int add) {
  k += n - 1;
  dat[k].fi += add;
  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, n - 1);
  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);
  }
}

vector<pii> q[1000010];

int main() {
  int r, l;
  cin >> n >> r >> l;
  init(n);
  rep(i, n) update(i, INF);
  vi v(n, 0);
  int d, t, x, tt = 0;
  pii p;
  rep(i, r) {
    cin >> d >> t >> x;
    q[t].pb(pii(d - 1, x));
    // p = query(0, n, 0, 0, n);
    // cout<<-p.se<<endl;
    //  v[-p.se] = t-tt;
    //  tt = t;
    //  update(d-1, x);
  }
  rep(i, l + 1) {
    rep(j, q[i].size()) { update(q[i][j].fi, q[i][j].se); }
    p = query(0, n, 0, 0, n);
    v[-p.se]++;
    // cout<<-p.se<<endl;
  }
  // p = query(0, n, 0, 0, n);
  // v[-p.se] = l-tt;
  int ans = 0;
  for (int i = 1; i < v.size(); i++) {
    if (v[ans] < v[i])
      ans = i;
  }
  // rep(i, v.size()){
  // 	cout<<v[i]<<endl;
  // }
  cout << ans + 1 << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// #define int long long

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define mp make_pair
#define all(r) r.begin(), r.end()
#define fi first
#define se second
#define println(X) cout << X << endl;
#define DBG(X) cout << #X << " : " << X << endl;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-7;

const int MAX_N = 1 << 20;

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].fi = 0;
    dat[i].se = -(i - n + 2);
  }
}

void update(int k, int add) {
  k += n - 1;
  dat[k].fi += add;
  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(0, 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);
  }
}

signed main() {
  int r, l;
  cin >> n >> r >> l;
  init(n);
  vi v(n + 100, 0);
  int d, t, x, tt = 0;
  pii p;
  update(1, 0);
  rep(i, r) {
    cin >> d >> t >> x;
    p = query(0, n, 0, 0, n);
    // if(p.se >= 0) continue;
    if (p.se == 0)
      p.se = -1;
    else if (p.se > 0)
      continue;
    // cout<<p.se<<endl;
    v[-p.se] += t - tt;
    update(d, x);
    tt = t;
  }
  p = query(0, n, 0, 0, n);
  v[-p.se] += l - tt;

  // int ans = 0;
  // for(int i = 1; i < v.size(); i++){
  // 	if(v[ans] < v[i]) ans = i;
  // 	cout<<v[i]<<endl;
  // }
  // cout<<ans<<endl;
  cout << (int)(max_element(all(v)) - v.begin()) - 1 << endl;
}