Submission #1305547

#TimeUsernameProblemLanguageResultExecution timeMemory
1305547liangjeremyGlobal Warming (CEOI18_glo)C++20
100 / 100
185 ms20940 KiB
#include<bits/stdc++.h> #include<bits/extc++.h> #define fi first #define se second #define int long long using namespace std; //using namespace __gnu_pbds; using db=double; using ll=int64_t; using sll=__int128; using lb=long double; mt19937 rng(random_device{}()); struct segtree{ vector<int>mx; segtree(int n){ mx.resize(n<<2); } void update(int l, int r, int rt, int idx, int val){ if(l==r){ mx[rt]=max(mx[rt],val); return; } int mid=(l+r)>>1; if(idx<=mid)update(l,mid,rt<<1,idx,val); else update(mid+1,r,rt<<1|1,idx,val); mx[rt]=max(mx[rt<<1],mx[rt<<1|1]); } int query(int l, int r, int rt, int L, int R){ if(L<=l && r<=R)return mx[rt]; int ans=0; int mid=(l+r)>>1; if(L<=mid)ans=max(ans,query(l,mid,rt<<1,L,R)); if(R>mid)ans=max(ans,query(mid+1,r,rt<<1|1,L,R)); return ans; } }; int32_t main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,x; cin>>n>>x; vector<int>a(n+1); vector<int>v; vector<int>a2(n+1); for(int i=1; i<=n; i++){ cin>>a[i]; v.push_back(a[i]); } sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); for(int i=1; i<=n; i++){ a2[i]=a[i]; a[i]=lower_bound(v.begin(),v.end(),a[i])-v.begin()+1; } vector<int>pre(n+1); segtree segl(n+20); for(int i=1; i<=n; i++){ pre[i]=segl.query(0,n+1,1,0,a[i]-1)+1; segl.update(0,n+1,1,a[i],pre[i]); } int ans=segl.query(0,n+1,1,0,n+1); vector<int>suf(n+1); segtree segr(n+20); for(int i=n; i>=1; i--){ suf[i]=segr.query(0,n+1,1,a[i]+1,n+1)+1; segr.update(0,n+1,1,a[i],suf[i]); int idx=lower_bound(v.begin(),v.end(),a2[i-1]-x+1)-v.begin()+1; ans=max(ans,pre[i-1]+segr.query(0,n+1,1,idx,n+1)); } cout<<ans<<'\n'; } /* Overhead the albatross hangs motionless upon the air And deep beneath the rolling waves in labyrinths of coral caves The echo of a distant time comes willowing across the sand And everything is green and submarine And no one showed us to the land And no one knows the wheres or whys But something stirs and something tries And starts to climb towards the light */
#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...