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

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
// #include <numeric> //iota
// #include <sstream>
// #include <cstring>
// #include <tuple>
// #include <random>
// #include <string>
// #include <unordered_set>
// #include <bitset>
// #include <unordered_map>
// #include <stack>
// #include <cmath>
// #include <deque>
// #include <queue>
// #include <list>
#include <array>

using namespace std;
#define all(x) (x).begin(), (x).end()
#define vec vector
#define pb push_back
#define pp pop_back
#define fi first
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define se second
#define un_map unordered_map
#define rep(i, a, n) for (int i = a; i < n; ++i)
#define inc_rep(i, a, n, inc) for (int i = a; i < n; i += inc)
#define couts(x) cout << x << "\\n"
#define watch(x) cout << #x << " : " << x << endl;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLII;
template <typename T> void print(vector<T> &v) {
  rep(i, 0, (int)v.size()) cout << v[i] << " ";
  cout << endl;
}
int nxt() {
  int x;
  cin >> x;
  return x;
}
ll nxtl() {
  ll x;
  cin >> x;
  return x;
}
string nxts() {
  string x;
  cin >> x;
  return x;
}
double nxtd() {
  double x;
  cin >> x;
  return x;
}

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}

#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

ll power_mod(ll a, ll b, ll mo) {
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = (ans % mo * a % mo) % mo;
    }
    a = (a % mo * a % mo) % mo;
    b /= 2;
  }
  return ans;
}
ll POW(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = ans * a;
    }
    a = a * a;
    b /= 2;
  }
  return ans;
}

// string int_to_string(int x) {
// stringstream ss;
// ss << x;
// string ni = ss.str();
// return ni;
//}

// int string_to_int(string x) {
// int n;
// stringstream s(x);
// s >> n;
// return n;
//}

// char int_to_char(int a) {
// return a + '0';
//}

// void INVERSE() {
// fact[0] = 1;
// for (ll i = 1; i <= z; ++i) {
// fact[i] = (fact[i - 1] * i) % mod;
//}
// inv[0] = 1;
// inv[1] = 1;
// for (ll i = 2; i <= z; ++i) {
// inv[i] = (mod - (mod / i) * inv[mod % i] % mod) % mod;
//}
// mul[0] = 1;
// for (int i = 1; i <= z; ++i) {
// mul[i] = (mul[i - 1] * inv[i]) % mod;
//}
//}

// read questions carefully , write clean code and check your logic again on WA
// !!!

// problems are not difficult just make sure you understand the qusetion and
// just think smartly (make sure to use pen and paper when needed)

// void update(int index, int val) {
// for (; index <= n; index += index & -index) {
// fen[index] = (val + fen[index]) % mod;
//}
//}
// void query(int index) {
// long long sum = 0;
// for (; index > 0; index -= index & -index) {
// sum = (sum + fen[index]) % mod;
//}
// return sum;
//}
int const p = 800000;
int dp[p];
int v[p];
int segment_tree[p];
int fen[p];
int segme[p];
int n, k;
void SetMax(int pos, int x) {
  for (int i = pos + p; i; i >>= 1)
    fen[i] = max(fen[i], x);
}
int GetMax(int L, int R) {
  int res = 0;
  for (L += p, R += p; L < R; L >>= 1, R >>= 1) {
    if (L & 1) {
      res = max(res, fen[L]);
      L++;
    }
    if (R & 1) {
      R--;
      res = max(res, fen[R]);
    }
  }
  return res;
}

// void build_tree(int l, int r, int index) {
// if (l == r) {
// segment_tree[index] = v[l];
// return ;
//}
// int half = (l + r) / 2;
// build_tree(l,half,index*2);
// build_tree(half + 1,r,index*2 + 1);
// segment_tree[index] = min(segment_tree[index * 2], segment_tree[index * 2 +
// 1]);
//}
// void update(int l, int r, int index, int check_index, int val) {
// if (l == r) {
// segment_tree[index] = val;
// return ;
//}
// int mid = (l + r) / 2;
// if (check_index <= mid) {
// update(l,mid,index * 2,check_index, val);
//}else {
// update(mid + 1,r,index * 2 + 1,check_index, val);
//}
// segment_tree[index] = max(segment_tree[index * 2],segment_tree[index * 2 +
// 1]);

//}
// int query(int l, int r, int l1 , int r1, int index) {
// if (l1 <= l && r1 >= r) {
// return segment_tree[index];
//}
// if (r1 < l || l1 > r) {
// return 0;
//}
// int mid = (l + r) / 2;
// return max(query(l,mid,l1,r1,index*2),query(mid + 1,r,l1,r1,index*2 + 1));
//}

int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> v[i];
    v[i]++;
  }
  for (int i = n; i > 0; --i) {
    int r = v[i] + k;
    int l = (v[i] - k >= 1) ? v[i] - k : 1;
    int val = GetMax(l, r + 1);
    dp[v[i]] = val + 1;
    SetMax(v[i], dp[v[i]]);
    // cout << l << " " <<  r << " " << dp[v[i]] << " " << v[i] << " " << val <<
    // " " << dp[val] << endl;
  }

  int maxm = 0;
  for (int i = 0; i <= 300000 + 1; ++i)
    maxm = max(maxm, dp[i]);
  cout << maxm;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
// #include <numeric> //iota
// #include <sstream>
// #include <cstring>
// #include <tuple>
// #include <random>
// #include <string>
// #include <unordered_set>
// #include <bitset>
// #include <unordered_map>
// #include <stack>
// #include <cmath>
// #include <deque>
// #include <queue>
// #include <list>
#include <array>

using namespace std;
#define all(x) (x).begin(), (x).end()
#define vec vector
#define pb push_back
#define pp pop_back
#define fi first
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define se second
#define un_map unordered_map
#define rep(i, a, n) for (int i = a; i < n; ++i)
#define inc_rep(i, a, n, inc) for (int i = a; i < n; i += inc)
#define couts(x) cout << x << "\\n"
#define watch(x) cout << #x << " : " << x << endl;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLII;
template <typename T> void print(vector<T> &v) {
  rep(i, 0, (int)v.size()) cout << v[i] << " ";
  cout << endl;
}
int nxt() {
  int x;
  cin >> x;
  return x;
}
ll nxtl() {
  ll x;
  cin >> x;
  return x;
}
string nxts() {
  string x;
  cin >> x;
  return x;
}
double nxtd() {
  double x;
  cin >> x;
  return x;
}

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}

#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

ll power_mod(ll a, ll b, ll mo) {
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = (ans % mo * a % mo) % mo;
    }
    a = (a % mo * a % mo) % mo;
    b /= 2;
  }
  return ans;
}
ll POW(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = ans * a;
    }
    a = a * a;
    b /= 2;
  }
  return ans;
}

// string int_to_string(int x) {
// stringstream ss;
// ss << x;
// string ni = ss.str();
// return ni;
//}

// int string_to_int(string x) {
// int n;
// stringstream s(x);
// s >> n;
// return n;
//}

// char int_to_char(int a) {
// return a + '0';
//}

// void INVERSE() {
// fact[0] = 1;
// for (ll i = 1; i <= z; ++i) {
// fact[i] = (fact[i - 1] * i) % mod;
//}
// inv[0] = 1;
// inv[1] = 1;
// for (ll i = 2; i <= z; ++i) {
// inv[i] = (mod - (mod / i) * inv[mod % i] % mod) % mod;
//}
// mul[0] = 1;
// for (int i = 1; i <= z; ++i) {
// mul[i] = (mul[i - 1] * inv[i]) % mod;
//}
//}

// read questions carefully , write clean code and check your logic again on WA
// !!!

// problems are not difficult just make sure you understand the qusetion and
// just think smartly (make sure to use pen and paper when needed)

// void update(int index, int val) {
// for (; index <= n; index += index & -index) {
// fen[index] = (val + fen[index]) % mod;
//}
//}
// void query(int index) {
// long long sum = 0;
// for (; index > 0; index -= index & -index) {
// sum = (sum + fen[index]) % mod;
//}
// return sum;
//}
int p = 300000 + 1;
int dp[300000 + 2];
int v[300000 + 2];
int segment_tree[300000 + 2];
int n, k;

void build_tree(int l, int r, int index) {
  if (l == r) {
    segment_tree[index] = v[l];
    return;
  }
  int half = (l + r) / 2;
  build_tree(l, half, index * 2);
  build_tree(half + 1, r, index * 2 + 1);
  segment_tree[index] =
      min(segment_tree[index * 2], segment_tree[index * 2 + 1]);
}
void update(int l, int r, int index, int check_index, int val) {
  if (l == r) {
    segment_tree[index] = val;
    return;
  }
  int mid = (l + r) / 2;
  if (check_index <= mid) {
    update(l, mid, index * 2, check_index, val);
  } else {
    update(mid + 1, r, index * 2 + 1, check_index, val);
  }
  segment_tree[index] =
      max(segment_tree[index * 2], segment_tree[index * 2 + 1]);
}
int query(int l, int r, int l1, int r1, int index) {
  if (l1 <= l && r1 >= r) {
    return segment_tree[index];
  }
  if (r1 < l || l1 > r) {
    return 0;
  }
  int mid = (l + r) / 2;
  return max(query(l, mid, l1, r1, index * 2),
             query(mid + 1, r, l1, r1, index * 2 + 1));
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; ++i)
    cin >> v[i];
  for (int i = n - 1; i >= 0; --i) {
    int r = (v[i] + k >= p) ? v[i] + k : p;
    int l = (v[i] - k >= 0) ? v[i] - k : 0;
    int val = query(1, n, l, r, 1);
    dp[v[i]] = val + 1;
    update(1, n, 1, v[i], dp[v[i]]);
  }

  int maxm = 0;
  for (int i = 0; i <= 300000; ++i)
    maxm = max(maxm, dp[i]);
  cout << maxm;
}