Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
//
// using namespace __gnu_pbds;
// template<typename T> using ordered_set = tree<T, null_type, less<T>,
// rb_tree_tag, tree_order_statistics_node_update>;

// class Timer {
//     clock_t start;
//     string name;
// public:
//     Timer() {name = "";start = clock();}
//     Timer(string s){name = s;start = clock();}
//     ~Timer() {fprintf(stderr, "%s: %.3gs\\n", name.c_str(), 1.0*(clock() -
//     start) / CLOCKS_PER_SEC);}
// };

// #define isValid(a, b) ((a) >= 0 && (a) < (b))
// int dr[]  =  {0, -1, -1, -1,  0,  1, 1, 1};
// int dc[]  =  {1,  1,  0, -1, -1, -1, 0, 1};

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define um unordered_map
#define us unordered_set
#define endl '\\n'
#define what_is(x)                                                             \\
  cerr << fixed << setprecision(2) << boolalpha << #x << " = " << x << endl

const double EPS = 1e-9;
const double PI = acos(-1.0);

template <typename T> inline T sq(T a) { return a * a; }
template <typename T1, typename T2> inline pair<T1, T2> mp(T1 a, T2 b) {
  return make_pair(a, b);
}
template <typename T1, typename T2> inline T1 safeMod(T1 a, T2 m) {
  return (a % m + m) % m;
} /*handling negative sign of remainder*/
template <typename T1, typename T2> inline bool isEq(T1 a, T2 b) {
  return abs(a - b) < EPS;
}
template <typename T1, typename T2, typename T3>
inline bool isEq(T1 a, T2 b, T3 eps) {
  return abs(a - b) < eps;
}
template <typename T> inline bool isKthBitOn(T n, int k) {
  assert(n <= numeric_limits<T>::max());
  assert(k <= numeric_limits<T>::digits);
  T ONE = 1;
  return bool((n & (ONE << k)));
}
template <typename T> inline void setKthBit(T &n, int k) {
  assert(n <= numeric_limits<T>::max());
  assert(k <= numeric_limits<T>::digits);
  T ONE = 1;
  n = (n | (ONE << k));
}
// mt19937 rng((int)chrono::steady_clock::now().time_since_epoch().count());
// template<typename T> inline T nextRand(T lo, T hi){return
// uniform_int_distribution<T>(lo, hi)(rng);}

#define toRad(deg) ((deg)*PI / 180.0)
#define toDeg(rad) ((rad)*180.0 / PI)

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int oo = 0x3f3f3f3f;
const int MAX = 200010;
const int MOD = 1000000007;
const int precision = 10;
//
// #define max(a, b) ((a) > (b))?(a):(b)
// #define min(a, b) ((a) < (b))?(a):(b)

int tree[4 * 300002];
int mv;
void update(int p, int v) {
  p += mv;
  tree[p] = max(tree[p], v);
  while (p > 1) {
    tree[p / 2] = max(tree[p], tree[p ^ 1]);
    p /= 2;
  }
}
int query(int l, int r) //[l, r)
{
  int mx = 0;
  l += mv;
  r += mv;
  while (l < r) {
    if (l & 1)
      mx = max(mx, tree[l++]);
    if (r & 1)
      mx = max(mx, tree[--r]);
    l /= 2;
    r /= 2;
  }
  return mx;
}

int v[300005];
void solve(int kas) {
  int n, k;
  scanf("%d %d", &n, &k);
  mv = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &v[i]);
    mv = max(mv, v[i] + k);
  }
  for (int i = mv - 1; i > 0; i--) {
    tree[i] = max(tree[i << 1], tree[(i << 1) | 1]);
  }
  int mx = 0;
  for (int i = n - 1; i >= 0; i--) {
    int l = max(0, v[i] - k);
    int r = min(mv, v[i] + k);
    int mm = query(l, r + 1);
    mx = max(mx, 1 + mm);
    update(v[i], 1 + mm);
    // cout << i << " " << v[i] << " " << mm+1 << endl;
  }
  printf("%d\\n", mx);
}

int main() {
  // assert(freopen("in.txt", "r", stdin));
  // assert(freopen("out.txt", "w", stdout));
  // cout << fixed << setprecision(precision);
  int tc = 1;
  // cin >> tc;
  for (int _ = 1; _ <= tc; _++) {
    solve(_);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
//
// using namespace __gnu_pbds;
// template<typename T> using ordered_set = tree<T, null_type, less<T>,
// rb_tree_tag, tree_order_statistics_node_update>;

// class Timer {
//     clock_t start;
//     string name;
// public:
//     Timer() {name = "";start = clock();}
//     Timer(string s){name = s;start = clock();}
//     ~Timer() {fprintf(stderr, "%s: %.3gs\\n", name.c_str(), 1.0*(clock() -
//     start) / CLOCKS_PER_SEC);}
// };

// #define isValid(a, b) ((a) >= 0 && (a) < (b))
// int dr[]  =  {0, -1, -1, -1,  0,  1, 1, 1};
// int dc[]  =  {1,  1,  0, -1, -1, -1, 0, 1};

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define um unordered_map
#define us unordered_set
#define endl '\\n'
#define what_is(x)                                                             \\
  cerr << fixed << setprecision(2) << boolalpha << #x << " = " << x << endl

const double EPS = 1e-9;
const double PI = acos(-1.0);

template <typename T> inline T sq(T a) { return a * a; }
template <typename T1, typename T2> inline pair<T1, T2> mp(T1 a, T2 b) {
  return make_pair(a, b);
}
template <typename T1, typename T2> inline T1 safeMod(T1 a, T2 m) {
  return (a % m + m) % m;
} /*handling negative sign of remainder*/
template <typename T1, typename T2> inline bool isEq(T1 a, T2 b) {
  return abs(a - b) < EPS;
}
template <typename T1, typename T2, typename T3>
inline bool isEq(T1 a, T2 b, T3 eps) {
  return abs(a - b) < eps;
}
template <typename T> inline bool isKthBitOn(T n, int k) {
  assert(n <= numeric_limits<T>::max());
  assert(k <= numeric_limits<T>::digits);
  T ONE = 1;
  return bool((n & (ONE << k)));
}
template <typename T> inline void setKthBit(T &n, int k) {
  assert(n <= numeric_limits<T>::max());
  assert(k <= numeric_limits<T>::digits);
  T ONE = 1;
  n = (n | (ONE << k));
}
// mt19937 rng((int)chrono::steady_clock::now().time_since_epoch().count());
// template<typename T> inline T nextRand(T lo, T hi){return
// uniform_int_distribution<T>(lo, hi)(rng);}

#define toRad(deg) ((deg)*PI / 180.0)
#define toDeg(rad) ((rad)*180.0 / PI)

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int oo = 0x3f3f3f3f;
const int MAX = 200010;
const int MOD = 1000000007;
const int precision = 10;

int tree[4 * 300000];
#define max(a, b) ((a) > (b)) ? (a) : (b)
#define min(a, b) ((a) < (b)) ? (a) : (b)
void update(int n, int l, int r, int p, int v) {
  if (l == r) {
    if (l == p)
      tree[n] = max(tree[n], v);
    return;
  }
  int m = (l + r) / 2;
  update(2 * n, l, m, p, v);
  update(2 * n + 1, m + 1, r, p, v);
  tree[n] = max(tree[n << 1], tree[(n << 1) | 1]);
}

int query(int n, int l, int r, int ql, int qr) {
  if (r < ql || qr < l)
    return 0;
  if (ql <= l && r <= qr)
    return tree[n];
  int m = (l + r) / 2;
  return max(query(n << 1, l, m, ql, qr),
             query((n << 1) | 1, m + 1, r, ql, qr));
}

int v[300005];
void solve(int kas) {
  int n, k;
  scanf("%d %d", &n, &k);
  int mv = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &v[i]);
    mv = max(mv, v[i]);
  }
  int mx = 0;
  for (int i = n - 1; i >= 0; i--) {
    int l = max(0, v[i] - k);
    int r = min(mv, v[i] + k);
    int mm = query(1, 0, mv, l, r);
    mx = max(mx, 1 + mm);
    update(1, 0, mv, v[i], 1 + mm);
    // cout << i << " " << v[i] << " " << mx << endl;
  }
  printf("%d\\n", mx);
}

int main() {
  // assert(freopen("in.txt", "r", stdin));
  // assert(freopen("out.txt", "w", stdout));
  // cout << fixed << setprecision(precision);
  int tc = 1;
  // cin >> tc;
  for (int _ = 1; _ <= tc; _++) {
    solve(_);
  }
  return 0;
}