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


// Problem : D - Flat Subsequence
// Contest : AtCoder - ACL Beginner Contest
// URL : https://atcoder.jp/contests/abl/tasks/abl_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define eb emplace_back
#define pb push_back
#define size(s) (int)s.size()
// #define int long long
#define INF 1000000000000000000
#define vi vector<int>
#define vs vector<string>
#define vv vector<vector<int>>
#define pii pair<int, int>
#define m_p(x, y) make_pair(x, y)
#define vp vector<pair<int, int>>
#define setbits(x) __builtin_popcountll(x)
#define f first
#define se second
#define inc(v, n, x) v.assign(n, x)
#define incd(v, n) v.resize(n)
#define iniz(n) memset(n, 0, sizeof(n))
#define inin(n) memset(n, -1, sizeof(n))
#define inimi(n) memset(n, 0xc0, sizeof(n))
#define inima(n) memset(n, 0x3f, sizeof(n))
#define all(v) (v).begin(), (v).end()

using namespace std;

template <typename T1, typename T2>
istream &operator>>(istream &is, vector<pair<T1, T2>> &v) {
  for (pair<T1, T2> &t : v)
    is >> t.f >> t.se;
  return is;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v)
    is >> t;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &t : v) {
    os << t << " ";
  }
  os << '\\n';
  return os;
}

const int N = 1200004;
double pi = acos(-1.0);
int md = 1e9 + 7;
int dx1[] = {0, 0, -1, 1};
int dy1[] = {1, -1, 0, 0};
// const int N=2e5;

template <class T> T abst(T a) { return a < 0 ? -a : a; }

int Tree[N];
void build_max(int node, int start, int end, int a[]) {
  if (start == end) {
    Tree[node] = a[start];
    return;
  }
  int mid = ((start + end) >> 1);
  build_max(2 * node + 1, start, mid, a);
  build_max(2 * node + 2, mid + 1, end, a);
  // According to the question,change from here
  Tree[node] = max(Tree[2 * node + 1], Tree[2 * node + 2]);
}
void update_max(int node, int start, int end, int i, int val) {
  if (start == end) {
    Tree[node] = val;
    return;
  }
  int mid = ((start + end) >> 1);
  if (i <= mid)
    update_max(2 * node + 1, start, mid, i, val);
  else
    update_max(2 * node + 2, mid + 1, end, i, val);
  // According to the question,change from here
  Tree[node] = max(Tree[2 * node + 1], Tree[2 * node + 2]);
}
int query_max(int node, int start, int end, int l, int r) {
  if (start > end || end < l || start > r)
    return 0; // 0 in case of sum , high value in case of min , low value in
              // case of max and soooo on
  if (start >= l && end <= r)
    return (Tree[node]);
  int mid = (start + end) / 2;
  int p1 = query_max(node * 2 + 1, start, mid, l, r);
  int p2 = query_max(node * 2 + 2, mid + 1, end, l, r);
  return max(p1, p2);
}

void solve() {
  int n, k;
  cin >> n >> k;
  int ans = 1;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    int l = max(0, x - k);
    int r = min(300000, x + k);
    int mx = query_max(0, 0, 300000, l, r);
    ans = max(ans, mx + 1);
    update_max(0, 0, 300000, x, mx + 1);
  }
  cout << ans;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}


Error: WA

Fixed submission:


// Problem : D - Flat Subsequence
// Contest : AtCoder - ACL Beginner Contest
// URL : https://atcoder.jp/contests/abl/tasks/abl_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define eb emplace_back
#define pb push_back
#define size(s) (int)s.size()
// #define int long long
#define INF 1000000000000000000
#define vi vector<int>
#define vs vector<string>
#define vv vector<vector<int>>
#define pii pair<int, int>
#define m_p(x, y) make_pair(x, y)
#define vp vector<pair<int, int>>
#define setbits(x) __builtin_popcountll(x)
#define f first
#define se second
#define inc(v, n, x) v.assign(n, x)
#define incd(v, n) v.resize(n)
#define iniz(n) memset(n, 0, sizeof(n))
#define inin(n) memset(n, -1, sizeof(n))
#define inimi(n) memset(n, 0xc0, sizeof(n))
#define inima(n) memset(n, 0x3f, sizeof(n))
#define all(v) (v).begin(), (v).end()

using namespace std;

template <typename T1, typename T2>
istream &operator>>(istream &is, vector<pair<T1, T2>> &v) {
  for (pair<T1, T2> &t : v)
    is >> t.f >> t.se;
  return is;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v)
    is >> t;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &t : v) {
    os << t << " ";
  }
  os << '\\n';
  return os;
}

const int N = 1200004;
double pi = acos(-1.0);
int md = 1e9 + 7;
int dx1[] = {0, 0, -1, 1};
int dy1[] = {1, -1, 0, 0};
// const int N=2e5;

template <class T> T abst(T a) { return a < 0 ? -a : a; }

int Tree[N];
void build_max(int node, int start, int end, int a[]) {
  if (start == end) {
    Tree[node] = a[start];
    return;
  }
  int mid = ((start + end) >> 1);
  build_max(2 * node + 1, start, mid, a);
  build_max(2 * node + 2, mid + 1, end, a);
  // According to the question,change from here
  Tree[node] = max(Tree[2 * node + 1], Tree[2 * node + 2]);
}
void update_max(int node, int start, int end, int i, int val) {
  if (start == end) {
    Tree[node] = val;
    return;
  }
  int mid = ((start + end) >> 1);
  if (i <= mid)
    update_max(2 * node + 1, start, mid, i, val);
  else
    update_max(2 * node + 2, mid + 1, end, i, val);
  // According to the question,change from here
  Tree[node] = max(Tree[2 * node + 1], Tree[2 * node + 2]);
}
int query_max(int node, int start, int end, int l, int r) {
  if (start > end || end < l || start > r)
    return 0; // 0 in case of sum , high value in case of min , low value in
              // case of max and soooo on
  if (start >= l && end <= r)
    return (Tree[node]);
  int mid = (start + end) / 2;
  int p1 = query_max(node * 2 + 1, start, mid, l, r);
  int p2 = query_max(node * 2 + 2, mid + 1, end, l, r);
  return max(p1, p2);
}

void solve() {
  int n, k;
  cin >> n >> k;
  int ans = 1;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    int l = max(0, x - k);
    int r = min(300000, x + k);
    int mx = query_max(1, 0, 300000, l, r);
    ans = max(ans, mx + 1);
    update_max(1, 0, 300000, x, mx + 1);
  }
  cout << ans;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}