Problem p02537 (C++)

Buggy submission (status: Runtime Error):

// Problem : D - Flat Subsequence
// Contest : AtCoder - ACL Beginner Contest
// URL : https://atcoder.jp/contests/abl/tasks/abl_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

// $%U%$
// Mere bas ka nahi hai
// still i'm trying
// apna time aayega
//
//        .--------------.
//        | Try First One|
//        '--------------'
//                |     .--------------.
//                |     |              |
//                V     V              |
//              .--------------.       |
//              |      AC.     |<---.  |
//              '--------------'    |  |
//              (True)|  |(False)   |  |
//           .--------'  |          |  |
//           |           V          |  |
//           |  .--------------.    |  |
//           |  |   Try Again  |----'  |
//           |  '--------------'       |
//           |                         |
//           |  .--------------.       |
//           '->| Try Next One |-------'
//              '--------------'
//
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll power(ll x, ll y, ll m) {
  if (y == 0)
    return 1;
  ll p = power(x, y / 2, m) % m;
  p = (p * p) % m;
  return (y % 2 == 0) ? p : (x * p) % m;
}
ll nCr(ll n, ll r, ll m) {
  if (r > n)
    return 0;
  ll a = 1, b = 1, i;
  for (i = 0; i < r; i++) {
    a = (a * n) % m;
    --n;
  }
  while (r) {
    b = (b * r) % m;
    --r;
  }
  return (a * power(b, m - 2, m)) % m;
}
#define Test                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define endl '\n'
vector<int> tree, v, t;
void update(int l, int r, int index, int cindex) {
  if (l == r) {
    tree[index] = t[l];
    return;
  }
  int mid = (l + r) / 2;
  if (cindex <= mid)
    update(l, mid, 2 * index + 1, cindex);
  else
    update(mid + 1, r, 2 * index + 2, cindex);
  tree[index] = max(tree[index * 2 + 1], tree[index * 2 + 2]);
}

int find(int l, int r, int tl, int tr, int index) {
  if (tl > tr) {
    return 0;
  }
  if (tl == l && tr == r) {
    return tree[index];
  }
  int mid = (l + r) / 2;
  int t1 = find(l, mid, tl, min(tr, mid), 2 * index + 1);
  int t2 = find(mid + 1, r, max(tl, mid + 1), tr, 2 * index + 2);
  return max(t1, t2);
}

void solve() {
  int n, k;
  cin >> n >> k;
  v = t = vector<int>(n);
  tree = vector<int>(4 * (300001));
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
    int temp = find(0, 300000, max(v[i] - k, 0), min(v[i] + k, 300000), 0);
    // cout << temp << endl;
    t[v[i]] = temp + 1;
    update(0, 300000, 0, v[i]);
  }
  cout << tree[0] << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // Test
  solve();
  return 0;
}


Fixed submission:

// Problem : D - Flat Subsequence
// Contest : AtCoder - ACL Beginner Contest
// URL : https://atcoder.jp/contests/abl/tasks/abl_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

// $%U%$
// Mere bas ka nahi hai
// still i'm trying
// apna time aayega
//
//        .--------------.
//        | Try First One|
//        '--------------'
//                |     .--------------.
//                |     |              |
//                V     V              |
//              .--------------.       |
//              |      AC.     |<---.  |
//              '--------------'    |  |
//              (True)|  |(False)   |  |
//           .--------'  |          |  |
//           |           V          |  |
//           |  .--------------.    |  |
//           |  |   Try Again  |----'  |
//           |  '--------------'       |
//           |                         |
//           |  .--------------.       |
//           '->| Try Next One |-------'
//              '--------------'
//
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll power(ll x, ll y, ll m) {
  if (y == 0)
    return 1;
  ll p = power(x, y / 2, m) % m;
  p = (p * p) % m;
  return (y % 2 == 0) ? p : (x * p) % m;
}
ll nCr(ll n, ll r, ll m) {
  if (r > n)
    return 0;
  ll a = 1, b = 1, i;
  for (i = 0; i < r; i++) {
    a = (a * n) % m;
    --n;
  }
  while (r) {
    b = (b * r) % m;
    --r;
  }
  return (a * power(b, m - 2, m)) % m;
}
#define Test                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define endl '\n'
vector<int> tree, v, t;
void update(int l, int r, int index, int cindex) {
  if (l == r) {
    tree[index] = t[l];
    return;
  }
  int mid = (l + r) / 2;
  if (cindex <= mid)
    update(l, mid, 2 * index + 1, cindex);
  else
    update(mid + 1, r, 2 * index + 2, cindex);
  tree[index] = max(tree[index * 2 + 1], tree[index * 2 + 2]);
}

int find(int l, int r, int tl, int tr, int index) {
  if (tl > tr) {
    return 0;
  }
  if (tl == l && tr == r) {
    return tree[index];
  }
  int mid = (l + r) / 2;
  int t1 = find(l, mid, tl, min(tr, mid), 2 * index + 1);
  int t2 = find(mid + 1, r, max(tl, mid + 1), tr, 2 * index + 2);
  return max(t1, t2);
}

void solve() {
  int n, k;
  cin >> n >> k;
  v = vector<int>(n);
  t = vector<int>(300001);
  tree = vector<int>(4 * (300001));
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
    int temp = find(0, 300000, max(v[i] - k, 0), min(v[i] + k, 300000), 0);
    // cout << temp << endl;
    t[v[i]] = temp + 1;
    update(0, 300000, 0, v[i]);
  }
  cout << tree[0] << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // Test
  solve();
  return 0;
}