#include <bits/stdc++.h>
using namespace std ;
using ll = long long ;
struct SegTree {
int _n ;
struct Node {
ll value = 0 ;
Node() {}
Node(ll x) {
value = x ;
}
};
Node combine(Node l, Node r) {
if (l.value > r.value) return l ;
return r ;
}
vector<Node> t ;
SegTree(int len) {
_n = len ;
t.assign(len * 4 + 1, Node(0)) ;
}
void modify(int i, ll x, int v, int tl, int tr) {
if (tl == tr) {
t[v] = combine(x, t[v]) ;
} else {
int tm = (tl + tr) >> 1 ;
if (i <= tm)
modify(i, x, v << 1, tl, tm) ;
else
modify(i, x, v << 1 | 1, tm + 1, tr) ;
t[v] = combine(t[v * 2], t[v * 2 + 1]) ;
}
}
void modify(int i, ll x) {
modify(i, x, 1, 1, _n) ;
}
Node get(int l, int r, int v, int tl, int tr) {
if (l > tr || r < tl) return Node(0) ;
if (l <= tl && tr <= r) return t[v] ;
int tm = (tl + tr) >> 1 ;
Node L = get(l, r, v << 1, tl, tm) ;
Node R = get(l ,r, v << 1 | 1, tm + 1, tr) ;
return combine(L, R) ;
}
Node get(int l, int r) {
if (l > r) return Node(0) ;
return get(l, r, 1, 1, _n) ;
}
};
const int N = 6e5 + 5 ;
multiset<int> st[N * 4] ;
ll tree[N * 4] ;
void add(int i, int x, int v, int tl, int tr) {
if (tl == tr) {
st[tl].insert(x) ;
tree[v] = *st[tl].rbegin() ;
} else {
int tm = (tl + tr) / 2 ;
if (i <= tm) {
add(i, x, v * 2, tl, tm) ;
} else {
add(i, x, v * 2 + 1, tm + 1, tr) ;
}
tree[v] = max(tree[v * 2], tree[v * 2 + 1]) ;
}
}
void del(int i, int x, int v, int tl, int tr) {
if (tl == tr) {
st[tl].erase(st[tl].find(x)) ;
if (st[tl].size())
tree[v] = *st[tl].rbegin() ;
else
tree[v] = 0 ;
} else {
int tm = (tl + tr) / 2 ;
if (i <= tm) {
del(i, x, v * 2, tl, tm) ;
} else {
del(i, x, v * 2 + 1, tm + 1, tr) ;
}
tree[v] = max(tree[v * 2], tree[v * 2 + 1]) ;
}
}
int query(int l, int r, int v, int tl, int tr) {
if (l > tr || r< tl) return 0 ;
if (l <= tl && tr <= r) return tree[v] ;
int tm = (tl + tr) / 2 ;
return max(query(l, r, v * 2, tl, tm), query(l, r, v * 2 + 1, tm + 1, tr)) ;
}
int32_t main() {
ios::sync_with_stdio(false) ;
cin.tie(nullptr) ;
int n , x ;
cin >> n >> x ;
vector<int> a(n + 1), c ;
for (int i = 1 ; i <= n ; i++) {
cin >> a[i] ;
c.push_back(a[i]) ;
c.push_back(a[i] - 1) ;
c.push_back(a[i] + 1) ;
c.push_back(a[i] - x + 1) ;
c.push_back(a[i] + x + 1) ;
}
sort(c.begin(), c.end()) ;
c.erase(unique(c.begin(), c.end()), c.end()) ;
auto get = [&](int i) -> int {
return (lower_bound(c.begin(), c.end(), i) - c.begin() + 1) ;
};
int m = c.size() ;
SegTree s(m) ;
int ret = 0 ;
vector<int> pre(n + 1, 1), suf(n + 1, 1) ;
for (int i = 1 ; i <= n ; i++) {
pre[i] = max(pre[i] * 1ll, s.get(1, get(a[i] - 1)).value + 1) ;
s.modify(get(a[i]), pre[i]) ;
ret = max(ret, pre[i]) ;
}
if (!x) {
cout << ret ;
return 0 ;
}
s = SegTree(m) ;
for (int i = n ; i >= 1 ; i--) {
suf[i] = max(suf[i] * 1ll, s.get(get(a[i] + 1), m).value + 1) ;
s.modify(get(a[i]), suf[i]) ;
add(get(a[i]), suf[i], 1, 1, m) ;
}
for (int i = 1 ; i <= n ; i++) {
del(get(a[i]), suf[i], 1, 1, m) ;
int l = get(a[i] - x + 1) , r = get(a[i] + x + 1) ;
int value = query(l, r, 1, 1 ,m) ;
ret = max(ret, value + pre[i]) ;
ret = max(ret, pre[i]) ;
}
cout << ret ;
return 0 ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
113232 KB |
Output is correct |
2 |
Correct |
23 ms |
113204 KB |
Output is correct |
3 |
Correct |
22 ms |
113244 KB |
Output is correct |
4 |
Correct |
26 ms |
113244 KB |
Output is correct |
5 |
Correct |
22 ms |
113244 KB |
Output is correct |
6 |
Correct |
22 ms |
113496 KB |
Output is correct |
7 |
Correct |
22 ms |
113208 KB |
Output is correct |
8 |
Correct |
22 ms |
113244 KB |
Output is correct |
9 |
Correct |
22 ms |
113244 KB |
Output is correct |
10 |
Correct |
22 ms |
113240 KB |
Output is correct |
11 |
Correct |
23 ms |
113244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
113232 KB |
Output is correct |
2 |
Correct |
23 ms |
113204 KB |
Output is correct |
3 |
Correct |
22 ms |
113244 KB |
Output is correct |
4 |
Correct |
26 ms |
113244 KB |
Output is correct |
5 |
Correct |
22 ms |
113244 KB |
Output is correct |
6 |
Correct |
22 ms |
113496 KB |
Output is correct |
7 |
Correct |
22 ms |
113208 KB |
Output is correct |
8 |
Correct |
22 ms |
113244 KB |
Output is correct |
9 |
Correct |
22 ms |
113244 KB |
Output is correct |
10 |
Correct |
22 ms |
113240 KB |
Output is correct |
11 |
Correct |
23 ms |
113244 KB |
Output is correct |
12 |
Correct |
22 ms |
113244 KB |
Output is correct |
13 |
Correct |
22 ms |
113240 KB |
Output is correct |
14 |
Correct |
24 ms |
113104 KB |
Output is correct |
15 |
Correct |
22 ms |
113244 KB |
Output is correct |
16 |
Correct |
23 ms |
113244 KB |
Output is correct |
17 |
Correct |
22 ms |
113336 KB |
Output is correct |
18 |
Correct |
22 ms |
113240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
113232 KB |
Output is correct |
2 |
Correct |
23 ms |
113204 KB |
Output is correct |
3 |
Correct |
22 ms |
113244 KB |
Output is correct |
4 |
Correct |
26 ms |
113244 KB |
Output is correct |
5 |
Correct |
22 ms |
113244 KB |
Output is correct |
6 |
Correct |
22 ms |
113496 KB |
Output is correct |
7 |
Correct |
22 ms |
113208 KB |
Output is correct |
8 |
Correct |
22 ms |
113244 KB |
Output is correct |
9 |
Correct |
22 ms |
113244 KB |
Output is correct |
10 |
Correct |
22 ms |
113240 KB |
Output is correct |
11 |
Correct |
23 ms |
113244 KB |
Output is correct |
12 |
Correct |
22 ms |
113244 KB |
Output is correct |
13 |
Correct |
22 ms |
113240 KB |
Output is correct |
14 |
Correct |
24 ms |
113104 KB |
Output is correct |
15 |
Correct |
22 ms |
113244 KB |
Output is correct |
16 |
Correct |
23 ms |
113244 KB |
Output is correct |
17 |
Correct |
22 ms |
113336 KB |
Output is correct |
18 |
Correct |
22 ms |
113240 KB |
Output is correct |
19 |
Correct |
25 ms |
113596 KB |
Output is correct |
20 |
Correct |
24 ms |
113620 KB |
Output is correct |
21 |
Correct |
24 ms |
113596 KB |
Output is correct |
22 |
Correct |
23 ms |
113240 KB |
Output is correct |
23 |
Correct |
24 ms |
113448 KB |
Output is correct |
24 |
Correct |
23 ms |
113384 KB |
Output is correct |
25 |
Correct |
23 ms |
113500 KB |
Output is correct |
26 |
Correct |
23 ms |
113696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
224 ms |
138180 KB |
Output is correct |
2 |
Correct |
216 ms |
138460 KB |
Output is correct |
3 |
Correct |
226 ms |
138376 KB |
Output is correct |
4 |
Correct |
212 ms |
138376 KB |
Output is correct |
5 |
Correct |
108 ms |
128968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
157 ms |
130404 KB |
Output is correct |
2 |
Correct |
157 ms |
130400 KB |
Output is correct |
3 |
Correct |
148 ms |
130460 KB |
Output is correct |
4 |
Correct |
88 ms |
123272 KB |
Output is correct |
5 |
Correct |
22 ms |
113212 KB |
Output is correct |
6 |
Correct |
81 ms |
121132 KB |
Output is correct |
7 |
Correct |
125 ms |
127620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
359 ms |
147576 KB |
Output is correct |
2 |
Correct |
342 ms |
148684 KB |
Output is correct |
3 |
Correct |
746 ms |
183996 KB |
Output is correct |
4 |
Correct |
313 ms |
154040 KB |
Output is correct |
5 |
Correct |
220 ms |
139280 KB |
Output is correct |
6 |
Correct |
330 ms |
159460 KB |
Output is correct |
7 |
Correct |
369 ms |
160048 KB |
Output is correct |
8 |
Correct |
263 ms |
144408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
113232 KB |
Output is correct |
2 |
Correct |
23 ms |
113204 KB |
Output is correct |
3 |
Correct |
22 ms |
113244 KB |
Output is correct |
4 |
Correct |
26 ms |
113244 KB |
Output is correct |
5 |
Correct |
22 ms |
113244 KB |
Output is correct |
6 |
Correct |
22 ms |
113496 KB |
Output is correct |
7 |
Correct |
22 ms |
113208 KB |
Output is correct |
8 |
Correct |
22 ms |
113244 KB |
Output is correct |
9 |
Correct |
22 ms |
113244 KB |
Output is correct |
10 |
Correct |
22 ms |
113240 KB |
Output is correct |
11 |
Correct |
23 ms |
113244 KB |
Output is correct |
12 |
Correct |
22 ms |
113244 KB |
Output is correct |
13 |
Correct |
22 ms |
113240 KB |
Output is correct |
14 |
Correct |
24 ms |
113104 KB |
Output is correct |
15 |
Correct |
22 ms |
113244 KB |
Output is correct |
16 |
Correct |
23 ms |
113244 KB |
Output is correct |
17 |
Correct |
22 ms |
113336 KB |
Output is correct |
18 |
Correct |
22 ms |
113240 KB |
Output is correct |
19 |
Correct |
25 ms |
113596 KB |
Output is correct |
20 |
Correct |
24 ms |
113620 KB |
Output is correct |
21 |
Correct |
24 ms |
113596 KB |
Output is correct |
22 |
Correct |
23 ms |
113240 KB |
Output is correct |
23 |
Correct |
24 ms |
113448 KB |
Output is correct |
24 |
Correct |
23 ms |
113384 KB |
Output is correct |
25 |
Correct |
23 ms |
113500 KB |
Output is correct |
26 |
Correct |
23 ms |
113696 KB |
Output is correct |
27 |
Correct |
224 ms |
138180 KB |
Output is correct |
28 |
Correct |
216 ms |
138460 KB |
Output is correct |
29 |
Correct |
226 ms |
138376 KB |
Output is correct |
30 |
Correct |
212 ms |
138376 KB |
Output is correct |
31 |
Correct |
108 ms |
128968 KB |
Output is correct |
32 |
Correct |
157 ms |
130404 KB |
Output is correct |
33 |
Correct |
157 ms |
130400 KB |
Output is correct |
34 |
Correct |
148 ms |
130460 KB |
Output is correct |
35 |
Correct |
88 ms |
123272 KB |
Output is correct |
36 |
Correct |
22 ms |
113212 KB |
Output is correct |
37 |
Correct |
81 ms |
121132 KB |
Output is correct |
38 |
Correct |
125 ms |
127620 KB |
Output is correct |
39 |
Correct |
359 ms |
147576 KB |
Output is correct |
40 |
Correct |
342 ms |
148684 KB |
Output is correct |
41 |
Correct |
746 ms |
183996 KB |
Output is correct |
42 |
Correct |
313 ms |
154040 KB |
Output is correct |
43 |
Correct |
220 ms |
139280 KB |
Output is correct |
44 |
Correct |
330 ms |
159460 KB |
Output is correct |
45 |
Correct |
369 ms |
160048 KB |
Output is correct |
46 |
Correct |
263 ms |
144408 KB |
Output is correct |
47 |
Correct |
321 ms |
148684 KB |
Output is correct |
48 |
Correct |
330 ms |
148772 KB |
Output is correct |
49 |
Correct |
733 ms |
184132 KB |
Output is correct |
50 |
Correct |
305 ms |
154012 KB |
Output is correct |
51 |
Correct |
256 ms |
136276 KB |
Output is correct |
52 |
Correct |
340 ms |
140832 KB |
Output is correct |
53 |
Correct |
338 ms |
161404 KB |
Output is correct |
54 |
Correct |
349 ms |
161916 KB |
Output is correct |
55 |
Correct |
528 ms |
170516 KB |
Output is correct |