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

#include <algorithm>
#include <iostream>
#include <vector>
#define INF -(1 << 29)
#define F first
#define S second
#define T P
using namespace std;
typedef pair<int, int> P;
const int MAX_N = 1000010;

bool cmp(const P &a, const P &b) {
  if (a.first == b.first)
    return a.second > b.second;
  return a.first < b.first;
}

int n;
T dat[3 * 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] = P(INF, INF);
}

// k番めの値(0-indexed)をaに変更
void update(int k, T a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2], cmp);
  }
}

// [a,b)の最大値を求める
T query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return P(INF, INF);
  else if (a <= l && r <= b)
    return dat[k];

  T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
  T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
  return max(vl, vr, cmp);
}

// nの範囲でミスしないように極力こっちを利用
T _query(int a, int b) { return query(a, b, 0, 0, n); }

int main() {
  int n, q;
  cin >> n >> q;

  init(n);
  P total[n + 1];
  for (int i = 0; i <= n; i++)
    total[i] = P(INF, INF);
  vector<P> vec;
  for (int i = 0; i < q; i++) {
    int a, v;
    cin >> a >> v;
    if (total[a] == P(INF, INF)) {
      vec.push_back(P(a, v));
      int index = vec.size() - 1;
      total[a] = P(v, index);
      update(index, P(v, a));
    } else {
      total[a].first += v;
      update(total[a].second, P(total[a].first, a));
    }

    P p = _query(0, vec.size());
    cout << p.second << " " << p.first << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define INF -(1 << 29)
#define F first
#define S second
#define T P
using namespace std;
typedef pair<int, int> P;
const int MAX_N = 1000010;

bool cmp(const P &a, const P &b) {
  if (a.first == b.first)
    return a.second > b.second;
  return a.first < b.first;
}

int n;
T 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] = P(INF, INF);
}

// k番めの値(0-indexed)をaに変更
void update(int k, T a) {
  k += n - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2], cmp);
  }
}

// [a,b)の最大値を求める
T query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return P(INF, INF);
  else if (a <= l && r <= b)
    return dat[k];

  T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
  T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
  return max(vl, vr, cmp);
}

// nの範囲でミスしないように極力こっちを利用
T _query(int a, int b) { return query(a, b, 0, 0, n); }

int main() {
  int n, q;
  cin >> n >> q;

  init(n);
  P total[n + 1];
  for (int i = 0; i <= n; i++)
    total[i] = P(INF, INF);
  vector<P> vec;
  for (int i = 0; i < q; i++) {
    int a, v;
    cin >> a >> v;
    if (total[a] == P(INF, INF)) {
      vec.push_back(P(a, v));
      int index = vec.size() - 1;
      total[a] = P(v, index);
      update(index, P(v, a));
    } else {
      total[a].first += v;
      update(total[a].second, P(total[a].first, a));
    }

    P p = _query(0, vec.size());
    cout << p.second << " " << p.first << endl;
  }

  return 0;
}