Problem p02537 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 1000000007LL;
#define speed                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
//_______________________________________________________________________________
const int N = 300005;
int seg[2 * N];
int a[N];
int dp[N];
void build(int ind, int rl, int rr) {
  if (rl == rr) {
    seg[ind] = a[rr];
    return;
  }
  int mid = (rl + rr) / 2;
  build(2 * ind, rl, mid);
  build((2 * ind) + 1, mid + 1, rr);
  seg[ind] = max(seg[2 * ind], seg[(2 * ind) + 1]);
}
void update(int ind, int idx, int val, int rl, int rr) {
  if (rl == rr) {
    seg[ind] = val;
    // a[idx] = val;
    return;
  }
  int mid = (rl + rr) / 2;
  if (idx >= rl && idx <= mid) {
    update(2 * ind, idx, val, rl, mid);
  } else {
    update((2 * ind) + 1, idx, val, mid + 1, rr);
  }
  seg[ind] = max(seg[2 * ind], seg[(2 * ind) + 1]);
}
int query(int ind, int ql, int qr, int rl, int rr) {
  if (ql > rr || rl > qr) {
    return -1;
  }
  if (rr <= qr && rl >= ql) {
    return seg[ind];
  }
  int mid = (rl + rr) / 2;
  return max(query(2 * ind, ql, qr, rl, mid),
             query((2 * ind) + 1, ql, qr, mid + 1, rr));
}
int main() {
  int n, k;
  cin >> n >> k;
  memset(dp, 0, sizeof dp);
  memset(seg, 0, sizeof seg);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = n - 1; i >= 0; i--) {
    int mn = max(a[i] - k, 0);
    int mx = min(a[i] + k, 300002);
    dp[a[i]] = max(dp[a[i]], query(1, mn, mx, 0, 300002) + 1);
    //       cout<<a[i]<<" "<<query(1,mn,mx,0,300002)<<endl;
    update(1, a[i], dp[a[i]], 0, 300002);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, dp[a[i]]);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 1000000007LL;
#define speed                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
//_______________________________________________________________________________
const int N = 300005;
int seg[4 * N];
int a[N];
int dp[N];
void build(int ind, int rl, int rr) {
  if (rl == rr) {
    seg[ind] = a[rr];
    return;
  }
  int mid = (rl + rr) / 2;
  build(2 * ind, rl, mid);
  build((2 * ind) + 1, mid + 1, rr);
  seg[ind] = max(seg[2 * ind], seg[(2 * ind) + 1]);
}
void update(int ind, int idx, int val, int rl, int rr) {
  if (rl == rr) {
    seg[ind] = val;
    // a[idx] = val;
    return;
  }
  int mid = (rl + rr) / 2;
  if (idx >= rl && idx <= mid) {
    update(2 * ind, idx, val, rl, mid);
  } else {
    update((2 * ind) + 1, idx, val, mid + 1, rr);
  }
  seg[ind] = max(seg[2 * ind], seg[(2 * ind) + 1]);
}
int query(int ind, int ql, int qr, int rl, int rr) {
  if (ql > rr || rl > qr) {
    return -1;
  }
  if (rr <= qr && rl >= ql) {
    return seg[ind];
  }
  int mid = (rl + rr) / 2;
  return max(query(2 * ind, ql, qr, rl, mid),
             query((2 * ind) + 1, ql, qr, mid + 1, rr));
}
int main() {
  int n, k;
  cin >> n >> k;
  memset(dp, 0, sizeof dp);
  memset(seg, 0, sizeof seg);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = n - 1; i >= 0; i--) {
    int mn = max(a[i] - k, 0);
    int mx = min(a[i] + k, 300002);
    dp[a[i]] = max(dp[a[i]], query(1, mn, mx, 0, 300002) + 1);
    //       cout<<a[i]<<" "<<query(1,mn,mx,0,300002)<<endl;
    update(1, a[i], dp[a[i]], 0, 300002);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, dp[a[i]]);
  }
  cout << ans << endl;
}