alexjercan/bugnet huggingface dataset:
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll parent[1000000], Rank[1000000];

void initialize() {
  for (ll i = 1; i <= 1000000; i++) {
    parent[i] = i;
    Rank[i] = 1;
  }
}

ll Find(ll r) {
  if (parent[r] == r)
    return r;

  parent[r] = Find(parent[r]);

  return parent[r];
}
ll component, ans = 0;
void Union(ll x, ll y) {
  ll u = Find(x);

  ll v = Find(y);

  if (u != v) { // cout<<"The are different Component so that the include in the
                // same component";

    component--; // first a sob gula alada component e chilo ar ekhon ekoi
                 // component e ene dichi tai comonent num ek ek kore komachi

    if (Rank[v] > Rank[u]) {
      Rank[v] += Rank[u];
      parent[u] = v;
    } // zetar child beshi setar sathe new child add korchi abong mot child num
      // add korchi

    else {
      Rank[u] += Rank[v];
      parent[v] = u;
    }

    // ll mx=max(Rank[u],Rank[v]);

    // ans=max(ans,mx);

    //  cout<<"u ="<<Rank[u]<<endl;

    // cout<<"v= "<<Rank[v]<<endl;

    // ekoi componet e rakhlam

  } else { // cout<<"The are same component";
  }
}

int main() {
  initialize();

  ll N, E, u, v;

  cin >> N;
  cin >> E;
  component = N;
  for (ll i = 1; i <= E; i++) {
    cin >> u >> v;

    Union(u, v);
  }
  cout << component - 1 << endl;
  // cout<<ans<<endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll parent[1000], Rank[1000];

void initialize() {
  for (ll i = 1; i <= 1000; i++) {
    parent[i] = i;
    Rank[i] = 1;
  }
}

ll Find(ll r) {
  if (parent[r] == r)
    return r;

  parent[r] = Find(parent[r]);

  return parent[r];
}
ll component, ans = 0;
void Union(ll x, ll y) {
  ll u = Find(x);

  ll v = Find(y);

  if (u != v) { // cout<<"The are different Component so that the include in the
                // same component";

    component--; // first a sob gula alada component e chilo ar ekhon ekoi
                 // component e ene dichi tai comonent num ek ek kore komachi

    if (Rank[v] > Rank[u]) {
      Rank[v] += Rank[u];
      parent[u] = v;
    } // zetar child beshi setar sathe new child add korchi abong mot child num
      // add korchi

    else {
      Rank[u] += Rank[v];
      parent[v] = u;
    }

    // ll mx=max(Rank[u],Rank[v]);

    // ans=max(ans,mx);

    //  cout<<"u ="<<Rank[u]<<endl;

    // cout<<"v= "<<Rank[v]<<endl;

    // ekoi componet e rakhlam

  } else { // cout<<"The are same component";
  }
}

int main() {
  initialize();

  ll N, E, u, v;

  cin >> N;
  cin >> E;
  component = N;
  for (ll i = 1; i <= E; i++) {
    cin >> u >> v;

    Union(u, v);
  }
  cout << component - 1 << endl;
  // cout<<ans<<endl;
}
Problem p02536 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define rep(i, n) for (ll i = 0; i < n; i++)
class UnionFind {
  vector<int> par;
  vector<int> siz;

public:
  UnionFind(int n) : par(n), siz(n, 1) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
    }
  }
  int root(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = root(par[x]);
    }
  }
  bool same(int x, int y) { return root(x) == root(y); }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return 0;
    }
    if (siz[x] == siz[y]) {
      swap(x, y);
    }
    siz[x] += siz[y];
    par[y] = x;
    return 1;
  }
  int size(int x) { return siz[root(x)]; }
};
int main() {
  int n, q, a, b;
  cin >> n >> q;
  int ans = n - 1;
  UnionFind uf(n);
  rep(i, q) {
    cin >> a >> b;
    a--;
    b--;
    if (uf.unite(a, b)) {
      ans--;
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define rep(i, n) for (ll i = 0; i < n; i++)
class UnionFind {
  vector<int> par;
  vector<int> siz;

public:
  UnionFind(int n) : par(n), siz(n, 1) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
    }
  }
  int root(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = root(par[x]);
    }
  }
  bool same(int x, int y) { return root(x) == root(y); }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return 0;
    }
    if (siz[x] == siz[y]) {
      swap(x, y);
    }
    siz[x] += siz[y];
    par[y] = x;
    return 1;
  }
  int size(int x) { return siz[root(x)]; }
};
int main() {
  int n, q, a, b;
  cin >> n >> q;
  int ans = n - 1;
  UnionFind uf(n);
  rep(i, q) {
    cin >> a >> b;

    if (uf.unite(a, b)) {
      ans--;
    }
  }
  cout << ans << endl;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define endl '\\n'
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repr(i, a, b) for (int i = a; i >= b; i--)
#define int long long
#define sz(x) (int)x.size()
#define mp make_pair
#define fi first
#define se second
#define io ios_base::sync_with_stdio(false), cin.tie(NULL)
using namespace std;
#define INF 1e9
int tree[1000000];
int n = 300001;
void update(int l, int v) {
  tree[l + n] = v;
  for (int i = l + n; i > 0; i = i >> 1)
    tree[i >> 1] = max(tree[i], tree[i ^ 1]);
}
int query(int l, int r) {
  int res = 0;
  for (l = l + n, r = r + n; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      res = max(res, tree[l++]);
    if (r & 1)
      res = max(res, tree[--r]);
  }
  return res;
}
signed main() {
  io;
  int s, k;
  cin >> s >> k;
  int a[s];
  rep(i, 0, s) cin >> a[i];

  memset(tree, 0, sizeof(tree));
  for (auto it : a) {
    int c = query(max((ll)0, it - k), min(it + k, (ll)300000) + 1);
    update(it, c + 1);
  }
  cout << query(0, n);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define endl '\\n'
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repr(i, a, b) for (int i = a; i >= b; i--)
#define int long long
#define sz(x) (int)x.size()
#define mp make_pair
#define fi first
#define se second
#define io ios_base::sync_with_stdio(false), cin.tie(NULL)
using namespace std;
#define INF 1e7

signed main() {
  io;
  int n, k;
  cin >> n >> k;
  int a[n];
  rep(i, 0, n) cin >> a[i];
  int dp[n];

  dp[0] = 1;
  int ans = 1;
  for (int i = 1; i < n; i++) {
    dp[i] = 1;
    for (int j = 0; j < i; j++) {
      if (abs(a[j] - a[i]) <= k) {
        dp[i] = max(dp[i], 1 + dp[j]);
      }
    }
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(), (v).end()

using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;
using vii = vector<vector<int>>;

const ll LINF = 1LL << 60;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;

template <typename T> bool chmin(T &a, const T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmax(T &a, const T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int n_;
vector<int> dat;

void init(int size) {
  // initialize size=a power of 2
  n_ = 1;
  while (n_ < size)
    n_ *= 2;
  dat.assign(2 * n_ - 1, 0);
}

// a_id(0-indexed) -> val
void update(int id, int val) {
  // id of leaf
  id += n_ - 1;
  dat[id] = val;
  while (id > 0) {
    id = (id - 1) / 2;
    dat[id] = max(dat[2 * id + 1], dat[2 * id + 2]);
  }
}

// get Min{a_i | s <= i < t}, [s, t)
// dat[id] interval -> [l, r)
// first, query(s, t, 0, 0, n_)
int query(int s, int t, int id, int l, int r) {
  if (r <= s || t <= l)
    return 0;

  if (s <= l && r <= t)
    return dat[id];
  int vl = query(s, t, 2 * id + 1, l, (l + r) / 2);
  int vr = query(s, t, 2 * id + 2, (l + r) / 2, r);
  return max(vl, vr);
}

int main() {
  int N, K;
  cin >> N >> K;
  vi A(N);
  int mx = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    chmax(mx, A[i]);
  }
  vi nA = A;
  sort(all(nA));

  init(mx + 1);
  for (int i = 0; i < N; i++) {
    int id1 = lower_bound(all(nA), A[i] - K) - nA.begin();
    int id2 = upper_bound(all(nA), A[i] + K) - nA.begin();
    update(A[i], query(nA[id1], nA[id2 - 1] + 1, 0, 0, n_) + 1);
  }
  cout << query(0, n_, 0, 0, n_) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(), (v).end()

using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;
using vii = vector<vector<int>>;

const ll LINF = 1LL << 60;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;

template <typename T> bool chmin(T &a, const T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmax(T &a, const T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int N, K;
  cin >> N >> K;
  vi A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  vi dp(N, 1);
  int ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < i; j++) {
      if (abs(A[i] - A[j]) <= K)
        chmax(dp[i], dp[j] + 1);
    }
    chmax(ans, dp[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

// D - Flat Subsequence
#include <bits/stdc++.h>
using namespace std;

#include <atcoder/segtree>
int op(int a, int b) { return max(a, b); } // 処理
int e() { return 0; }                      // 単位元
using MONOID = int;
using SEG_T = atcoder::segtree<MONOID, op, e>;
#define SEGT_MAX 300005

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> A(n);
  for (auto &a : A)
    cin >> a;
  SEG_T seg(SEGT_MAX);
  int ans = 0;
  for (int &a : A) {
    int l = max(0, a - k);
    int r = min(a + k + 1, SEGT_MAX);
    int longest = seg.prod(l, r); // [l,r)
    seg.set(a, longest + 1);
    ans = max(ans, longest + 1);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

// D - Flat Subsequence
#include <bits/stdc++.h>
using namespace std;
#define rp(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)
#define chmax(x, y) x = max(x, (y))

#define MAX 300'005
int dp[MAX];

int main() {
  int n, k;
  cin >> n >> k;
  int ans = 0;
  rp(i, 0, n) {
    int ai;
    cin >> ai;
    int longest = 0;
    int l = max(0, ai - k);
    int r = min(ai + k + 1, MAX);
    rp(aj, l, r) chmax(longest, dp[aj]); // [l,r)
    chmax(dp[ai], longest + 1);
    chmax(ans, dp[ai]);
  }
  cout << ans << endl;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <atcoder/segtree>
#include <bits/stdc++.h>
using namespace std;
#define all(a) a.begin(), a.end()
using ll = long long;
const int INF = 1 << 30;
const ll INFll = 1LL << 62;
const int mod = int(1e9) + 7;
// const int mod=998244353;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using ld = long double;
using V = vector<int>;
using Vl = vector<ll>;
using VV = vector<vector<int>>;
using VVl = vector<vector<ll>>;

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, k;
  cin >> n >> k;
  V a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  const int MAX_A = 300101;
  atcoder::segtree<int, op, e> dp(MAX_A);
  for (int i = 0; i < n; ++i) {
    int l, r;
    l = a[i] - k;
    r = a[i] + k + 1;
    l = max(0, l);
    r = min(MAX_A, r);
    int now = dp.prod(l, r);
    dp.set(a[i], now + 1);
  }
  cout << dp.all_prod() << "\\n";
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define all(a) a.begin(), a.end()
using ll = long long;
const int INF = 1 << 30;
const ll INFll = 1LL << 62;
const int mod = int(1e9) + 7;
// const int mod=998244353;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using ld = long double;
using V = vector<int>;
using Vl = vector<ll>;
using VV = vector<vector<int>>;
using VVl = vector<vector<ll>>;

int MAX_N = 300001;
VVl bit(MAX_N, Vl()); // 1-index
void add(ll i, ll a, ll w) {
  for (ll x = a; x <= MAX_N; x += x & -x)
    bit[x][i] += w;
}
ll sum(ll i, ll a) { // sum v[1]～v[a]
  ll ret = 0;
  for (int x = a; x > 0; x -= x & -x)
    ret += bit[x][i];
  return ret;
}

int main() {
  ll n, k;
  cin >> n >> k;
  Vl a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  ll mm = 0;
  Vl tp(MAX_N, 0);
  for (ll i = 0; i < n; ++i) {
    for (ll j = mm; j >= 0; --j) {
      //            cout <<a[i]<<" "<<j<<endl;
      if (j == 0) {
        if (bit.size() == j) {
          bit.push_back(tp);
        }
        if (a[i] + k + 1 <= MAX_N) {
          add(j + 1, a[i] + k + 1, -1);
        }
        if (a[i] - k >= 0) {
          add(j + 1, a[i] - k, 1);
        }
        mm = max(mm, j + 1);
        //                cout <<j+1<<" "<<a[i]<<endl;
      } else {

        if (sum(j, a[i])) {
          if (bit.size() == j) {
            bit.push_back(tp);
          }
          if (a[i] + k + 1 <= MAX_N) {
            add(j + 1, a[i] + k + 1, -1);
          }
          if (a[i] - k >= 0) {
            add(j + 1, a[i] - k, 1);
          }
          mm = max(mm, j + 1);
        }
      }
    }
  }
  cout << mm << endl;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define int long long
#define endl '\\n'

using namespace std;

const int N = 3e5 + 77;
int A[N], dp[N];

int32_t main() {
  fast;
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> A[i];
  }
  for (int i = 1; i <= n; ++i) {
    dp[i] = 1;
    for (int j = i - 1; i - j <= 100 && j >= 1; --j)
      if (abs(A[i] - A[j]) <= k)
        dp[i] = max(dp[i], dp[j] + 1);
  }
  int ans = 0;
  // for(int i = 1 ; i <= n ; ++i) cout << dp[i] << ' ' ; cout << endl;
  for (int i = 1; i <= n; ++i)
    ans = max(ans, dp[i]);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define int long long
#define endl '\\n'

using namespace std;

const int N = 3e5 + 77;
int A[N], dp[N];

int32_t main() {
  fast;
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> A[i];
  }
  int ans = 0;
  dp[0] = 1;
  for (int i = 1; i < n; ++i) {
    for (int j = 0; j < i; ++j) {
      if (abs(A[i] - A[j]) <= k)
        dp[i] = max(dp[i], dp[j] + 1);
    }
  }
  for (int i = 0; i < n; ++i)
    ans = max(dp[i], ans);
  // for(int i = 0; i < n ; ++i) cout << dp[i] << ' ' ; cout << endl;
  cout << ans << endl;
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (n); ++i)

const int N = 6e5 + 7;
int n, a[N], k, tree[4 * N];

#define m (l + r) / 2
#define lson 2 * node
#define rson 2 * node + 1

void update(int pos, int v, int l, int r, int node) {
  if (l == r) {
    tree[node] = v;
    return;
  }
  if (pos <= m)
    update(pos, v, l, m, lson);
  else
    update(pos, v, m + 1, r, rson);
  tree[node] = max(tree[lson], tree[rson]);
}

int query(int ql, int qr, int l, int r, int node) {
  if (ql <= l && qr >= r)
    return tree[node];
  int ans = 0;
  if (ql <= m)
    ans = max(ans, query(ql, qr, l, m, lson));
  if (qr > m)
    ans = max(ans, query(ql, qr, m + 1, r, rson));
  return ans;
}

int main() {
  cin >> n >> k;
  rep(i, n) cin >> a[i + 1];
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    int res = query(max(a[i] - k, 0), a[i] + k, 0, N, 1) + 1;
    ans = max(ans, res);
    update(a[i], res, 0, N, 1);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<int> dis(n), pre(n);
  for (int i = 0; i < n; i++) {
    dis[i] = 1;
    pre[i] = -1;
    for (int j = i - 1; j >= 0; j--) {
      if (abs(a[i] - a[j]) <= k && dis[j] + 1 > dis[i]) {
        pre[i] = j;
        dis[i] = dis[j] + 1;
      }
    }
  }
  cout << dis[n - 1] << endl;
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 50;
int maxn[N << 2], a[N], f[N];
/*void change(int rt,int l,int r,int pos,int v)
{
        if(l==r&&l==pos)
        {
                maxx[rt]=v;
                return ;
        }
        int mid=(l+r)>>1;
        if(pos>mid) change(rt<<1|1,mid+1,r,pos,v);
        else change(rt<<1,l,mid,pos,v);
        maxx[rt]=max(maxx[rt<<1],maxx[rt<<1|1]);
}
int query(int rt,int l,int r,int x,int y)
{
        if(x>=l&&r<=y)
        {
                return maxx[rt];
        }
        int ans=0;
        int mid=(l+r)>>1;
        if(x<=mid) ans=max(ans,query(rt<<1,l,mid,x,y));
        if(mid<y) ans=max(ans,query(rt<<1|1,mid+1,r,x,y));
        return ans;
}*/
void change(int k, int l, int r, int pos, int v) {
  if (l == pos && pos == r) {
    maxn[k] = v;
    return;
  }
  int mid = l + r >> 1;
  if (pos <= mid)
    change(k << 1, l, mid, pos, v);
  else
    change(k << 1 | 1, mid + 1, r, pos, v);
  maxn[k] = max(maxn[k << 1], maxn[k << 1 | 1]);
}

int query(int k, int l, int r, int qx, int qy) {
  if (qx <= l && r <= qy)
    return maxn[k];
  int mid = l + r >> 1;
  int ans = 0;
  if (qx <= mid)
    ans = max(ans, query(k << 1, l, mid, qx, qy));
  if (mid < qy)
    ans = max(ans, query(k << 1 | 1, mid + 1, r, qx, qy));
  return ans;
}
int n, k;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  f[1] = 1;
  change(1, 0, 3e5, a[1], 1);
  for (int i = 2; i <= n; ++i) {
    int now = query(1, 0, 3e5, max(a[i] - k, 0), min(300000, a[i] + k));
    f[i] = max(1, now + 1);
    now = query(1, 0, 3e5, a[i], a[i]);
    if (now < f[i])
      change(1, 0, 3e5, a[i], f[i]);
  }
  int Max = 0;
  for (int i = 1; i <= n; ++i)
    Max = max(f[i], Max);
  cout << Max << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 50;
int maxx[maxn << 2], a[maxn], f[maxn];
void change(int rt, int l, int r, int pos, int v) {
  if (l == r && l == pos) {
    maxx[l] = v;
    return;
  }
  int mid = (l + r) >> 1;
  if (pos > mid)
    change(rt << 1 | 1, mid + 1, r, pos, v);
  else
    change(rt << 1, l, mid, pos, v);
  maxx[rt] = max(maxx[rt << 1], maxx[rt << 1 | 1]);
}
int query(int rt, int l, int r, int x, int y) {
  if (x >= l && r <= y) {
    return maxx[rt];
  }
  int ans = 0;
  int mid = (l + r) >> 1;
  if (mid >= x)
    ans = max(ans, query(rt << 1, l, mid, x, y));
  if (mid < y)
    ans = max(ans, query(rt << 1 | 1, mid + 1, r, x, y));
  return ans;
}
int n, k;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  f[1] = 1;
  change(1, 0, 3e5, a[1], 1);
  for (int i = 2; i <= n; ++i) {
    int now = query(1, 0, 3e5, max(a[i] - k, 0), min(300000, a[i] + k));
    f[i] = max(1, now + 1);
    now = query(1, 0, 3e5, a[i], a[i]);
    if (now < f[i])
      change(1, 0, 3e5, a[i], f[i]);
  }
  int Max = 0;
  for (int i = 1; i <= n; ++i)
    Max = max(f[i], Max);
  cout << Max << endl;
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "stdio.h"

#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))

#define INF 300002

struct segtree {
  int l;
  int r;
  int max;
} tree[300010 * 4];

int dp[300010];
int seq[300010];

int modify(int i, int dis, int val) {
  if (tree[i].l == tree[i].r) {
    tree[i].max = val;
    return 0;
  }
  int mid = (tree[i].l + tree[i].r) >> 1;
  if (dis > mid)
    modify(2 * i + 1, dis, val);
  else
    modify(2 * i, dis, val);
  if (tree[i].max < val)
    tree[i].max = val;
  return 0;
}

int search(int i, int l, int r) {
  if (tree[i].l >= l && tree[i].r <= r)
    return tree[i].max;
  int m = (tree[i].l + tree[i].r) >> 1;
  int x = 0, y = 0;
  if (m >= l)
    x = search(i * 2, l, r);
  if (m < r)
    y = search(i * 2 + 1, l, r);
  return MAX(x, y);
}

int build(int i, int l, int r) {
  tree[i].l = l;
  tree[i].r = r;
  tree[i].max = 0;
  if (l == r)
    return 0;
  int mid = (l + r) >> 1;
  build(2 * i, l, mid);
  build(2 * i + 1, mid + 1, r);
  return 0;
}

int main() {
  int N, K;
  scanf("%d", &N);
  scanf("%d", &K);
  int max = 0, min = INF;
  for (int i = 0; i < N; i++) {
    scanf("%d", &seq[i]);
    max = MAX(seq[i], max);
    min = MIN(seq[i], min);
  }
  build(1, min, max);
  dp[0] = 1;
  modify(1, seq[0], dp[0]);
  int res = 0;
  for (int i = 1; i < N; i++) {
    dp[i] = search(1, seq[i] - K, seq[i] + K) + 1;
    modify(1, seq[i], dp[i]);
    if (dp[i] > res)
      res = dp[i];
  }
  printf("%d", res);
}

Error: TLE

Fixed submission:

#include "stdio.h"

#define MAX(a, b) (((a) > (b)) ? (a) : (b))

struct segtree {
  int l;
  int r;
  int max;
} tree[300010 * 4];

int dp[300010];
int seq[300010];

int modify(int i, int dis, int val) {
  if (tree[i].l == tree[i].r) {
    tree[i].max = val;
    return 0;
  }
  int mid = (tree[i].l + tree[i].r) >> 1;
  if (dis > mid)
    modify(2 * i + 1, dis, val);
  else
    modify(2 * i, dis, val);
  if (tree[i].max < val)
    tree[i].max = val;
  return 0;
}

int search(int i, int l, int r) {
  if (tree[i].l >= l && tree[i].r <= r)
    return tree[i].max;
  else if (tree[i].l > r || tree[i].r < l)
    return 0;
  else
    return MAX(search(2 * i, l, r), search(2 * i + 1, l, r));
}

int build(int i, int l, int r) {
  tree[i].l = l;
  tree[i].r = r;
  tree[i].max = 0;
  if (l == r)
    return 0;
  int mid = (l + r) >> 1;
  build(2 * i, l, mid);
  build(2 * i + 1, mid + 1, r);
  return 0;
}

int main() {
  int N, K;
  scanf("%d", &N);
  scanf("%d", &K);
  int m = 0;
  for (int i = 0; i < N; i++) {
    scanf("%d", &seq[i]);
    m = MAX(seq[i], m);
  }
  build(1, 0, m);
  dp[0] = 1;
  modify(1, seq[0], dp[0]);
  int res = 0;
  for (int i = 1; i < N; i++) {
    dp[i] = search(1, seq[i] - K, seq[i] + K) + 1;
    modify(1, seq[i], dp[i]);
    if (dp[i] > res)
      res = dp[i];
  }
  printf("%d", res);
}