Submission #1152742

#TimeUsernameProblemLanguageResultExecution timeMemory
1152742ezzzayGlobal Warming (CEOI18_glo)C++20
100 / 100
1433 ms185960 KiB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define pb push_back
const int N=1e6+5;
int a[N];
int dp[N];
int b[N];
int n;
int st[4*N];
int tmp[N];
int st2[4*N];
void update(int node, int L, int R, int idx, int val){
    if(L==R){
        st[node]=val;
        return ;
    }
    int mid=(L+R)/2;
    if(L<=idx and idx<=mid){
        update(node*2,L,mid,idx,val);
    }
    else {
        update(node*2+1,mid+1,R,idx,val);
    }
    st[node]=max(st[node*2],st[node*2+1]);
}
int find(int node, int L , int R, int l, int r){
    if(l>R or L>r)return 0;
    if(l<=L and R<=r)return st[node];
    int mid=(L+R)/2;
    return max( find(node*2,L,mid,l,r) , find(node*2+1,mid+1,R,l,r)) ;
}
void update2(int node, int L, int R, int idx, int val){
    if(L==R){
        st2[node]=val;
        return ;
    }
    int mid=(L+R)/2;
    if(L<=idx and idx<=mid){
        update2(node*2,L,mid,idx,val);
    }
    else {
        update2(node*2+1,mid+1,R,idx,val);
    }
    st2[node]=max(st2[node*2],st2[node*2+1]);
}
int find2(int node, int L , int R, int l, int r){
    if(l>R or L>r)return 0;
    if(l<=L and R<=r)return st2[node];
    int mid=(L+R)/2;
    return max( find2(node*2,L,mid,l,r) , find2(node*2+1,mid+1,R,l,r)) ;
}
multiset< int> mlt[N];
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int x;
    cin>>n>>x;
    map<int,int>mp,mp2;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        mp[a[i]];
        mp[a[i]-x];
        mp[a[i]+x];
    }
    int idx=1;
    for(auto it=mp.begin();it!=mp.end();it++){
        it->ss = idx++;
    }
    for(int i=1;i<=n;i++){
    	b[i]=a[i];
    	a[i]=mp[a[i]];
	}
    for(int i=1;i<=n;i++){
        dp[i]= find(1,1,idx,1,a[i]-1)+1;
        update(1,1,idx,a[i],dp[i]);
    }
    for(int i=1;i<=n;i++){
    	a[i]= -b[i];
    	mp2[a[i]];
        mp2[a[i]-x];
        mp2[a[i]+x];
	}
	int idx2=1;
    for(auto it=mp2.begin();it!=mp2.end();it++){
        it->ss = idx2++;
    }
    for(int i=1;i<=n;i++){
    	a[i]=mp2[a[i]];
	}
	int ans=st[1];
	for(int i=0;i<N*4;i++){
		st[i]=0;
	}
	for(int i=1;i<=n;i++){
		mlt[mp[b[i]]].insert( - dp[i]);
		int k=  - (*mlt[mp[b[i]]].begin());
		update(1,1,idx,mp[b[i]],k);
	}
	
	for(int i=n;i>=1;i--){
        tmp[i]= find2(1,1,idx2,1,a[i]-1)+1;
        update2(1,1,idx2,a[i],tmp[i]);
        int h= mp[b[i]+x]-1;
        int y=mp[b[i]];
        mlt[y].erase( mlt[y].find(-dp[i]));
        int k=0;
        if(mlt[y].size()){
        	k-= *mlt[y].begin();
		}
        update(1,1,idx,y,k);
        ans=max(ans,tmp[i]+ find(1,1,idx,1, h));
        
    }
    cout<<ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...