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>
#include<fstream>
using namespace std;
ifstream fin("VNOICUP.INP");
ofstream fout("VNOICUP.OUT");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
using u128 = __uint128_t;
//const int x[4] = {1, -1, 0, 0};
//const int y[4] = {0, 0, 1, -1};
const ll mod = 1e9 + 9;
const int mxn = 1e6 + 5, mxq = 2e5 + 5, sq = 400, mxv = 3e5 + 5;
//const int base = (1 << 18);
const int inf = 1e9, neg = -69420;
struct DSU{
vt<pii>past_p, past_sz;
vt<int>checkpoint;
int p[mxn + 1], sz[mxn + 1];
void init(){
for(int i = 1; i <= mxn; i++){
p[i] = i; sz[i] = 1;
}
past_sz.clear(); past_p.clear(); checkpoint.clear();
}
int fp(int a){
if(p[a] == a)return(a);
return(fp(p[a]));
}
bool check(int u, int v){
return(fp(u) == fp(v));
}
void unon(int a, int b){
a = fp(a); b = fp(b);
if(a > b)swap(a, b);
//past_sz.pb({a, sz[a]}); past_p.pb({b, p[b]});
if(a != b){
sz[a] += sz[b]; p[b] = a;
}
}
void rollback(){
//int before = p[past_p.back().fi], after = past_p.back().se;
//if(before != past_p.back().fi && after == past_p.back().fi)cnt++;
p[past_p.back().fi] = past_p.back().se; past_p.pop_back();
sz[past_sz.back().fi] = past_sz.back().se; past_sz.pop_back();
}
void save(){
checkpoint.pb(past_sz.size());
}
void to_last(){
while(past_sz.size() != checkpoint.back())rollback();
checkpoint.pop_back();
}
};
int st[4 * mxn + 1];
void upd(int nd, int l, int r, int id, int v){
if(id > r || id < l)return;
if(l == r){
st[nd] = v;
return;
}
int mid = (l + r) >> 1;
upd(nd << 1, l, mid, id, v); upd(nd << 1 | 1, mid + 1, r, id, v);
st[nd] = max(st[nd << 1], st[nd << 1 | 1]);
}
int get(int nd, int l, int r, int ql, int qr){
if(ql > r || qr < l)return(0);
if(ql <= l && qr >= r)return(st[nd]);
int mid = (l + r) >> 1;
return(max(get(nd << 1, l, mid, ql, qr), get(nd << 1 | 1, mid + 1, r, ql, qr)));
}
DSU dsu;
int n, d;
int a[mxn + 1], dp[mxn + 1];
vt<int>idx[mxn + 1];
vt<int>comp;
int find(int x){
return(lower_bound(comp.begin(), comp.end(), x) - comp.begin() + 1);
}
void compress(){
for(int i = 1; i <= n; i++)comp.pb(a[i]);
sort(comp.begin(), comp.end());
for(int i = 1; i <= n; i++){
a[i] = find(a[i]);
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> d;
dsu.init();
for(int i = 1; i <= n; i++)cin >> a[i];
compress();
for(int i = 1; i <= n; i++){
idx[a[i]].pb(i);
}
set<int>save;
int ans = 1;
for(int i = 1; i <= n; i++){
if(sz(idx[i]) == 0)continue;
reverse(idx[i].begin(), idx[i].end());
for(int j = 0; j < sz(idx[i]); j++){
int x = idx[i][j];
auto lw = save.lower_bound(x);
if(lw != save.begin()){
--lw;
if((x - *lw) <= d){
dsu.unon(x, *lw);
}
}
auto hg = save.upper_bound(x);
if(hg != save.end()){
if((*hg - x) <= d){
dsu.unon(x, *hg);
}
}
int lft = dsu.fp(x);
dp[x] = get(1, 1, n, lft, x - 1) + 1;
save.insert(x);
ans = max(ans, dp[x]);
upd(1, 1, n, x, dp[x]);
}
}
//for(int i = 1; i <= n; i++)cout << dp[i] << ' ';
cout << ans;
}
Compilation message (stderr)
Main.cpp: In member function 'void DSU::to_last()':
Main.cpp:61:30: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} [-Wsign-compare]
61 | while(past_sz.size() != checkpoint.back())rollback();
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
# | 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... |