#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
int N, X;
struct MaxTree{
int len = 1;
vector<int> tr;
MaxTree(int n){
while(len<n){
len*=2;
}
tr.resize(2*len);
}
void upd(int u){
for(u/=2; u>=1; u/=2){
tr[u] = max(tr[u*2], tr[u*2+1]);
}
}
int get(int l, int r){
l += len;
r+=len+1;
int res = 0;
for(; l<r; l/=2, r/=2){
if(l%2 ==1){
res = max(res, tr[l++]);
}
if(r%2 == 1){
res = max(res, tr[--r]);
}
}
return res;
}
void set(int pos, int val){
pos+=len;
tr[pos] = max(tr[pos], val);
upd(pos);
}
};
int find_pos(int e, vector<int>& v){
int pos =0;
for(int step = (1LL<<20); step>=1; step/=2){
if(pos+step<v.size() && v[pos+step]<=e){
pos+=step;
}
}
return pos;
}
vector<int> vals;
int lis(vector<pii>& v){
MaxTree low(vals.size());
MaxTree high(vals.size());
for(int i= 0; i<N; i++){
int best_high = max(high.get(0, v[i].first-1), low.get(0, v[i].second-1));
high.set(v[i].first, best_high+1);
int best_low = low.get(0, v[i].first-1);
low.set(v[i].first, best_low+1);
}
return high.get(0, vals.size()-1);
}
signed main(){
cin>>N>>X;
vector<pii> t(N);
vals.push_back(0);
for(int i = 0; i<N; i++){
cin>>t[i].first;
vals.push_back(t[i].first);
vals.push_back(t[i].first+X);
}
sort(vals.begin(), vals.end());
for(int i = 0; i<N; i++){
t[i] ={find_pos(t[i].first, vals), find_pos(t[i].first+X, vals)};
}
cout<<lis(t)<<endl;
}
Compilation message
glo.cpp: In function 'long long int find_pos(long long int, std::vector<long long int>&)':
glo.cpp:51:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | if(pos+step<v.size() && v[pos+step]<=e){
| ~~~~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
348 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
348 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
163 ms |
23228 KB |
Output is correct |
2 |
Correct |
175 ms |
23048 KB |
Output is correct |
3 |
Correct |
169 ms |
23768 KB |
Output is correct |
4 |
Correct |
160 ms |
23216 KB |
Output is correct |
5 |
Correct |
99 ms |
23832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
6084 KB |
Output is correct |
2 |
Correct |
38 ms |
6096 KB |
Output is correct |
3 |
Correct |
49 ms |
6084 KB |
Output is correct |
4 |
Correct |
24 ms |
6092 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
22 ms |
6448 KB |
Output is correct |
7 |
Correct |
33 ms |
6608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
11712 KB |
Output is correct |
2 |
Correct |
79 ms |
11716 KB |
Output is correct |
3 |
Correct |
182 ms |
23076 KB |
Output is correct |
4 |
Correct |
117 ms |
23224 KB |
Output is correct |
5 |
Correct |
54 ms |
11720 KB |
Output is correct |
6 |
Correct |
105 ms |
22900 KB |
Output is correct |
7 |
Correct |
122 ms |
22712 KB |
Output is correct |
8 |
Correct |
70 ms |
11716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
348 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
163 ms |
23228 KB |
Output is correct |
28 |
Correct |
175 ms |
23048 KB |
Output is correct |
29 |
Correct |
169 ms |
23768 KB |
Output is correct |
30 |
Correct |
160 ms |
23216 KB |
Output is correct |
31 |
Correct |
99 ms |
23832 KB |
Output is correct |
32 |
Correct |
36 ms |
6084 KB |
Output is correct |
33 |
Correct |
38 ms |
6096 KB |
Output is correct |
34 |
Correct |
49 ms |
6084 KB |
Output is correct |
35 |
Correct |
24 ms |
6092 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
22 ms |
6448 KB |
Output is correct |
38 |
Correct |
33 ms |
6608 KB |
Output is correct |
39 |
Correct |
77 ms |
11712 KB |
Output is correct |
40 |
Correct |
79 ms |
11716 KB |
Output is correct |
41 |
Correct |
182 ms |
23076 KB |
Output is correct |
42 |
Correct |
117 ms |
23224 KB |
Output is correct |
43 |
Correct |
54 ms |
11720 KB |
Output is correct |
44 |
Correct |
105 ms |
22900 KB |
Output is correct |
45 |
Correct |
122 ms |
22712 KB |
Output is correct |
46 |
Correct |
70 ms |
11716 KB |
Output is correct |
47 |
Correct |
82 ms |
12636 KB |
Output is correct |
48 |
Correct |
76 ms |
12740 KB |
Output is correct |
49 |
Correct |
167 ms |
25624 KB |
Output is correct |
50 |
Correct |
104 ms |
25124 KB |
Output is correct |
51 |
Correct |
99 ms |
23064 KB |
Output is correct |
52 |
Correct |
106 ms |
25020 KB |
Output is correct |
53 |
Correct |
109 ms |
25020 KB |
Output is correct |
54 |
Correct |
123 ms |
25276 KB |
Output is correct |
55 |
Correct |
143 ms |
26080 KB |
Output is correct |