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

#include <bits/stdc++.h>
#define Mashu cout << "UUZ ate it." << endl
#define RE register int
#define LL int
using namespace std;

inline int read() {
  char ch = getchar();
  int x = 0, cf = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      cf = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = getchar();
  }
  if (cf < 0)
    return -x;
  else
    return x;
}

LL /*tag[400001], */ num[1200001];

LL lc(LL p) { return p << 1; }

LL rc(LL p) { return p << 1 | 1; }

void pushup(LL p) { num[p] = max(num[lc(p)], num[rc(p)]); }

// void build(LL p, LL l, LL r){
//	if (l == r){
//		num[p] = a[l];
//		return;
//	}
//	LL mid = (l + r) >> 1;
//	build(lc(p), l, mid);
//	build(rc(p), mid + 1, r);
//	pushup(p);
// }

// void upd(LL p, LL l, LL r, LL x){
//	tag[p] += x;
//	num[p] += (r - l + 1) * x;
// }

// void pushdown(LL p, LL l, LL r){
//	LL mid = (l + r) >> 1;
//	upd(lc(p), l, mid, tag[p]);
//	upd(rc(p), mid + 1, r, tag[p]);
//	tag[p] = 0;
// }

void change(LL L, LL R, LL l, LL r, LL p, LL x) {
  if (L <= l && r <= R) {
    //		tag[p] += x;
    //		num[p] += (r - l + 1) * x;
    num[p] = x;
    return;
  }
  //	pushdown(p, l, r);
  LL mid = (l + r) >> 1;
  if (L <= mid)
    change(L, R, l, mid, lc(p), x);
  if (R >= mid + 1)
    change(L, R, mid + 1, r, rc(p), x);
  pushup(p);
}

LL Q(LL L, LL R, LL l, LL r, LL p) {
  if (L <= l && r <= R) {
    return num[p];
  }
  LL ans = -1;
  //	pushdown(p, l, r);
  LL mid = (l + r) >> 1;
  if (L <= mid)
    ans = max(ans, Q(L, R, l, mid, lc(p)));
  if (R >= mid + 1)
    ans = max(ans, Q(L, R, mid + 1, r, rc(p)));
  return ans;
}

int qwq[300010];

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> qwq[i];
  }
  int ans = 1;
  for (int i = 1; i <= n; i++) {
    int l = max(0, qwq[i] - k);
    int r = min(300000, qwq[i] + k);
    int qaq = Q(l, r, 0, 300000, 1) + 1;
    ans = max(ans, qaq);
    change(qwq[i], qwq[i], 0, 300000, 1, qaq);
  }
  cout << ans << endl;
  return 0;
  Mashu;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define Mashu cout << "UUZ ate it." << endl
#define RE register int
#define LL int
using namespace std;

inline int read() {
  char ch = getchar();
  int x = 0, cf = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      cf = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = getchar();
  }
  if (cf < 0)
    return -x;
  else
    return x;
}

LL /*tag[400001], */ num[1200001];

LL lc(LL p) { return p << 1; }

LL rc(LL p) { return p << 1 | 1; }

void pushup(LL p) { num[p] = max(num[lc(p)], num[rc(p)]); }

// void build(LL p, LL l, LL r){
//	if (l == r){
//		num[p] = a[l];
//		return;
//	}
//	LL mid = (l + r) >> 1;
//	build(lc(p), l, mid);
//	build(rc(p), mid + 1, r);
//	pushup(p);
// }

// void upd(LL p, LL l, LL r, LL x){
//	tag[p] += x;
//	num[p] += (r - l + 1) * x;
// }

// void pushdown(LL p, LL l, LL r){
//	LL mid = (l + r) >> 1;
//	upd(lc(p), l, mid, tag[p]);
//	upd(rc(p), mid + 1, r, tag[p]);
//	tag[p] = 0;
// }

void change(LL L, LL R, LL l, LL r, LL p, LL x) {
  if (L <= l && r <= R) {
    //		tag[p] += x;
    //		num[p] += (r - l + 1) * x;
    num[p] = x;
    return;
  }
  //	pushdown(p, l, r);
  LL mid = (l + r) >> 1;
  if (L <= mid)
    change(L, R, l, mid, lc(p), x);
  if (R >= mid + 1)
    change(L, R, mid + 1, r, rc(p), x);
  pushup(p);
}

LL Q(LL L, LL R, LL l, LL r, LL p) {
  if (L <= l && r <= R) {
    return num[p];
  }
  LL ans = -1;
  //	pushdown(p, l, r);
  LL mid = (l + r) >> 1;
  if (L <= mid)
    ans = max(ans, Q(L, R, l, mid, lc(p)));
  if (R >= mid + 1)
    ans = max(ans, Q(L, R, mid + 1, r, rc(p)));
  return ans;
}

int qwq[300010];

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> qwq[i];
  }
  int ans = 1;
  for (int i = 1; i <= n; i++) {
    int l = max(1, qwq[i] - k);
    int r = min(300000, qwq[i] + k);
    int qaq = Q(l, r, 1, 300000, 1) + 1;
    ans = max(ans, qaq);
    change(qwq[i], qwq[i], 1, 300000, 1, qaq);
  }
  cout << ans << endl;
  return 0;
  Mashu;
}