Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define per(i, a, n) for (int i = (a); i > (n); i--)
typedef long long ll;
const int maxn = 3e5 + 5;
const int mod = 1e9 + 7;
using namespace std;

ll a[maxn], dp[maxn];

const int N = 300010;

struct TR {
  int l;
  int r;
  int sum;
  int maxv;
} tr[4 * N];
int w[N];
int n, m;

void pushup(int u) {
  tr[u].sum = tr[u << 1].sum + tr[u << 1 | 1].sum;
  tr[u].maxv = max(tr[u << 1].maxv, tr[u << 1 | 1].maxv);
}

void build(int u, int l, int r) {
  if (l == r)
    tr[u] = {l, r, w[r], w[r]};
  else {
    int mid = (l + r) >> 1;
    tr[u] = {l, r};
    build(u << 1, l, mid);
    build(u << 1 | 1, mid + 1, r);
    pushup(u);
  }
}

int query(int u, int l, int r) {
  if (tr[u].l >= l && tr[u].r <= r)
    return tr[u].sum;
  int mid = (tr[u].r + tr[u].l) >> 1;
  int sum = 0;
  if (l <= mid)
    sum += query(u << 1, l, r);
  if (r > mid)
    sum += query(u << 1 | 1, l, r);
  return sum;
}

void modify(int u, int x, int v) {
  if (tr[u].l == tr[u].r) {
    tr[u].sum =
        v; // 直接把一点的值进行修改而不需要像树状数组那样+一个值再-一个值
    tr[u].maxv = v;
  } else {
    int mid = (tr[u].l + tr[u].r) >> 1;
    if (x <= mid)
      modify(u << 1, x, v);
    else
      modify(u << 1 | 1, x, v);
    pushup(u);
  }
}

int qmax(int u, int l, int r) { // 查找一段区间中的最大值
  if (tr[u].l >= l && tr[u].r <= r)
    return tr[u].maxv;
  int mid = (tr[u].l + tr[u].r) >> 1;
  int Max = -1;
  if (l <= mid)
    Max = qmax(u << 1, l, r);
  if (r > mid)
    Max = max(Max, qmax(u << 1 | 1, l, r));

  return Max;
}

void solve() {
  ll n, k, ans = 1;
  cin >> n >> k;
  rep(i, 0, n) { cin >> a[i]; }
  build(1, 1, 300006);
  // dp[0]=1;
  rep(i, 0, n) {
    ll t = qmax(1, max(a[i] + 1ll - k, 1ll), min(a[i] + 1ll + k, 300006ll));
    // cout << i << ' ' << t << '\\n';
    dp[i] = t + 1;
    modify(1, a[i] + 1, dp[i]);
  }
  rep(i, 0, n) { ans = max(dp[i], ans); }
  cout << ans << '\\n';
}

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifdef DEBUG
  freopen("C:/Users/Fish_Brother/Desktop/in", "r", stdin);
  // freopen("C:/Users/Fish_Brother/Desktop/out", "w", stdout);
#endif

  // int t; cin >> t; while(t--)
  solve();

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define per(i, a, n) for (int i = (a); i > (n); i--)
typedef long long ll;
const int maxn = 2e5 + 5;
const int mod = 1e9 + 7;
using namespace std;

ll a[maxn];

void solve() {
  ll n, k;
  cin >> n >> k;
  rep(i, 0, n) { cin >> a[i]; }
  ll ans = -1;
  rep(i, 0, n) {
    if (n - i <= ans)
      break;
    ll res = 1;
    ll pos = i;
    rep(j, i + 1, n) {
      if (abs(a[pos] - a[j]) <= k) {
        pos = j;
        res++;
        ans = max(ans, res);
      }
    }
  }
  cout << ans << '\\n';
}

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifdef DEBUG
  freopen("C:/Users/Fish_Brother/Desktop/in", "r", stdin);
  // freopen("C:/Users/Fish_Brother/Desktop/out", "w", stdout);
#endif

  // int t; cin >> t; while(t--)
  solve();

  return 0;
}