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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

#define MAXN 660000

struct Seg {
  vector<ll> sum;
  vector<ll> mx;
  Seg() {
    sum.resize(1 << 23, 0);
    mx.resize(1 << 23, 0);
  }

  void add_val(ll l, ll r, ll val, ll bottom = 0, ll top = 1 << 22,
               ll pos = 1) {
    if (l <= bottom && top <= r) {
      sum[pos] += val;
      return;
    }
    if (r <= bottom || l >= top)
      return;

    ll mid = (bottom + top) / 2;
    add_val(l, r, val, bottom, mid, pos * 2);
    add_val(l, r, val, mid, top, pos * 2 + 1);
    mx[pos] =
        max<ll>(mx[pos * 2] + sum[pos * 2], mx[pos * 2 + 1] + sum[pos * 2 + 1]);
  }

  ll max_val(ll l, ll r, ll bottom = 0, ll top = 1 << 22, ll pos = 1) {
    if (l <= bottom && top <= r)
      return sum[pos] + mx[pos];
    if (r <= bottom || l >= top)
      return -INF;

    ll mid = (bottom + top) / 2;
    return max<ll>(max_val(l, r, bottom, mid, pos * 2),
                   max_val(l, r, mid, top, pos * 2 + 1)) +
           sum[pos];
  }
};

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  Seg seg;
  rep(i, n) {
    ll l = max(0, a[i] - k);
    ll r = a[i] + k + 1;
    ll mx = seg.max_val(l, r);
    ll cv = seg.max_val(a[i], a[i] + 1);
    seg.add_val(a[i], a[i] + 1, mx + 1 - cv);
  }

  cout << seg.max_val(1, MAXN);

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

#define MAXN 660000

struct Seg {
  vector<ll> sum;
  vector<ll> mx;
  Seg() {
    sum.resize(1 << 29, 0);
    mx.resize(1 << 29, 0);
  }

  void add_val(ll l, ll r, ll val, ll bottom = 0, ll top = 1 << 28,
               ll pos = 1) {
    if (l <= bottom && top <= r) {
      sum[pos] += val;
      return;
    }
    if (r <= bottom || l >= top)
      return;

    ll mid = (bottom + top) / 2;
    add_val(l, r, val, bottom, mid, pos * 2);
    add_val(l, r, val, mid, top, pos * 2 + 1);
    mx[pos] =
        max<ll>(mx[pos * 2] + sum[pos * 2], mx[pos * 2 + 1] + sum[pos * 2 + 1]);
  }

  ll max_val(ll l, ll r, ll bottom = 0, ll top = 1 << 28, ll pos = 1) {
    if (l <= bottom && top <= r)
      return sum[pos] + mx[pos];
    if (r <= bottom || l >= top)
      return -INF;

    ll mid = (bottom + top) / 2;
    return max<ll>(max_val(l, r, bottom, mid, pos * 2),
                   max_val(l, r, mid, top, pos * 2 + 1)) +
           sum[pos];
  }
};

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  Seg seg;
  int result = 0;
  rep(i, n) {
    ll l = max(0, a[i] - k);
    ll r = a[i] + k + 1;
    ll mx = seg.max_val(l, r);
    ll cv = seg.max_val(a[i], a[i] + 1);
    seg.add_val(a[i], a[i] + 1, mx + 1 - cv);
  }

  cout << seg.max_val(1, MAXN);

  return 0;
}