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

#include <algorithm>
#include <atcoder/segtree>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <utility>
#include <vector>
#define s(n) scanf("%d", &n)
#define sc(n) scanf("%c", &n)
#define sl(n) scanf("%I64d", &n)
#define sf(n) scanf("%lf", &n)
#define ss(n) scanf("%s", n)
#define INF (int)1e9
#define EPS 1e-9
#define bitcount __builtin_popcount
#define gcd __gcd
#define forall(i, a, b) for (int i = a; i < b; i++)
#define foreach(v, c)                                                          \\
  for (typeof((c).begin()) v = (c).begin(); v != (c).end(); ++v)
#define all(a) a.begin(), a.end()
#define in(a, b) ((b).find(a) != (b).end())
#define pb push_back
#define fill(a, v) memset(a, v, sizeof a)
#define sz(a) ((int)(a.size()))
#define mp make_pair
#define MOD 1000000007
typedef long long ll;
/*For you see , each day i love you
 more.Today more than yesterday
  less than tomorrow.*/
using namespace std;
using namespace atcoder;

int dp[300001];

int op(int a, int b) { return max(a, b); }
int e() { return 0; }

int main() {
  int n, k;
  cin >> n >> k;
  segtree<int, op, e> seg(300001);

  vector<int> vec;

  for (int i = 0; i < n; i++) {
    int x;
    s(x);
    vec.push_back(x);
  }

  int maxii = 0;

  for (int i = n - 1; i >= 0; i--) {
    int low = max(0, vec[i] - k);
    int high = min(300000, vec[i] + k);

    int maxi = seg.prod(low, high + 1);

    dp[i] = 1 + maxi;
    maxii = max(maxii, dp[i]);

    seg.set(vec[i], dp[i]);
  }

  cout << maxii << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <utility>
#include <vector>
#define s(n) scanf("%d", &n)
#define sc(n) scanf("%c", &n)
#define sl(n) scanf("%I64d", &n)
#define sf(n) scanf("%lf", &n)
#define ss(n) scanf("%s", n)
#define INF (int)1e9
#define EPS 1e-9
#define bitcount __builtin_popcount
#define gcd __gcd
#define forall(i, a, b) for (int i = a; i < b; i++)
#define foreach(v, c)                                                          \\
  for (typeof((c).begin()) v = (c).begin(); v != (c).end(); ++v)
#define all(a) a.begin(), a.end()
#define in(a, b) ((b).find(a) != (b).end())
#define pb push_back
#define fill(a, v) memset(a, v, sizeof a)
#define sz(a) ((int)(a.size()))
#define mp make_pair
#define MOD 1000000007
typedef long long ll;
/*For you see , each day i love you
 more.Today more than yesterday
  less than tomorrow.*/
using namespace std;

int seg_tree[1100000];

int dp[300001];

int find_max(int left, int right, int low, int high, int cur) {
  //	cout<<left<<" "<<right<<" "<<low<<" "<<high<<endl;
  if (left == right) {

    //	cout<<'('<<left<<" "<<right<<" "<<low<<" "<<high<<endl;
    return seg_tree[cur];
  }

  int mid = (left + right) / 2;

  if (low >= left && high <= mid) {
    return find_max(left, mid, low, high, cur * 2 + 1);
  } else if (low > mid && high <= right) {
    return find_max(mid + 1, right, low, high, cur * 2 + 2);
  } else {
    int low1 = max(left, low);
    int maxi1 = find_max(left, mid, low1, mid, cur * 2 + 1);
    int high1 = min(right, high);

    int maxi2 = find_max(mid + 1, right, mid + 1, high1, cur * 2 + 2);

    return max(maxi1, maxi2);
  }
}

void fix_it(int left, int right, int key, int ne, int cur) {
  // cout<<left<<" "<<right<<endl;
  if (left == right) {
    seg_tree[cur] = max(ne, seg_tree[cur]);
    return;
  }

  int mid = (left + right) / 2;

  if (key <= mid) {
    fix_it(left, mid, key, ne, cur * 2 + 1);
  } else {
    fix_it(mid + 1, right, key, ne, cur * 2 + 2);
  }
}

int main() {
  int n, k;
  cin >> n >> k;

  vector<int> vec;

  for (int i = 0; i < n; i++) {
    int x;
    s(x);
    vec.push_back(x);
  }

  for (int i = n - 1; i >= 0; i--) {
    int low = max(0, vec[i] - k);
    int high = min(300000, vec[i] + k);

    int maxi = find_max(0, 300000, low, high, 0);

    dp[i] = 1 + maxi;

    fix_it(0, 300000, vec[i], dp[i], 0);
  }

  cout << dp[0] << endl;

  return 0;
}