#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=int(a);i<int(b);i++)
#define rrep(i,a,b) for(int i=int(a);i>int(b); i--);
#define all(v) v.begin(),v.end()
#define trav(a,v) for(auto&a:v)
using namespace std;
const long long inf = 1e15;
typedef long long ll;
struct MaxTree {
typedef ll T;
static constexpr T unit = -inf;
vector<T> s;
T f(T a, T b) {
return max(a, b);
}
ll n;
MaxTree(ll n = 0, T def = unit) : n(n), s(2 * n, def) {}
void update(ll pos, T val) {
for (s[pos += n] = val; pos /= 2;) {
s[pos] = f(s[2 * pos], s[2 * pos + 1]);
}
}
T query(ll l, ll r) {
T a = unit, b = unit;
if (l == r)return 0;
for (l += n, r += n; l < r; l /= 2, r /= 2) {
if (l % 2)a = f(a, s[l++]);
if (r % 2) b = f(b, s[--r]);
}
return f(a, b);
}
T getVal(ll pos) {
return s[pos + n];
}
};
int main() {
cin.sync_with_stdio(false);
ll n, x;
cin >> n >> x;
vector<vector<ll>> before(2 * n + 1);
vector<ll> v(n);
rep(i, 0, n) cin >> v[i];
ll ans = 0;
rep(i, 0, n)v.push_back(v[i] - x);
vector<ll> sorted = v;
sort(all(sorted));
map<ll, ll> maping;
rep(i, 0, 2 * n)maping[sorted[i]] = i;
vector<ll> lis(n);
MaxTree tree(2 * n);
rep(i, 0, 2 * n) {
tree.update(i, 0);
}
MaxTree tree2 = tree;
vector<ll> v2;
rep(i, n, 2 * n) v2.push_back(v[i]);
rep(i, 0, n) {
lis[i] = 1 + tree.query(0, maping[v2[i]]);
if (tree.getVal(maping[v2[i]])) before[maping[v2[i]]].push_back(tree.getVal(maping[v2[i]]));
tree.update(maping[v2[i]], lis[i]);
}
rep(i, 0, n) {
ll curval = 1 + tree2.query(maping[v[n - 1 - i]] + 1, 2 * n);
tree2.update(maping[v[n - 1 - i]], curval);
if (before[maping[v2[n - 1 - i]]].empty())tree.update(maping[v2[n - 1 - i]], 0);
else {
tree.update(maping[v2[n - 1 - i]], before[maping[v2[n - 1 - i]]].back());
before[maping[v2[n - 1 - i]]].pop_back();
}
ans = max(ans, curval + tree.query(0, maping[v[n - 1 - i]]));
}
cout << ans << endl;
return 0;
}
Compilation message
glo.cpp: In constructor 'MaxTree::MaxTree(ll, MaxTree::T)':
glo.cpp:20:5: warning: 'MaxTree::n' will be initialized after [-Wreorder]
20 | ll n;
| ^
glo.cpp:16:12: warning: 'std::vector<long long int> MaxTree::s' [-Wreorder]
16 | vector<T> s;
| ^
glo.cpp:21:2: warning: when initialized here [-Wreorder]
21 | MaxTree(ll n = 0, T def = unit) : n(n), s(2 * n, def) {}
| ^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
312 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
312 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
312 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
2 ms |
588 KB |
Output is correct |
20 |
Correct |
2 ms |
588 KB |
Output is correct |
21 |
Correct |
2 ms |
588 KB |
Output is correct |
22 |
Correct |
2 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
460 KB |
Output is correct |
24 |
Correct |
2 ms |
460 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
930 ms |
46324 KB |
Output is correct |
2 |
Correct |
932 ms |
46120 KB |
Output is correct |
3 |
Correct |
944 ms |
46248 KB |
Output is correct |
4 |
Correct |
935 ms |
46232 KB |
Output is correct |
5 |
Correct |
346 ms |
40076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
185 ms |
14868 KB |
Output is correct |
2 |
Correct |
184 ms |
14920 KB |
Output is correct |
3 |
Correct |
185 ms |
14992 KB |
Output is correct |
4 |
Correct |
82 ms |
11692 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
105 ms |
12324 KB |
Output is correct |
7 |
Correct |
128 ms |
14292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
495 ms |
29524 KB |
Output is correct |
2 |
Correct |
483 ms |
29524 KB |
Output is correct |
3 |
Correct |
1216 ms |
58748 KB |
Output is correct |
4 |
Correct |
406 ms |
46480 KB |
Output is correct |
5 |
Correct |
223 ms |
30132 KB |
Output is correct |
6 |
Correct |
435 ms |
57232 KB |
Output is correct |
7 |
Correct |
429 ms |
57780 KB |
Output is correct |
8 |
Correct |
307 ms |
30516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
312 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
2 ms |
588 KB |
Output is correct |
20 |
Correct |
2 ms |
588 KB |
Output is correct |
21 |
Correct |
2 ms |
588 KB |
Output is correct |
22 |
Correct |
2 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
460 KB |
Output is correct |
24 |
Correct |
2 ms |
460 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
2 ms |
460 KB |
Output is correct |
27 |
Correct |
930 ms |
46324 KB |
Output is correct |
28 |
Correct |
932 ms |
46120 KB |
Output is correct |
29 |
Correct |
944 ms |
46248 KB |
Output is correct |
30 |
Correct |
935 ms |
46232 KB |
Output is correct |
31 |
Correct |
346 ms |
40076 KB |
Output is correct |
32 |
Correct |
185 ms |
14868 KB |
Output is correct |
33 |
Correct |
184 ms |
14920 KB |
Output is correct |
34 |
Correct |
185 ms |
14992 KB |
Output is correct |
35 |
Correct |
82 ms |
11692 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
105 ms |
12324 KB |
Output is correct |
38 |
Correct |
128 ms |
14292 KB |
Output is correct |
39 |
Correct |
495 ms |
29524 KB |
Output is correct |
40 |
Correct |
483 ms |
29524 KB |
Output is correct |
41 |
Correct |
1216 ms |
58748 KB |
Output is correct |
42 |
Correct |
406 ms |
46480 KB |
Output is correct |
43 |
Correct |
223 ms |
30132 KB |
Output is correct |
44 |
Correct |
435 ms |
57232 KB |
Output is correct |
45 |
Correct |
429 ms |
57780 KB |
Output is correct |
46 |
Correct |
307 ms |
30516 KB |
Output is correct |
47 |
Correct |
464 ms |
30516 KB |
Output is correct |
48 |
Correct |
463 ms |
30488 KB |
Output is correct |
49 |
Correct |
1118 ms |
60660 KB |
Output is correct |
50 |
Correct |
384 ms |
47404 KB |
Output is correct |
51 |
Correct |
333 ms |
36168 KB |
Output is correct |
52 |
Correct |
452 ms |
47456 KB |
Output is correct |
53 |
Correct |
457 ms |
59948 KB |
Output is correct |
54 |
Correct |
453 ms |
60712 KB |
Output is correct |
55 |
Correct |
707 ms |
56488 KB |
Output is correct |