Language: C++

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
#define umap unordered_map
#define size(a) int((a).size())
#define present(c, x) (c.find(x) != c.end()) 
#define printVerdict(verdict) cout << (verdict ? "Yes": "No") << '\n'
#define printDecimal(d) printf("%.9f\n", d)
#define inrange(val, start, end) (val >= start && val <= end)
#define var(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const ll inf = 0x3f3f3f3f;

template <class T1, class T2, class T3> 
void printTuple(tuple<T1, T2, T3> t) { cout << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << '\n'; }
template <class T1, class T2>
void printPair(pair<T1, T2> p) { cout << p.first << " " << p.second << '\n'; }
template <class T>
void printArray(vector<T> arr) { for (int i = 0; i<size(arr); i++) {cout << arr[i] << " ";} cout << '\n'; }

struct segTree {
	int sz;
	ll baseValue = -1e9; 
	vector<ll> tree;
	void init(ll n) {
		sz = 1;
		while (sz < n) sz *= 2;
		tree.assign(2*sz, baseValue);
	}
	
	ll query(ll a, ll b) {
		return max(a, b);
	}
	
	void set(int idx, ll val) {set(idx, val, 0, 0, sz);}
	void set(int idx, ll val, int curr, int left, int right) {
		if (right - left == 1) tree[curr] = val;
		else {
			int mid = left + (right - left)/2;
			if (idx < mid) set(idx, val, curr*2+1, left, mid);
			else set(idx, val, curr*2 + 2, mid, right);
			tree[curr] = query(tree[curr*2 + 1], tree[curr*2 + 2]);
		}
	}
	
	ll evaluate(int left, int right) {return evaluate(left, right, 0, 0, sz);}
	ll evaluate(int left, int right, int curr, int curr_left, int curr_right) {
		if (curr_left >= right || left >= curr_right) return baseValue;
		if (curr_left >= left && curr_right <= right) return tree[curr];
		int mid = curr_left + (curr_right - curr_left)/2;
		ll s1 = evaluate(left, right, curr*2+1, curr_left, mid), s2 = evaluate(left, right, curr*2+2, mid, curr_right);
		return query(s1, s2);
	}
};

int main() {
	std::ios_base::sync_with_stdio(false);
	cin.tie(0);
	int n,k, maxVal = -1; cin >> n >> k;
	vector<ll> a(n);
	for (int i = 0; i<n; i++) {
		cin >> a[i];
		maxVal = max(maxVal, a[i]);
	}
	segTree tree;
	tree.init(maxVal + 1);
	tree.set(a[0], 1);
	ll ret = 1;
	for (int i = 1; i<n; i++) {
		int best = tree.evaluate(max(a[i]-k, 0), min(a[i]+k+1, maxVal + 1));
		tree.set(a[i], best + 1);
		ret = max(ret, best + 1);
	}
	cout << ret << '\n';
}



Compiler output:
a.cc: In function 'int main()':
a.cc:65:29: error: no matching function for call to 'max(int&, __gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type&)'
   65 |                 maxVal = max(maxVal, a[i]);
      |                          ~~~^~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:65:29: note:   deduced conflicting types for parameter 'const _Tp' ('int' and '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'})
   65 |                 maxVal = max(maxVal, a[i]);
      |                          ~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:65:29: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   65 |                 maxVal = max(maxVal, a[i]);
      |                          ~~~^~~~~~~~~~~~~~
a.cc:72:45: error: no matching function for call to 'max(__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type, int)'
   72 |                 int best = tree.evaluate(max(a[i]-k, 0), min(a[i]+k+1, maxVal + 1));
      |                                          ~~~^~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:72:45: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   72 |                 int best = tree.evaluate(max(a[i]-k, 0), min(a[i]+k+1, maxVal + 1));
      |                                          ~~~^~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:72:45: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   72 |                 int best = tree.evaluate(max(a[i]-k, 0), min(a[i]+k+1, maxVal + 1));
      |                                          ~~~^~~~~~~~~~~
a.cc:72:61: error: no matching function for call to 'min(__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type, int)'
   72 |                 int best = tree.evaluate(max(a[i]-k, 0), min(a[i]+k+1, maxVal + 1));
      |                                                          ~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:72:61: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   72 |                 int best = tree.evaluate(max(a[i]-k, 0), min(a[i]+k+1, maxVal + 1));
      |                                                          ~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:72:61: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   72 |                 int best = tree.evaluate(max(a[i]-k, 0), min(a[i]+k+1, maxVal + 1));
      |                                                          ~~~^~~~~~~~~~~~~~~~~~~~~~
a.cc:74:26: error: no matching function for call to 'max(ll&, int)'
   74 |                 ret = max(ret, best + 1);
      |                       ~~~^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:74:26: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   74 |                 ret = max(ret, best + 1);
      |                       ~~~^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:74:26: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   74 |                 ret = max(ret, best + 1);
      |                       ~~~^~~~~~~~~~~~~~~