This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
struct SegTree {
int n;
vector<int> tree;
SegTree(int _n) {
n = _n;
tree.resize(4*n+5);
}
void update(int u, int tl, int tr, int pos, int val) {
if(tl == tr) {
tree[u] = max(tree[u], val);
} else {
int tm = (tl + tr) / 2;
if(pos <= tm)
update(2*u, tl, tm, pos, val);
else
update(2*u+1, tm+1, tr, pos, val);
tree[u] = max(tree[2*u], tree[2*u+1]);
}
}
int query(int u, int tl, int tr, int l, int r) {
if(tl > tr || l > tr || tl > r) return 0;
if(l <= tl && tr <= r) return tree[u];
int tm = (tl + tr) / 2;
return max(
query(2*u, tl, tm, l, r),
query(2*u+1, tm+1, tr, l, r)
);
}
void update(int p, int v) { update(1, 0, n-1, p, v); }
int query(int l, int r) { return query(1, 0, n-1, l, r); }
void reset() { for(int i=0; i<tree.size(); i++) tree[i] = 0; }
};
int32_t main() {
int n, X, ans = 0;
cin >> n >> X;
set<int> s;
vector<int> v(n+1);
for(int i=1; i<=n; i++) {
cin >> v[i];
s.insert(v[i]); s.insert(v[i] - X);
}
vector<int> comp(all(s));
SegTree tree(2*n+5);
vector<int> L(n+1);
for(int i=1; i<=n; i++) {
int p = lower_bound(all(comp), v[i]) - comp.begin();
L[i] = tree.query(0, p-1) + 1;
tree.update(p, L[i]);
}
tree.reset();
for(int i=n; i>=1; i--) {
int p = lower_bound(all(comp), v[i] - X) - comp.begin();
int lis = tree.query(p+1, 2*n-1) + 1;
ans = max(ans, L[i] + lis - 1);
p = lower_bound(all(comp), v[i]) - comp.begin();
tree.update(p, tree.query(p+1, 2*n-1) + 1);
}
cout << ans << '\n';
return 0;
}
Compilation message (stderr)
glo.cpp: In member function 'void SegTree::reset()':
glo.cpp:39:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | void reset() { for(int i=0; i<tree.size(); i++) tree[i] = 0; }
| ~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |