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

// Coded by Abhijay Mitra
#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <vector>
#define double long double
#define int long long int
#define ll int
#define ibs ios_base::sync_with_stdio(false)
#define cti cin.tie(0)
#define bp __builtin_popcount
#define pb emplace_back
#define koto_memory(a) cout << (sizeof(a) / 1048576.0) << " MB";
#define res(v) sort(all(v)), v.erase(unique(all(v)), v.end());
#define timer                                                                  \\
  cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec "       \\
       << endl;
#define deb(x) cout << endl << "[" << #x << " = " << x << "]" << endl;
using vi = std::vector<int>;
using vvi = std::vector<vi>;
using pii = std::pair<int, int>;
using vpii = std::vector<pii>;
using vvpii = std::vector<vpii>;
#define mp make_pair
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, b, a) for (int i = b; i >= a; i--)
#define all(x) x.begin(), x.end()
using namespace std;
const int N = 3e6 + 10;
const int inf = /*0x3f3f3f3f*/ 1e18 + 10;
// const ll M = 998244353 ; // Mulo
// const int M = 1e9+7 ; // Mulo
const double Pi = 3.14159265;
#define F first
#define S second
int n, k, a[N], ans = 1, z = 1;
unordered_map<int, int> t;
// queries
int sum(int l, int r, int v = 1, int tl = 0, int tr = N) {
  if (l > r)
    return 0;
  if (l == tl && r == tr)
    return t[v];
  int tm = (tl + tr) / 2;
  return max(sum(l, min(r, tm), v * 2, tl, tm),
             sum(max(l, tm + 1), r, v * 2 + 1, tm + 1, tr));
}
//
void update(int pos, int new_val, int v = 1, int tl = 0, int tr = N) {
  if (tl == tr) {
    t[v] = new_val;
  } else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      update(pos, new_val, v * 2, tl, tm);
    else
      update(pos, new_val, v * 2 + 1, tm + 1, tr);
    t[v] = max(t[v * 2], t[v * 2 + 1]);
  }
}
void solve() {
  cin >> n >> k;
  rep(i, 1, n) {
    cin >> a[i];
    z = sum(max(a[i] - k, 0LL), min(a[i] + k, N)) + 1;
    update(a[i], max(z, 1LL));
    ans = max(ans, z);
  }
  cout << ans;
}
int32_t main() {
  ibs;
  cti;
  solve();
  return 0;
  int xx = 0;
  int t;
  cin >> t;
  while (t--) { /*xx++;cout<<"Case "<<xx<<": "*/
    ;
    solve();
    cout << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// Coded by Abhijay Mitra
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <vector>
// #include <bits/stdc++.h>
#define double long double
#define int long long int
#define ll int
#define ibs ios_base::sync_with_stdio(false)
#define cti cin.tie(0)
#define bp __builtin_popcount
#define pb emplace_back
#define koto_memory(a) cout << (sizeof(a) / 1048576.0) << " MB";
#define res(v) sort(all(v)), v.erase(unique(all(v)), v.end());
#define timer                                                                  \\
  cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec "       \\
       << endl;
#define deb(x) cout << endl << "[" << #x << " = " << x << "]" << endl;
using vi = std::vector<int>;
using vvi = std::vector<vi>;
using pii = std::pair<int, int>;
using vpii = std::vector<pii>;
using vvpii = std::vector<vpii>;
#define mp make_pair
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, b, a) for (int i = b; i >= a; i--)
#define all(x) x.begin(), x.end()
using namespace std;
const int N = 6e6 + 10;
const int inf = /*0x3f3f3f3f*/ 1e18 + 10;
// const ll M = 998244353 ; // Mulo
// const int M = 1e9+7 ; // Mulo
const double Pi = 3.14159265;
#define F first
#define S second
int n, k, a[N], ans = 1, z = 1;
map<int, int> t;
// queries
int sum(int l, int r, int v = 1, int tl = 0, int tr = N) {
  if (l > r)
    return 0;
  if (l == tl && r == tr)
    return t[v];
  int tm = (tl + tr) / 2;
  return max(sum(l, min(r, tm), v * 2, tl, tm),
             sum(max(l, tm + 1), r, v * 2 + 1, tm + 1, tr));
}
//
void update(int pos, int new_val, int v = 1, int tl = 0, int tr = N) {
  if (tl == tr) {
    t[v] = new_val;
  } else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      update(pos, new_val, v * 2, tl, tm);
    else
      update(pos, new_val, v * 2 + 1, tm + 1, tr);
    t[v] = max(t[v * 2], t[v * 2 + 1]);
  }
}
void solve() {
  cin >> n >> k;
  rep(i, 1, n) {
    cin >> a[i];
    z = sum(max(a[i] - k, 0LL), min(a[i] + k, N)) + 1;
    update(a[i], max(z, 1LL));
    ans = max(ans, z);
  }
  cout << ans;
}
int32_t main() {
  ibs;
  cti;
  solve();
  return 0;
  int xx = 0;
  int t;
  cin >> t;
  while (t--) { /*xx++;cout<<"Case "<<xx<<": "*/
    ;
    solve();
    cout << endl;
  }
  return 0;
}