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

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cassert>
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint> // int64_t, int*_t
#include <cstdio>  // printf
#include <deque>   // deque
#include <functional>
#include <iostream> // cout, endl, cin
#include <limits>
#include <list>
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define int_INF 2147483647
#define pint_INF 2000000000
#define ll_INF 9223372036854775807
#define MOD 1000000007
#define vi vector<int>
#define vvi vector<vector<int>>
#define vvll vector<vector<long long>>
#define vvc vector<vector<char>>
#define vll vector<long long>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define sysp system("PAUSE")
#define pno cout << "no" << endl
#define pyes cout << "yes" << endl
#define pYes cout << "Yes" << endl
#define pNo cout << "No" << endl
#define pNO cout << "NO" << endl
#define pYES cout << "YES" << endl
#define endl "\\n"
#define pi 3.14159265358979
using namespace std;
using ll = long long;
#define int ll

class Segtree { // inputは0-indexed
private:
  int N = -1;
  vector<int> data; // ゼロに近いほうが親
  function<int(int, int)> func;
  int e_id;

public:
  Segtree(int n_, function<int(int, int)> f, int e) {
    func = f;
    e_id = e;

    int N_ = 1;
    while (N_ < n_)
      N_ *= 2;
    N = N_;
    data.resize(2 * N - 1, e_id);
  }

  void update(int k, int a) {
    k += N - 1;
    data[k] =
        a; // １点更新 // add queryのときは data[k] += a;としなければならない
    while (k > 0) {
      k = (k - 1) / 2;
      data[k] = func(data[k * 2 + 1], data[k * 2 + 2]);
    }
  }

  int query_sub(int a, int b, int k, int l, int r) {
    // k:現在見ているノードの位置  [l,r):dat[k]が表している区間
    if (r <= a || b <= l) { // 範囲外なら考えない
      return 0;
    } else if (a <= l && r <= b) { // 範囲内なので自身の値を返す
      return data[k];
    } else { // 一部区間が被る時
      int vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
      int vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
      return func(vl, vr);
    }
  }
  int query(int a, int b) {
    if (b < a)
      swap(a, b);
    return query_sub(a, b + 1, 0, 0, N);
  }
};

signed main() {
  const int Mx = 3000010;
  int N, K;
  cin >> N >> K;
  auto fx = [](int x1, int x2) -> int { return max(x1, x2); };
  int e = 0;
  Segtree RMQ(Mx + 1, fx, e);
  vector<int> vec(N);
  rep(i, N) { cin >> vec[i]; }
  for (int i = 0; i < N; i++) {
    int left = max(ll(0), vec[i] - K);
    int right = min(Mx + 1, vec[i] + K);
    int M = RMQ.query(left, right);
    RMQ.update(vec[i], M + 1);
  }
  cout << RMQ.query(0, Mx + 1) << endl;
  sysp;
}

Error: 0
sh: 1: PAUSE: not found


Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cassert>
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint> // int64_t, int*_t
#include <cstdio>  // printf
#include <deque>   // deque
#include <functional>
#include <iostream> // cout, endl, cin
#include <limits>
#include <list>
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define int_INF 2147483647
#define pint_INF 2000000000
#define ll_INF 9223372036854775807
#define MOD 1000000007
#define vi vector<int>
#define vvi vector<vector<int>>
#define vvll vector<vector<long long>>
#define vvc vector<vector<char>>
#define vll vector<long long>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define sysp system("PAUSE")
#define pno cout << "no" << endl
#define pyes cout << "yes" << endl
#define pYes cout << "Yes" << endl
#define pNo cout << "No" << endl
#define pNO cout << "NO" << endl
#define pYES cout << "YES" << endl
#define endl "\\n"
#define pi 3.14159265358979
using namespace std;
using ll = long long;
#define int ll

class Segtree { // inputは0-indexed
private:
  int N = -1;
  vector<int> data; // ゼロに近いほうが親
  function<int(int, int)> func;
  int e_id;

public:
  Segtree(int n_, function<int(int, int)> f, int e) {
    func = f;
    e_id = e;

    int N_ = 1;
    while (N_ < n_)
      N_ *= 2;
    N = N_;
    data.resize(2 * N - 1, e_id);
  }

  void update(int k, int a) {
    k += N - 1;
    data[k] =
        a; // １点更新 // add queryのときは data[k] += a;としなければならない
    while (k > 0) {
      k = (k - 1) / 2;
      data[k] = func(data[k * 2 + 1], data[k * 2 + 2]);
    }
  }

  int query_sub(int a, int b, int k, int l, int r) {
    // k:現在見ているノードの位置  [l,r):dat[k]が表している区間
    if (r <= a || b <= l) { // 範囲外なら考えない
      return 0;
    } else if (a <= l && r <= b) { // 範囲内なので自身の値を返す
      return data[k];
    } else { // 一部区間が被る時
      int vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
      int vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
      return func(vl, vr);
    }
  }
  int query(int a, int b) {
    if (b < a)
      swap(a, b);
    return query_sub(a, b + 1, 0, 0, N);
  }
};

signed main() {
  int N, K;
  cin >> N >> K;
  auto fx = [](int x1, int x2) -> int { return max(x1, x2); };
  int e = 0;
  Segtree RMQ(N + 1, fx, e);
  vector<int> vec(N);
  rep(i, N) {
    cin >> vec[i];
    vec[i];
  }
  for (int i = 0; i < N; i++) {
    int left = max(ll(0), vec[i] - K);
    int right = min(N, vec[i] + K);
    int M = RMQ.query(left, right);
    RMQ.update(vec[i], M + 1);
  }
  cout << RMQ.query(0, N - 1) << endl;
  sysp;
}