This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}
const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 3e5 + 7;
const ll oo = 1e18 + 69;
int n, d, a[maxn], dp[maxn], last[maxn];
vector<int> val, ev[maxn];
struct ST {
struct node {
int mx;
friend node operator + (node a, const node &b) {
maxi(a.mx, b.mx);
return a;
}
};
node st[4 * maxn];
int lz[4 * maxn];
void fix(int id, int l, int r) {
if(!lz[id]) return;
maxi(st[id].mx, lz[id]);
if(l != r) {
maxi(lz[id << 1], lz[id]);
maxi(lz[id << 1 | 1], lz[id]);
}
lz[id] = 0;
}
void init() {
for(int i = 1; i <= 4 * n; i++) st[i].mx = lz[i] = 0;
}
void update(int id, int l, int r, int pos, int val) {
if(pos < l || pos > r) return;
if(l == r) {
st[id].mx = val;
return;
}
int mid = l + r >> 1;
update(id << 1, l, mid, pos, val);
update(id << 1 | 1, mid + 1, r, pos, val);
st[id] = st[id << 1] + st[id << 1 | 1];
}
void update2(int id, int l, int r, int u, int v, int val) {
fix(id, l, r);
if(r < u || l > v) return;
if(u <= l && r <= v) {
lz[id] = val;
fix(id, l, r);
return;
}
int mid = l + r >> 1;
update2(id << 1, l, mid, u, v, val);
update2(id << 1 | 1, mid + 1, r, u, v, val);
}
node get(int id, int l, int r, int u, int v) {
fix(id, l, r);
if(r < u || l > v) return {0};
if(u <= l && r <= v) return st[id];
int mid = l + r >> 1;
return get(id << 1, l, mid, u, v) + get(id << 1 | 1, mid + 1, r, u, v);
}
pa get2(int id, int l, int r, int u, int v) {
if(r < u || l > v) return {n + 1, 0};
if(u <= l && r <= v) {
if(st[id].mx <= d) return {n + 1, 0};
while(l < r) {
int mid = l + r >> 1;
if(st[id << 1].mx > d) {
id = id << 1;
r = mid;
} else {
id = id << 1 | 1;
l = mid + 1;
}
}
return {l, st[id].mx};
}
int mid = l + r >> 1;
pa s1 = get2(id << 1, l, mid, u, v);
if(s1.fi != n + 1) return s1;
return get2(id << 1 | 1, mid + 1, r, u, v);
}
} st;
int id(int x) {
return lower_bound(all(val), x) - val.begin();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen(TASK".inp", "r", stdin);
//freopen(TASK".out", "w", stdout);
cin>>n>>d;
for(int i = 1; i <= n; i++) {
cin>>a[i];
val.pb(a[i]);
}
val.pb(0);
sort(all(val));
val.erase(unique(all(val)), val.end());
for(int i = n; i >= 1; i--) ev[id(a[i])].pb(i);
set<int> s;
s.insert(n + d + 1);
for(int i = 0; i < Size(val); i++) {
for(auto j:ev[i]) {
s.insert(j);
auto it = s.lower_bound(j);
if(it != s.begin()) {
st.update(1, 1, n, *prev(it), j - *prev(it));
}
st.update(1, 1, n, j, *next(it) - j);
}
for(auto j:ev[i]) {
auto tmp = st.get2(1, 1, n, j, n);
last[j] = min(n + 1, tmp.fi + min(d + 1, tmp.se));
}
}
// for(int i = 1; i <= n; i++) cout<<last[i]<<" ";
// cout<<"\n";
st.init();
for(int i = 0; i < Size(val); i++) {
for(auto j:ev[i]) {
dp[j] = st.get(1, 1, n, j, j).mx + 1;
}
for(auto j:ev[i]) {
st.update2(1, 1, n, j, last[j] - 1, dp[j]);
}
}
// for(int i = 1; i <= n; i++) cout<<dp[i]<<"\n";
cout<<*max_element(dp + 1, dp + 1 + n);
return 0;
}
Compilation message (stderr)
Main.cpp: In member function 'void ST::update(int, int, int, int, int)':
Main.cpp:57:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
57 | int mid = l + r >> 1;
| ~~^~~
Main.cpp: In member function 'void ST::update2(int, int, int, int, int, int)':
Main.cpp:71:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
71 | int mid = l + r >> 1;
| ~~^~~
Main.cpp: In member function 'ST::node ST::get(int, int, int, int, int)':
Main.cpp:80:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
80 | int mid = l + r >> 1;
| ~~^~~
Main.cpp: In member function 'std::pair<int, int> ST::get2(int, int, int, int, int)':
Main.cpp:89:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
89 | int mid = l + r >> 1;
| ~~^~~
Main.cpp:100:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
100 | int mid = l + r >> 1;
| ~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |