#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define en '\n'
#define sp ' '
#define tb '\t'
#define ri(n) int n; cin >> n
#define rl(n) ll n; cin >> n
#define rs(s) string s; cin >> s
#define rc(c) char c; cin >> c
#define rv(v) for (auto &x : v) cin >> x
#define pven(v) for (auto x : v) cout << x << en
#define pv(v) for (auto x : v) cout << x << sp; cout << en
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define yes cout << "YES" << en
#define no cout << "NO" << en
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define ssort(a, b) if (a < b) swap(a, b)
#define bitcnt(a) __builtin_popcountll(a)
#define bithigh(a) 63-__builtin_clzll(a)
#define lg bithigh
ll highpow(ll a) { return 1LL << (ll)lg(a); }
using namespace std;
struct node{
int val;
node* child[2];
node(int v = 0){ val = v; child[0] = child[1] = nullptr; }
node* ch(int i){ if (!child[i]) child[i] = new node(); return child[i]; }
};
class segtree{
private:
ll n;
node* root;
void init(ll s){
n = highpow(s);
if (bitcnt(s) > 1) n <<= 1;
root = new node();
}
int update(node* s, ll l, ll r, ll pos, int x){
if (pos < l || pos > r) return s->val;
if (l == r) return s->val = x;
ll m = (l + r)>>1;
int a = 0, b = 0;
if (pos <= m){
a = update(s->ch(0), l, m, pos, x);
if (s->child[1]) b = s->child[1]->val;
}
else{
b = update(s->ch(1), m+1, r, pos, x);
if (s->child[0]) a = s->child[0]->val;
}
return s->val = max(a, b);
}
int query(node* s, ll l, ll r, ll ql, ll qr) const {
if (ql > r || qr < l) return 0;
if (l >= ql && r <= qr) return s->val;
ll m = (l + r)>>1;
int a = 0, b = 0;
if (s->child[0]) a = query(s->ch(0), l, m, ql, qr);
if (s->child[1]) b = query(s->ch(1), m+1, r, ql, qr);
return max(a, b);
}
public:
segtree(ll n){ init(n); }
void update(ll pos, int x){ update(root, 0, n-1, pos, x); }
int query(ll l, ll r) const { if (l>r) return 0; return query(root, 0, n-1, l, r); }
};
const ll LINF = 4e18;
const int mxN = 2e5+10, INF = 2e9, mod = (1 ? 1e9+7 : 998244353);
ll n, m, a[mxN];
segtree* st[2];
void Solve(){
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> a[i];
map<ll, vector<array<int, 2> > > mp;
st[0] = new segtree(INF);
st[1] = new segtree(INF);
for (int i = n-1; ~i; i--){
int x = st[1]->query(a[i]+m+1, INF);
if (x+1 > st[1]->query(a[i]+m, a[i]+m))
st[1]->update(a[i]+m, x+1);
mp[a[i]].pb({x+1, i});
}
for (auto& [k, v] : mp) sort(all(v));
int ans = 0;
for (int i = 0; i < n; i++){
while (mp[a[i]].size() && mp[a[i]].back()[1] <= i) mp[a[i]].pop_back();
int t = 0;
if (mp[a[i]].size()) t = mp[a[i]].back()[0];
st[1]->update(a[i]+m, t);
int x = st[0]->query(0, a[i]-1);
if (x+1 > st[0]->query(a[i], a[i]))
st[0]->update(a[i], x+1);
smax(ans, x+1 + st[1]->query(a[i]+1, INF));
}
cout << ans << en;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0); cerr.tie(0);
cout << setprecision(12) << fixed;
cerr << setprecision(12) << fixed;
cerr << "Started!" << endl;
int t = 1;
//cin >> t;
while (t--)
Solve();
return 0;
}
Compilation message
glo.cpp: In function 'long long int highpow(long long int)':
glo.cpp:26:22: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
26 | #define bithigh(a) 63-__builtin_clzll(a)
| ^
glo.cpp:27:12: note: in expansion of macro 'bithigh'
27 | #define lg bithigh
| ^~~~~~~
glo.cpp:28:38: note: in expansion of macro 'lg'
28 | ll highpow(ll a) { return 1LL << (ll)lg(a); }
| ^~
glo.cpp: In function 'void Solve()':
glo.cpp:100:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
100 | for (auto& [k, v] : mp) sort(all(v));
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
324 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
324 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
4 ms |
1748 KB |
Output is correct |
20 |
Correct |
4 ms |
1748 KB |
Output is correct |
21 |
Correct |
4 ms |
1744 KB |
Output is correct |
22 |
Correct |
4 ms |
1748 KB |
Output is correct |
23 |
Correct |
3 ms |
980 KB |
Output is correct |
24 |
Correct |
3 ms |
1100 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1402 ms |
193128 KB |
Output is correct |
2 |
Correct |
1421 ms |
193264 KB |
Output is correct |
3 |
Correct |
1375 ms |
193488 KB |
Output is correct |
4 |
Correct |
1328 ms |
193172 KB |
Output is correct |
5 |
Correct |
465 ms |
104772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
301 ms |
54764 KB |
Output is correct |
2 |
Correct |
269 ms |
54696 KB |
Output is correct |
3 |
Correct |
294 ms |
54776 KB |
Output is correct |
4 |
Correct |
128 ms |
29532 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
88 ms |
8360 KB |
Output is correct |
7 |
Correct |
197 ms |
40836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
594 ms |
103028 KB |
Output is correct |
2 |
Correct |
624 ms |
103044 KB |
Output is correct |
3 |
Correct |
1364 ms |
193284 KB |
Output is correct |
4 |
Correct |
539 ms |
105004 KB |
Output is correct |
5 |
Correct |
187 ms |
25120 KB |
Output is correct |
6 |
Correct |
331 ms |
47648 KB |
Output is correct |
7 |
Correct |
377 ms |
48436 KB |
Output is correct |
8 |
Correct |
423 ms |
77208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
324 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
4 ms |
1748 KB |
Output is correct |
20 |
Correct |
4 ms |
1748 KB |
Output is correct |
21 |
Correct |
4 ms |
1744 KB |
Output is correct |
22 |
Correct |
4 ms |
1748 KB |
Output is correct |
23 |
Correct |
3 ms |
980 KB |
Output is correct |
24 |
Correct |
3 ms |
1100 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
1402 ms |
193128 KB |
Output is correct |
28 |
Correct |
1421 ms |
193264 KB |
Output is correct |
29 |
Correct |
1375 ms |
193488 KB |
Output is correct |
30 |
Correct |
1328 ms |
193172 KB |
Output is correct |
31 |
Correct |
465 ms |
104772 KB |
Output is correct |
32 |
Correct |
301 ms |
54764 KB |
Output is correct |
33 |
Correct |
269 ms |
54696 KB |
Output is correct |
34 |
Correct |
294 ms |
54776 KB |
Output is correct |
35 |
Correct |
128 ms |
29532 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
88 ms |
8360 KB |
Output is correct |
38 |
Correct |
197 ms |
40836 KB |
Output is correct |
39 |
Correct |
594 ms |
103028 KB |
Output is correct |
40 |
Correct |
624 ms |
103044 KB |
Output is correct |
41 |
Correct |
1364 ms |
193284 KB |
Output is correct |
42 |
Correct |
539 ms |
105004 KB |
Output is correct |
43 |
Correct |
187 ms |
25120 KB |
Output is correct |
44 |
Correct |
331 ms |
47648 KB |
Output is correct |
45 |
Correct |
377 ms |
48436 KB |
Output is correct |
46 |
Correct |
423 ms |
77208 KB |
Output is correct |
47 |
Correct |
638 ms |
103132 KB |
Output is correct |
48 |
Correct |
615 ms |
103092 KB |
Output is correct |
49 |
Correct |
1340 ms |
193244 KB |
Output is correct |
50 |
Correct |
473 ms |
104784 KB |
Output is correct |
51 |
Correct |
251 ms |
25756 KB |
Output is correct |
52 |
Correct |
460 ms |
50260 KB |
Output is correct |
53 |
Correct |
409 ms |
50064 KB |
Output is correct |
54 |
Correct |
380 ms |
50748 KB |
Output is correct |
55 |
Correct |
1002 ms |
145872 KB |
Output is correct |