이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
const int N=300001;
int n, d;
int a[N];
int mx[4*N];
int get_max(int l, int r, int x, int lx, int rx){
if(lx>=r || rx<=l) return 0;
if(lx>=l && rx<=r) return mx[x];
int m=(lx+rx)/2;
return max(get_max(l, r, 2*x+1, lx, m), get_max(l, r, 2*x+2, m, rx));
}
int get_max(int l, int r){ return get_max(l, r, 0, 0, n+1); }
void upd(int i, int v, int x, int lx, int rx){
if(rx-lx==1){
mx[x]=v;
return;
}
int m=(lx+rx)/2;
if(i<m) upd(i, v, 2*x+1, lx, m);
else upd(i, v, 2*x+2, m, rx);
mx[x]=max(mx[2*x+1], mx[2*x+2]);
}
void upd(int i, int v){ upd(i, v, 0, 0, n+1); }
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> d;
set<int> s;
map<int, int> mp;
vector<vector<int>> ind(n+1);
for(int i=1; i<=n; i++){
cin >> a[i];
s.insert(a[i]);
}
int cur=0;
for(auto it=s.begin(); it!=s.end(); it++){
mp[*it]=++cur;
}
for(int i=1; i<=n; i++){
a[i]=mp[a[i]];
ind[a[i]].push_back(i);
}
set<int> small, bad;
for(int i=d; i<=n; i++) bad.insert(i);
small.insert(0); small.insert(n+1);
vector<int> dp(n+1);
auto ins=[&](int i){
int l=*--small.upper_bound(i);
int r=*small.upper_bound(i);
small.insert(i);
if(r-l+1<d) return;
for(int j=max(i, l+d-1); i>=j-d+1 && j<=r; j++){
// cout << "erase " << j << "\n";
bad.erase(j);
}
};
int ans=0;
for(int k=1; k<=n; k++){
for(int i:ind[k]){
ins(i);
auto it=bad.upper_bound(i);
int l=it==bad.begin()?0:*--it-d+1;
dp[i]=get_max(l, i)+1;
// cout << "dp[" << i << "] = " << dp[i] << "\n";
// cout << "try [" << l << ", " << i << ")\n";
}
for(int i:ind[k]){
upd(i, dp[i]);
ans=max(ans, dp[i]);
}
}
cout << ans;
}
// segment tree max, update point
# | 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... |