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

#pragma GCC optimize(2)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define lowbit(n) n & (-n)
#define ls x << 1
#define rs x << 1 | 1
#define clc(a, b) memset(a, b, sizeof(a))
#define Buff ios::sync_with_stdio(false)
#define rush()                                                                 \\
  int Case = 0;                                                                \\
  int T;                                                                       \\
  scanf("%d", &T);                                                             \\
  while (T--)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#define reps(i, a, b) for (int i = a; b; i++)
#define read(a) scanf("%d", &a)
#define readd(a) scanf("%lf", &a)
#define readl(a) scanf("%lld", &a)
#define readc(a) scanf("%c", &a)
#define reads(a) scanf("%s", a)
#define sqr(x) x *x
#define pb push_back
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const int mod = 1e9 + 7;
const double eps = 1e-6;
const int N = 3e5 + 7;
int b[N];
struct BIT // binary index tree
{
  int t[N], n;
  int a[N];
#define lowbit(x) (x & (-x))
  void init(int n_) {
    n = n_;
    for (int i = 1; i <= n; i++)
      t[i] = a[i] = 0;
  }
  int query(int l, int r) {
    int res = a[r];
    while (r > l) // t[i]维护 [i-lowbit(i)+1,i]的最值
    {
      for (--r; r - lowbit(r) >= l;
           r -= lowbit(r)) // r-lowbit(r)+1>=l return max t[r],query(l,r-low(r))
        res = max(res, t[r]); // r-lowbit(r)+1<l return max a[r],query(l,r-1)
      res = max(res, a[r]);
    }
    return res;
  }
  void modify(int x, int v) {
    a[x] = v;
    while (x <= n) {
      t[x] = a[x];
      for (int i = 1, j = lowbit(x); i < j; i <<= 1) // y+( lowbit(y)=2^i) =x
        t[x] = max(
            t[x],
            t[x -
              i]); // 对pos=x的修改需要考虑其维护的(x-lowbit(x),x]区间里的数对该区间最值贡献
      x += lowbit(x); // 转移至包含该区间的下一个位置
    }
  }
};
BIT tree;
int main() {
  Buff;
  int n, k, maxv = 1, res = 0;
  cin >> n >> k;
  rep(i, 1, n) {
    cin >> b[i];
    b[i]++;
    maxv = max(maxv, b[i]);
  }
  tree.init(maxv);
  rep(i, 1, n) {
    int l = max(1, b[i] - k);
    int r = min(maxv, b[i] + k);
    int t = tree.query(l, r);
    //        cout <<"t:"<< t <<endl;
    res = max(res, t + 1);
    tree.modify(b[i], t + 1);
  }
  cout << res << endl;
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize(2)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define lowbit(n) n & (-n)
#define ls x << 1
#define rs x << 1 | 1
#define clc(a, b) memset(a, b, sizeof(a))
#define Buff ios::sync_with_stdio(false)
#define rush()                                                                 \\
  int Case = 0;                                                                \\
  int T;                                                                       \\
  scanf("%d", &T);                                                             \\
  while (T--)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#define reps(i, a, b) for (int i = a; b; i++)
#define read(a) scanf("%d", &a)
#define readd(a) scanf("%lf", &a)
#define readl(a) scanf("%lld", &a)
#define readc(a) scanf("%c", &a)
#define reads(a) scanf("%s", a)
#define sqr(x) x *x
#define pb push_back
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const int mod = 1e9 + 7;
const double eps = 1e-6;
const int N = 3e5 + 7;
int a[N], f[N];
struct BIT {
  int n, bit[N];
  void init() { clc(bit, 0), clc(f, 0); }
  int query(int l, int r) {
    int res = f[r];
    for (; r > l;) {
      for (r--; r - lowbit(r) >= l; r -= lowbit(r))
        res = max(res, bit[r]);
      res = max(res, f[r]);
    }
    return res;
  }
  void modify(int p, int v) {
    f[p] = v;
    for (int i = p; i <= n; i += lowbit(i)) {
      bit[i] = v;
      for (int j = 1; j < lowbit(i); j <<= 1)
        bit[i] = max(bit[i], bit[i - j]);
    }
  }
} tree;
int main() {
  Buff;
  int n, k, maxv = 0, res = 0;
  cin >> n >> k;
  tree.init();
  rep(i, 1, n) {
    cin >> a[i];
    a[i]++;
    maxv = max(maxv, a[i]);
  }
  tree.n = maxv;
  for (int i = 1; i <= n; i++) {
    int l = max(1, a[i] - k);
    int r = min(maxv, a[i] + k);
    int t = tree.query(l, r);
    //        cout <<"t:"<< t <<endl;
    res = max(res, t + 1);
    tree.modify(a[i], t + 1);
  }
  cout << res << endl;
  return 0;
}