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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

set<int> S;

int A[300005], B[300005];
int tree[3 * 400005];

int get_max(int left, int right, int L, int R, int root) {
  if (left > right || left > R || right < L)
    return 0;

  if (left >= L && right <= R)
    return tree[root];

  int mid = (left + right) / 2;

  return max(get_max(left, mid, L, R, root * 2),
             get_max(mid + 1, right, L, R, root * 2 + 1));
}

void update(int left, int right, int x, int val, int root) {
  if (left > right || left > x || right < x)
    return;

  if (left == right && right == x) {
    tree[root] = val;
    return;
  }

  int mid = (left + right) / 2;

  update(left, mid, x, val, root * 2);
  update(mid + 1, right, x, val, root * 2 + 1);

  tree[root] = max(tree[root * 2], tree[root * 2 + 1]);
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int N, K;
  cin >> N >> K;

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    S.insert(A[i]);
  }

  int k = 1;

  for (set<int>::iterator it = S.begin(); it != S.end(); it++)
    B[k++] = *it;

  k--;
  int ans = 0;

  for (int i = 1; i <= N; i++) {
    int x = lower_bound(B + 1, B + 1 + k, A[i]) - B;
    int y = lower_bound(B + 1, B + 1 + k, A[i] - K) - B;
    int z = upper_bound(B + 1, B + 1 + k, A[i] + K) - B - 1;

    int new_ans = get_max(1, N, y, z, 1) + 1;
    ans = max(ans, new_ans);

    update(1, N, x, new_ans, 1);
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

set<int> S;

int A[100005], B[100005];
int tree[400005];

int get_max(int left, int right, int L, int R, int root) {
  if (left > right || left > R || right < L)
    return 0;

  if (left >= L && right <= R)
    return tree[root];

  int mid = (left + right) / 2;

  return max(get_max(left, mid, L, R, root * 2),
             get_max(mid + 1, right, L, R, root * 2 + 1));
}

void update(int left, int right, int x, int val, int root) {
  if (left > right || left > x || right < x)
    return;

  if (left == right && right == x) {
    tree[root] = val;
    return;
  }

  int mid = (left + right) / 2;

  update(left, mid, x, val, root * 2);
  update(mid + 1, right, x, val, root * 2 + 1);

  tree[root] = max(tree[root * 2], tree[root * 2 + 1]);
}

int main() {

  int N, K;
  cin >> N >> K;

  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    S.insert(A[i]);
  }

  int k = 1;

  for (set<int>::iterator it = S.begin(); it != S.end(); it++)
    B[k++] = *it;

  k--;
  int ans = 0;

  for (int i = 1; i <= N; i++) {
    int x = lower_bound(B + 1, B + 1 + k, A[i]) - B;
    int y = lower_bound(B + 1, B + 1 + k, A[i] - K) - B;
    int z = upper_bound(B + 1, B + 1 + k, A[i] + K) - B - 1;

    int new_ans = get_max(1, N, y, z, 1) + 1;
    ans = max(ans, new_ans);

    update(1, N, x, new_ans, 1);
  }

  cout << ans << endl;

  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
int n, k;
int a[300010];
int ans;
int max_a;
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
struct seg {
  int tree[2000010], lim;
  void init(int n) {
    for (lim = 1; lim <= n; lim <<= 1)
      ;
    for (int i = 0; i < 2 * lim; i++) {
      tree[i] = 0;
    }
  }
  void add(int x, int v) {
    x += lim;
    tree[x] = v;
    while (x > 1) {
      x >>= 1;
      tree[x] = max(tree[2 * x], tree[2 * x + 1]);
    }
  }
  int query(int s, int e) {
    s += lim;
    e += lim;
    int ret = 0;
    while (s < e) {
      if (s % 2 == 1)
        ret = max(ret, tree[s++]);
      if (e % 2 == 0)
        ret = max(ret, tree[e--]);
      s >>= 1;
      e >>= 1;
    }
    if (s == e)
      ret = max(ret, tree[s]);
    return ret;
  }
} seg;
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    max_a = max_a > a[i] ? max_a : a[i];
  }
  seg.init(max_a);
  for (int i = n - 1; i >= 0; i--) {
    int t = seg.query(max(0, a[i] - k), min(max_a, a[i] + k)) + 1;
    ans = max(ans, t);
    seg.add(a[i], t);
  }
  printf("%d\\n", ans);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <stdio.h>
int n, k;
int a[100010];
int DT[100010];
int ans;
int max_a;
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
struct seg {
  int tree[2000010], lim;
  void init(int n) {
    for (lim = 1; lim <= n; lim <<= 1)
      ;
    for (int i = 0; i < 2 * lim; i++) {
      tree[i] = 0;
    }
  }
  void add(int x, int v) {
    x += lim;
    tree[x] = v;
    while (x > 1) {
      x >>= 1;
      tree[x] = max(tree[2 * x], tree[2 * x + 1]);
    }
  }
  int query(int s, int e) {
    s += lim;
    e += lim;
    int ret = 0;
    while (s < e) {
      if (s % 2 == 1)
        ret = max(ret, tree[s++]);
      if (e % 2 == 0)
        ret = max(ret, tree[e--]);
      s >>= 1;
      e >>= 1;
    }
    if (s == e)
      ret = max(ret, tree[s]);
    return ret;
  }
} seg;
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    max_a = max_a > a[i] ? max_a : a[i];
  }
  seg.init(max_a);
  for (int i = n - 1; i >= 0; i--) {
    int t = seg.query(max(0, a[i] - k), min(max_a, a[i] + k)) + 1;
    ans = max(ans, t);
    seg.add(a[i], t);
  }
  printf("%d\\n", ans);
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
ll mod2 = 998244353;
ll mod = 1e9 + 7;
vector<int> seg;
vector<int> dp;
void update(int v, int tl, int tr, int pos, int val) {
  if (tl == tr)
    seg[v] = val;
  else {
    int mid = (tl + tr) / 2;
    if (pos <= mid)
      update(2 * v, tl, mid, pos, val);
    else {
      update(2 * v + 1, mid + 1, tr, pos, val);
    }
    seg[v] = max(seg[2 * v], seg[2 * v + 1]);
  }
}
int get(int v, int tl, int tr, int l, int r) {
  if (tl > r || tr < l)
    return 0;
  if (tl >= l && tr <= r)
    return seg[v];
  int mid = (tl + tr) / 2;
  return max(get(2 * v, tl, mid, l, r), get(2 * v + 1, mid + 1, tr, l, r));
}
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int p = 3e5;
  seg.resize(4 * p, 0);
  int ans = 1;
  for (int i = 1; i <= n; i++) {
    int val = get(1, 1, p, max(0, a[i] - k), min(p, a[i] + k));
    update(1, 1, p, a[i], val + 1);
    ans = max(ans, val + 1);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
ll mod2 = 998244353;
ll mod = 1e9 + 7;
vector<int> seg;
vector<int> dp;
void update(int v, int tl, int tr, int pos, int val) {
  if (tl == tr)
    seg[v] = val;
  else {
    int mid = (tl + tr) / 2;
    if (pos <= mid)
      update(2 * v, tl, mid, pos, val);
    else {
      update(2 * v + 1, mid + 1, tr, pos, val);
    }
    seg[v] = max(seg[2 * v], seg[2 * v + 1]);
  }
}
int get(int v, int tl, int tr, int l, int r) {
  if (tl > r || tr < l)
    return 0;
  if (tl >= l && tr <= r)
    return seg[v];
  int mid = (tl + tr) / 2;
  return max(get(2 * v, tl, mid, l, r), get(2 * v + 1, mid + 1, tr, l, r));
}
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int p = *max_element(begin(a), end(a));
  seg.resize(p + 6, 0);
  int ans = 1;
  for (int i = 1; i <= n; i++) {
    int val = get(1, 1, p, max(0, a[i] - k), min(p, a[i] + k));
    update(1, 1, p, a[i], val + 1);
    ans = max(ans, val + 1);
  }
  cout << ans << endl;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ls ch[x][0]
#define rs ch[x][1]
#define ll long long
using namespace std;

inline ll read() {
  ll num = 0, neg = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      neg = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    num = (num << 3) + (num << 1) + c - '0';
    c = getchar();
  }
  return num * neg;
}

int n, k, tree[300010 << 2], a[300010], f[300010];

inline void push_up(int x) { tree[x] = max(tree[x << 1], tree[x << 1 | 1]); }
inline void update(int u, int l, int r, int x, int k) {
  if (l == r) {
    tree[x] = max(tree[x], k);
    return;
  }
  int mid = l + r >> 1;
  if (u <= mid)
    update(u, l, mid, x << 1, k);
  else
    update(u, mid + 1, r, x << 1 | 1, k);
  push_up(x);
  return;
}
inline int query(int ql, int qr, int l, int r, int x) {
  if (ql <= l && qr >= r)
    return tree[x];
  int mid = l + r >> 1, res = 0;
  if (ql <= mid)
    res = max(res, query(ql, qr, l, mid, x << 1));
  if (qr > mid)
    res = max(res, query(ql, qr, mid + 1, r, x << 1 | 1));
  return res;
}

int main() {
  n = read(), k = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = 1; i <= n; i++) {
    f[i] = query(max(a[i] - k, 0), min(300000, a[i] + k), 1, 300000, 1) + 1;
    update(a[i], 1, 300000, 1, f[i]);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(f[i], ans);
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ls ch[x][0]
#define rs ch[x][1]
#define ll long long
using namespace std;

inline ll read() {
  ll num = 0, neg = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      neg = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    num = (num << 3) + (num << 1) + c - '0';
    c = getchar();
  }
  return num * neg;
}

int n, k, tree[300010 << 2], a[300010], f[300010];

inline void push_up(int x) { tree[x] = max(tree[x << 1], tree[x << 1 | 1]); }
inline void update(int u, int l, int r, int x, int k) {
  if (l == r) {
    tree[x] = max(tree[x], k);
    return;
  }
  int mid = l + r >> 1;
  if (u <= mid)
    update(u, l, mid, x << 1, k);
  else
    update(u, mid + 1, r, x << 1 | 1, k);
  push_up(x);
  return;
}
inline int query(int ql, int qr, int l, int r, int x) {
  if (ql <= l && qr >= r)
    return tree[x];
  int mid = l + r >> 1, res = 0;
  if (ql <= mid)
    res = max(res, query(ql, qr, l, mid, x << 1));
  if (qr > mid)
    res = max(res, query(ql, qr, mid + 1, r, x << 1 | 1));
  return res;
}

int main() {
  n = read(), k = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = 1; i <= n; i++) {
    f[i] = query(max(a[i] - k, 0), min(300000, a[i] + k), 1, n, 1) + 1;
    update(a[i], 1, n, 1, f[i]);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(f[i], ans);
  cout << ans;
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
using namespace std;

const int maxn = 6e5 + 10;
int a[maxn];
int m[maxn << 2];
int query(int rt, int l, int r, int ll, int rr) {
  if (ll >= l && rr <= r) {
    return m[rt];
  }
  int mid = ll + rr >> 1;
  int ans = 0;
  if (r <= mid)
    ans = max(ans, query(rt * 2, l, r, ll, mid));
  else if (l > mid)
    ans = max(ans, query(rt * 2 + 1, l, r, mid + 1, rr));
  else
    ans = max(ans, max(query(rt * 2, l, r, ll, mid),
                       query(rt * 2 + 1, l, r, mid + 1, rr)));
  return ans;
}
void update(int rt, int l, int r, int v, int ll, int rr) {
  if (l == ll && r == rr) {
    m[rt] = v;
    return;
  }
  int mid = ll + rr >> 1;
  if (l <= mid)
    update(rt << 1, l, r, v, ll, mid);
  else
    update(rt * 2 + 1, l, r, v, mid + 1, rr);
  m[rt] = max(m[rt << 1], m[rt * 2 + 1]);
}
void solved() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), a[i]++;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int res = query(1, max(a[i] - k, 0), a[i] + k, 1, maxn) + 1;
    update(1, a[i], a[i], res, 1, maxn);
    ans = max(ans, res);
  }
  cout << ans << endl;
}
int main() {
  solved();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
using namespace std;

const int maxn = 5e5 + 10;
int dp[maxn];
int a[maxn];
void solved() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) {
    int m = 0;
    for (int j = 1; j < i; j++) {
      if (abs(a[j] - a[i]) <= k) {
        m = max(m, dp[j]);
      }
    }
    dp[i] = m + 1;
  }
  cout << dp[n] << endl;
}
int main() {
  solved();
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct segmentTree {
  int n;
  vector<ll> t;
  segmentTree(int N) {
    n = N;
    t.resize(2 * N, 0);
  }

  void build() {
    for (int i = 0; i < n; i++)
      cin >> t[i + n];
    for (int i = n - 1; i > 0; i--)
      t[i] = max(t[2 * i], t[2 * i + 1]);
  }
  void update(int pos, int val) {
    pos += n;
    t[pos] = val;
    for (; pos > 1; pos /= 2)
      t[pos / 2] = max(t[pos], t[pos ^ 1]);
  }
  ll query(int l, int r) { // interval on [l,r]
    r++;
    ll res = 0;
    for (l += n, r += n; l < r; l /= 2, r /= 2) {
      if (l & 1)
        res = max(res, 1ll * t[l++]);
      if (r & 1)
        res = max(res, 1ll * t[--r]);
    }
    return res;
  }
};

void solve() {
  int mx = 3e5 + 1;
  int n, k;
  cin >> n >> k;
  segmentTree temp(mx);
  for (int i = 0; i < n; i++) {
    int cur;
    cin >> cur;
    int wow = temp.query(max(0, cur - k), min(cur + k, mx)) + 1;
    temp.update(cur, wow);
  }
  cout << temp.query(0, mx);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // int t; cin >> t;
  // while (t--)
  solve();
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5 + 1000;
int n, k;
vector<int> t;

void build() {
  for (int i = n - 1; i > 0; i--)
    t[i] = max(t[2 * i], t[2 * i + 1]);
}
void update(int pos, int val) {
  pos += n;
  t[pos] = val;
  for (; pos > 1; pos /= 2)
    t[pos / 2] = max(t[pos], t[pos ^ 1]);
}
int query(int l, int r) { // interval on [l,r]
  r++;
  l = max(0, l);
  r = min(N, r);
  int res = 0;
  for (l += n, r += n; l < r; l /= 2, r /= 2) {
    if (l & 1)
      res = max(res, t[l++]);
    if (r & 1)
      res = max(res, t[--r]);
  }
  return res;
}

void solve() {
  cin >> n >> k;
  int ans = 0;
  t.resize(N, 0);
  for (int i = 0; i < n; i++) {
    int cur;
    cin >> cur;
    int mx = query(cur - k, cur + k);
    update(cur, mx + 1);
  }
  for (int i = 0; i < N; i++)
    ans = max(ans, t[i]);
  cout << ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // int t; cin >> t;
  // while (t--)
  solve();
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;

int n, k, a[N], f[N << 2];
void pushup(int o) { f[o] = max(f[o << 1], f[o << 1 | 1]); }
int ask(int l, int r, int o, int ld, int rd) {
  if (l == r)
    return f[o];
  if (ld <= l && rd >= r)
    return f[o];
  int mid = l + r >> 1, ans = 0;
  if (rd > mid)
    ans = max(ans, ask(mid + 1, r, o << 1 | 1, ld, rd));
  if (ld <= mid)
    ans = max(ans, ask(l, mid, o << 1, ld, rd));
  return ans;
}
void modify(int l, int r, int o, int x, int y) {
  if (l == r) {
    f[o] = y;
    return;
  }
  int mid = l + r >> 1;
  if (x <= mid)
    modify(l, mid, o << 1, x, y);
  else
    modify(mid + 1, r, o << 1 | 1, x, y);
  pushup(o);
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), a[i]++;
  for (int i = 1; i <= n; i++) {
    int x = ask(1, 300001, 1, max(1, a[i] - k), min(300001, a[i] + k)) + 1;
    modify(1, 300001, 1, a[i], x);
  }
  printf("%d\\n", f[1]);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;

int n, k, a[N], f[N << 2];
void pushup(int o) { f[o] = max(f[o << 1], f[o << 1 | 1]); }
int ask(int l, int r, int o, int ld, int rd) {
  if (l == r)
    return f[o];
  int mid = l + r >> 1, ans = 0;
  if (rd > mid)
    ans = max(ans, ask(mid + 1, r, o << 1 | 1, ld, rd));
  if (ld <= mid)
    ans = max(ans, ask(l, mid, o << 1, ld, rd));
  return ans;
}
void modify(int l, int r, int o, int x, int y) {
  if (l == r) {
    f[o] = y;
    return;
  }
  int mid = l + r >> 1;
  if (x <= mid)
    modify(l, mid, o << 1, x, y);
  else
    modify(mid + 1, r, o << 1 | 1, x, y);
  pushup(o);
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), a[i]++;
  for (int i = 1; i <= n; i++) {
    int x = ask(1, 300001, 1, max(1, a[i] - k), min(300001, a[i] + k)) + 1;
    modify(1, 300001, 1, a[i], x);
  }
  printf("%d\\n", f[1]);
}
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define watch(x) std::cout << (#x) << " is " << (x) << std::endl
#define print(x) std::cout << (x) << std::endl
#define println std::cout << std::endl;
using LL = long long;
#define lrt rt << 1
#define rrt rt << 1 | 1
#define lson l, m, lrt
#define rson m + 1, r, rrt
const int N = 3e5 + 2;
int mx[N * 5];

void update(int p, int val, int l, int r, int rt) {
  mx[rt] = std::max(mx[rt], val);
  if (l == r)
    return;
  int m = (l + r) >> 1;
  if (p > m)
    update(p, val, rson);
  else
    update(p, val, lson);
}
int query(int L, int R, int l, int r, int rt) {
  if (L <= l && R >= r)
    return mx[rt];
  int m = (l + r) >> 1;
  int ans = 0;
  if (L <= m)
    ans = std::max(ans, query(L, R, lson));
  if (R > m)
    ans = std::max(ans, query(L, R, rson));
  return ans;
}
int main() {
  // freopen("in","r",stdin);
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n, k;
  std::cin >> n >> k;
  for (int i = 0, x; i < n; ++i) {
    std::cin >> x;
    ++x;
    int now = query(x - k, x + k, 1, N, 1);
    update(x, now + 1, 1, N, 1);
  }
  print(mx[1]);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define watch(x) std::cout << (#x) << " is " << (x) << std::endl
#define print(x) std::cout << (x) << std::endl
#define println std::cout << std::endl;
using LL = long long;
#define lrt rt << 1
#define rrt rt << 1 | 1
#define lson l, m, lrt
#define rson m + 1, r, rrt
const int N = 3e5 + 2;
int mx[N * 3];

void update(int p, int val, int l, int r, int rt) {
  mx[rt] = std::max(mx[rt], val);
  if (l == r)
    return;
  int m = (l + r) >> 1;
  if (p > m)
    update(p, val, rson);
  else
    update(p, val, lson);
}
int query(int L, int R, int l, int r, int rt) {
  if (L <= l && R >= r)
    return mx[rt];
  int m = (l + r) >> 1;
  int ans = 0;
  if (L <= m)
    ans = std::max(ans, query(L, R, lson));
  if (R > m)
    ans = std::max(ans, query(L, R, rson));
  return ans;
}
int main() {
  // freopen("in","r",stdin);
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n, k;
  std::cin >> n >> k;
  for (int i = 0, x; i < n; ++i) {
    std::cin >> x;
    ++x;
    int now = query(x - k, x + k, 1, N, 1);
    update(x, now + 1, 1, N, 1);
  }
  print(mx[1]);
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
struct segt {
  int l, r, maxv;
} tree[2400010];
int a[300010], dp[300010];
void build(int le, int ri, int i) {
  tree[i].l = le;
  tree[i].r = ri;
  tree[i].maxv = 0;
  if (le == ri)
    return;
  int mid = (le + ri) >> 1;
  build(le, mid, i << 1);
  build(mid + 1, ri, i << 1 | 1);
}
void change(int p, int x, int i) {
  if (tree[i].l == tree[i].r) {
    tree[i].maxv = max(tree[i].maxv, x);
    return;
  }
  if (tree[i << 1].r >= p)
    change(p, x, i << 1);
  else
    change(p, x, i << 1 | 1);
  tree[i].maxv = max(tree[i << 1].maxv, tree[i << 1 | 1].maxv);
}
int ask(int le, int ri, int i) {
  if (le <= tree[i].l && tree[i].r <= ri)
    return tree[i].maxv;
  int cnt = 0;
  if (tree[i << 1].r >= le)
    cnt = ask(le, ri, i << 1);
  if (tree[i << 1 | 1].l <= ri)
    cnt = max(cnt, ask(le, ri, i << 1 | 1));
  return cnt;
}
signed main() {
  int n, k, i, ans = 1;
  cin >> n >> k;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  build(1, 300002, 1);
  for (i = 1; i <= n; i++) {
    dp[i] = max(
        1ll, ask(max(0ll, a[i] - k) + 1, min(300000ll, a[i] + k) + 1, 1) + 1);
    change(a[i] + 1, dp[i], 1);
    ans = max(ans, dp[i]);
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int max_(int a, int b) { return a > b ? a : b; }
int abs(int x) {
  if (x < 0)
    return -x;
  return x;
}
int n, a[300005], k, dp[300005], _max[300005];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  dp[1] = 1;
  _max[1] = 1;
  for (int i = 2; i <= n; i++) {
    int s = a[i] - k < 0 ? 0 : a[i] - k;
    int e = a[i] + k > 300000 ? 300000 : a[i] + k;
    for (int j = s; j <= e; j++)
      dp[i] = max_(dp[i], _max[j] + 1);
    _max[a[i]] = max_(_max[a[i]], dp[i]);
  }
  printf("%d", dp[n]);
  return 0;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

#define INF (int)1E6

struct seg {
  int log, size;
  vector<int> v;
  seg(int n) {
    log = 0;
    while ((1 << log) < n) {
      log++;
    }
    size = (1 << log);
    v = vector<int>(2 * size, 0);
  }

  void set(int p, int x) {
    p += size;
    v[p] = x;
    for (int i = 1; i <= log; i++) {
      int parent = p >> i;
      v[parent] = max(v[2 * parent], v[2 * parent + 1]);
    }
  }

  int query(int l, int r) {
    int sml = 0, smr = 0;
    l += size;
    r += size;
    while (l < r) {
      if (l & 1)
        sml = max(sml, v[l++]);
      if (r & 1)
        smr = max(v[--r], smr);
      l >>= 1;
      r >>= 1;
    }
    return max(sml, smr);
  }

  int get(int p) { return v[p + size]; }
};

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  seg s(300010);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int l = max(0, a[i] - k);
    int r = min(300000, a[i] + k);
    int tmp = s.query(l, r + 1) + 1;
    s.set(a[i], tmp);
    ans = max(ans, tmp);
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

#define INF (int)1E6

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  // a, maxlength
  vector<pair<int, int>> al;
  for (int i = 0; i < n; i++) {
    if (al.size() == 0) {
      al.emplace_back(make_pair(a[i], 1));
    } else {
      sort(al.begin(), al.end());
      auto minIter =
          lower_bound(al.begin(), al.end(), make_pair(a[i] - k, -INF));
      auto maxIter =
          upper_bound(al.begin(), al.end(), make_pair(a[i] + k, INF));
      // 該当なし
      if (maxIter - minIter <= 0) {
        al.emplace_back(make_pair(a[i], 1));
      } else {
        int minindex = minIter - al.begin();
        sort(minIter, maxIter,
             [](const auto &l, const auto &r) { return l.second > r.second; });
        al.emplace_back(make_pair(a[i], 1 + al[minindex].second));
      }
    }
  }

  sort(al.begin(), al.end(),
       [](auto const &l, auto const &r) { return l.second > r.second; });
  cout << al[0].second << endl;
}