#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 10;
const int INF = 1e9 + 7;
struct segtree{
int st[MX << 2], lz[MX << 2];
void build(int id, int l, int r){
if(l == r){
st[id] = -INF;
lz[id] = 0;
}else{
int mid = l + r >> 1;
build(id << 1, l, mid);
build(id << 1|1, mid + 1, r);
st[id] = max(st[id << 1], st[id << 1|1]);
}
}
void prop(int id, int l, int r){
if(lz[id] == 0) return;
st[id] += lz[id];
if(l != r){
lz[id << 1] += lz[id];
lz[id << 1|1] += lz[id];
}
lz[id] = 0;
}
void upd_pos(int id, int l, int r, int p, int v){
prop(id, l, r);
if(r < p || l > p) return;
if(l == p && p == r){
st[id] = v;
}else{
int mid = l + r >> 1;
upd_pos(id << 1, l, mid, p, v);
upd_pos(id << 1|1, mid + 1, r, p, v);
st[id] = max(st[id << 1], st[id << 1|1]);
}
}
void upd(int id, int l, int r, int from, int to, int v){
prop(id, l, r);
if(r < from || l > to) return;
if(from <= l && r <= to){
lz[id] += v;
prop(id, l, r);
}else{
int mid = l + r >> 1;
upd(id << 1, l, mid, from, to, v);
upd(id << 1|1, mid + 1, r, from, to, v);
st[id] = max(st[id << 1], st[id << 1|1]);
}
}
int que(int id, int l, int r, int from, int to){
prop(id, l, r);
if(r < from || l > to) return -INF;
if(from <= l && r <= to){
return st[id];
}else{
int mid = l + r >> 1;
return max(
que(id << 1, l, mid, from, to),
que(id << 1|1, mid + 1, r, from, to)
);
}
}
} S, T;
void chmax(int& a, int b){
if(b > a) a = b;
}
vector<int> cc;
int N, X, arr[MX], pref[MX], suff[MX];
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N >> X;
for(int i = 0; i < N; i++){
cin >> arr[i];
cc.push_back(arr[i]);
}
cc.push_back(0);
sort(cc.begin(), cc.end());
cc.resize(unique(cc.begin(), cc.end()) - cc.begin());
S.build(1, 0, N);
S.upd_pos(1, 0, N, 0, 0);
for(int i = 0; i < N; i++) arr[i] = lower_bound(cc.begin(), cc.end(), arr[i]) - cc.begin();
int ans = 0;
for(int i = 0; i < N; i++){
pref[i] = S.que(1, 0, N, 0, arr[i] - 1) + 1;
S.upd_pos(1, 0, N, arr[i], pref[i]);
chmax(ans, pref[i]);
}
T.build(1, 1, N + 1);
T.upd_pos(1, 1, N + 1, N + 1, 0);
for(int i = N - 1; i >= 0; i--){
suff[i] = T.que(1, 1, N + 1, arr[i] + 1, N + 1) + 1;
T.upd_pos(1, 1, N + 1, arr[i], suff[i]);
if(i > 0){
int curr = cc[arr[i - 1]] - X + 1;
int idx = lower_bound(cc.begin(), cc.end(), curr) - cc.begin();
chmax(ans, pref[i - 1] + T.que(1, 1, N + 1, idx, N + 1));
}
chmax(ans, suff[i]);
}
cout << ans << '\n';
}
Compilation message
glo.cpp: In member function 'void segtree::build(int, int, int)':
glo.cpp:15:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
15 | int mid = l + r >> 1;
| ~~^~~
glo.cpp: In member function 'void segtree::upd_pos(int, int, int, int, int)':
glo.cpp:38:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
38 | int mid = l + r >> 1;
| ~~^~~
glo.cpp: In member function 'void segtree::upd(int, int, int, int, int, int)':
glo.cpp:52:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
52 | int mid = l + r >> 1;
| ~~^~~
glo.cpp: In member function 'int segtree::que(int, int, int, int, int)':
glo.cpp:65:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
65 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
294 ms |
10772 KB |
Output is correct |
2 |
Correct |
272 ms |
12616 KB |
Output is correct |
3 |
Correct |
292 ms |
12764 KB |
Output is correct |
4 |
Correct |
337 ms |
12616 KB |
Output is correct |
5 |
Correct |
157 ms |
11940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
2984 KB |
Output is correct |
2 |
Correct |
60 ms |
3544 KB |
Output is correct |
3 |
Correct |
59 ms |
3520 KB |
Output is correct |
4 |
Correct |
34 ms |
3304 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
42 ms |
3280 KB |
Output is correct |
7 |
Correct |
52 ms |
3484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
111 ms |
5636 KB |
Output is correct |
2 |
Correct |
113 ms |
5584 KB |
Output is correct |
3 |
Correct |
231 ms |
10696 KB |
Output is correct |
4 |
Correct |
127 ms |
10764 KB |
Output is correct |
5 |
Correct |
74 ms |
5596 KB |
Output is correct |
6 |
Correct |
129 ms |
10548 KB |
Output is correct |
7 |
Correct |
131 ms |
10564 KB |
Output is correct |
8 |
Correct |
86 ms |
5548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
294 ms |
10772 KB |
Output is correct |
28 |
Correct |
272 ms |
12616 KB |
Output is correct |
29 |
Correct |
292 ms |
12764 KB |
Output is correct |
30 |
Correct |
337 ms |
12616 KB |
Output is correct |
31 |
Correct |
157 ms |
11940 KB |
Output is correct |
32 |
Correct |
56 ms |
2984 KB |
Output is correct |
33 |
Correct |
60 ms |
3544 KB |
Output is correct |
34 |
Correct |
59 ms |
3520 KB |
Output is correct |
35 |
Correct |
34 ms |
3304 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
42 ms |
3280 KB |
Output is correct |
38 |
Correct |
52 ms |
3484 KB |
Output is correct |
39 |
Correct |
111 ms |
5636 KB |
Output is correct |
40 |
Correct |
113 ms |
5584 KB |
Output is correct |
41 |
Correct |
231 ms |
10696 KB |
Output is correct |
42 |
Correct |
127 ms |
10764 KB |
Output is correct |
43 |
Correct |
74 ms |
5596 KB |
Output is correct |
44 |
Correct |
129 ms |
10548 KB |
Output is correct |
45 |
Correct |
131 ms |
10564 KB |
Output is correct |
46 |
Correct |
86 ms |
5548 KB |
Output is correct |
47 |
Correct |
125 ms |
6604 KB |
Output is correct |
48 |
Correct |
137 ms |
6676 KB |
Output is correct |
49 |
Correct |
288 ms |
12808 KB |
Output is correct |
50 |
Correct |
150 ms |
11896 KB |
Output is correct |
51 |
Correct |
113 ms |
10784 KB |
Output is correct |
52 |
Correct |
189 ms |
11972 KB |
Output is correct |
53 |
Correct |
134 ms |
11952 KB |
Output is correct |
54 |
Correct |
141 ms |
12684 KB |
Output is correct |
55 |
Correct |
233 ms |
12744 KB |
Output is correct |