#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
const int N = 2e5 + 5;
int dp[N][2];
struct SegTree{
vector<int> tree;
int n;
SegTree(int _n){
n = _n;
tree.resize(4*n+5);
}
void update(int node,int l,int r,int idx,int val){
if(l == r) return tree[node] = max(tree[node] , val) , void();
int md = l + r >> 1;
idx <= md ? update(node<<1,l,md,idx,val) : update(node<<1|1,md+1,r,idx,val);
tree[node] = max(tree[node<<1],tree[node<<1|1]);
}
int query(int node,int l,int r,int lQ,int rQ){
if(l > r || l > rQ || lQ > r) return 0;
if(lQ <= l && r <= rQ) return tree[node];
int md = l + r >> 1;
return max(query(node<<1,l,md,lQ,rQ) , query(node<<1|1,md+1,r,lQ,rQ));
}
void update(int idx,int val){
update(1,1,n,idx,val);
}
int query(int l,int r){
if(l > r) return 0;
return query(1,1,n,l,r);
}
};
void compress(vector<int> &a){
int n = a.size();
vector<pair<int,int>> b(n);
for(int i=0;i<n;i++){
b[i].first = a[i];
b[i].second = i;
}
sort(b.begin(),b.end());
a[b[0].second] = 1;
int cnt = 1;
for(int i=1;i<n;i++){
if(b[i].first == b[i-1].first){
a[b[i].second] = a[b[i-1].second];
}
else a[b[i].second] = ++cnt;
}
}
void solve(){
ll n,x;
cin>>n>>x;
vector<ll> v(n);
for(int i=0;i<n;i++) cin>>v[i];
vector<int> tmp;
for(auto i : v) {
tmp.push_back(i);
tmp.push_back(i-x+1);
tmp.push_back(i+1);
}
compress(tmp);
vector<ll> change(n);
for(int i=0;i<n;i++){
v[i] = tmp[i*3];
change[i] = tmp[i*3+1];
}
SegTree sg0(3*n+5);
SegTree sg1(3*n+5);
for(int i=n-1;i>=0;i--){
for(int j=0;j<2;j++){
/*if(v[k] > v[i]){
dp[i][j] = max(dp[i][j] , dp[k][j] + 1);
}*/
dp[i][j] = j==0?sg0.query(v[i]+1,3*n):sg1.query(v[i]+1,3*n);
if(!j){
/*for(int k=i+1;k<n;k++){
if(v[k] > v[i]-x){
dp[i][j] = max(dp[i][j] , dp[k][1] + 1);
}
}*/
dp[i][j] = max(dp[i][j] , sg1.query(change[i],3*n));
}
j==0?sg0.update(v[i],dp[i][j]+1):sg1.update(v[i],dp[i][j]+1);
}
}
int ans = 0;
for(int i=0;i<n;i++){
ans = max(ans, dp[i][0] + 1);
}
cout<<ans<<endl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
Compilation message
glo.cpp: In member function 'void SegTree::update(int, int, int, int, int)':
glo.cpp:16:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
16 | int md = l + r >> 1;
| ~~^~~
glo.cpp: In member function 'int SegTree::query(int, int, int, int, int)':
glo.cpp:23:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
23 | int md = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
2 ms |
396 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
282 ms |
28040 KB |
Output is correct |
2 |
Correct |
300 ms |
28772 KB |
Output is correct |
3 |
Correct |
273 ms |
28908 KB |
Output is correct |
4 |
Correct |
297 ms |
29852 KB |
Output is correct |
5 |
Correct |
199 ms |
28052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
6832 KB |
Output is correct |
2 |
Correct |
58 ms |
7208 KB |
Output is correct |
3 |
Correct |
62 ms |
7236 KB |
Output is correct |
4 |
Correct |
50 ms |
6976 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
44 ms |
7336 KB |
Output is correct |
7 |
Correct |
52 ms |
7228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
13220 KB |
Output is correct |
2 |
Correct |
147 ms |
14236 KB |
Output is correct |
3 |
Correct |
290 ms |
29648 KB |
Output is correct |
4 |
Correct |
223 ms |
28036 KB |
Output is correct |
5 |
Correct |
99 ms |
13708 KB |
Output is correct |
6 |
Correct |
174 ms |
26764 KB |
Output is correct |
7 |
Correct |
177 ms |
28764 KB |
Output is correct |
8 |
Correct |
114 ms |
14312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
2 ms |
396 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
282 ms |
28040 KB |
Output is correct |
28 |
Correct |
300 ms |
28772 KB |
Output is correct |
29 |
Correct |
273 ms |
28908 KB |
Output is correct |
30 |
Correct |
297 ms |
29852 KB |
Output is correct |
31 |
Correct |
199 ms |
28052 KB |
Output is correct |
32 |
Correct |
59 ms |
6832 KB |
Output is correct |
33 |
Correct |
58 ms |
7208 KB |
Output is correct |
34 |
Correct |
62 ms |
7236 KB |
Output is correct |
35 |
Correct |
50 ms |
6976 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
44 ms |
7336 KB |
Output is correct |
38 |
Correct |
52 ms |
7228 KB |
Output is correct |
39 |
Correct |
125 ms |
13220 KB |
Output is correct |
40 |
Correct |
147 ms |
14236 KB |
Output is correct |
41 |
Correct |
290 ms |
29648 KB |
Output is correct |
42 |
Correct |
223 ms |
28036 KB |
Output is correct |
43 |
Correct |
99 ms |
13708 KB |
Output is correct |
44 |
Correct |
174 ms |
26764 KB |
Output is correct |
45 |
Correct |
177 ms |
28764 KB |
Output is correct |
46 |
Correct |
114 ms |
14312 KB |
Output is correct |
47 |
Correct |
138 ms |
14284 KB |
Output is correct |
48 |
Correct |
133 ms |
14284 KB |
Output is correct |
49 |
Correct |
315 ms |
29312 KB |
Output is correct |
50 |
Correct |
203 ms |
28104 KB |
Output is correct |
51 |
Correct |
144 ms |
20456 KB |
Output is correct |
52 |
Correct |
233 ms |
28992 KB |
Output is correct |
53 |
Correct |
182 ms |
27340 KB |
Output is correct |
54 |
Correct |
198 ms |
28012 KB |
Output is correct |
55 |
Correct |
233 ms |
28640 KB |
Output is correct |