제출 #1048360

#제출 시각아이디문제언어결과실행 시간메모리
1048360vako_pGlobal Warming (CEOI18_glo)C++14
100 / 100
233 ms13788 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back const int mxN = 2e5 + 5; ll n,mx,a[mxN],dp[mxN],dp1[mxN]; pair<ll,ll> _b[mxN + 1]; pair<ll,ll>* b = _b + 1; vector<ll> vv; struct segtree{ vector<ll> v; ll sz = 1; void init(){ while(sz < n) sz *= 2; v.assign(2 * sz, 0LL); } void clear(){ for(int i = 0; i < 2 * sz; i++) v[i] = 0LL; } void set(ll val, ll i, ll x, ll l, ll r){ if(r - l == 1){ v[x] = val; return; } ll mid = l + (r - l) / 2; if(i < mid) set(val, i, 2 * x + 1, l, mid); else set(val, i, 2 * x + 2, mid, r); v[x] = max(v[2 * x + 1], v[2 * x + 2]); } void set(ll val, ll i){ set(val, i, 0, 0, sz); } ll find(ll l, ll r, ll x, ll lx, ll rx){ if(lx >= r or rx <= l) return 0LL; if(lx >= l and rx <= r) return v[x]; ll mid = lx + (rx - lx) / 2; return max(find(l, r, 2 * x + 1, lx, mid), find(l, r, 2 * x + 2, mid, rx)); } ll find(ll l, ll r){ return find(l, r, 0, 0, sz); } }; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> mx; for(int i = 0; i < n; i++){ cin >> a[i]; b[i] = {a[i], i}; } segtree s; s.init(); sort(b, b + n); for(int i = 0; i < n; i++){ if(b[i].first != b[i - 1].first){ for(auto it : vv) s.set(dp[it], it); vv.clear(); } dp[b[i].second] = s.find(0, b[i].second) + 1; vv.pb(b[i].second); } vv.clear(); s.clear(); reverse(b, b + n); for(int i = 0; i < n; i++){ if(b[i].first != b[i - 1].first){ for(auto it : vv) s.set(dp1[it], it); vv.clear(); } dp1[b[i].second] = s.find(b[i].second + 1, n) + 1; vv.pb(b[i].second); } // for(int i = 0; i < n; i++) cout << dp[i] << ' '; // cout << '\n'; // for(int i = 0; i < n; i++) cout << dp1[i] << ' '; // cout << '\n'; reverse(b, b + n); s.clear(); ll l = 0,ans = 0; for(int i = 0; i < n; i++){ while(l < n and b[l].first < b[i].first + mx){ s.set(dp[b[l].second], b[l].second); l++; } ans = max(ans, dp1[b[i].second] + s.find(0, b[i].second)); } s.clear(); reverse(b, b + n); l = n - 1; for(int i = 0; i < n; i++){ if(l >= 0) while(b[l].first > b[i].first - mx){ s.set(dp1[b[l].second], b[l].second); l--; if(l < 0) break; } ans = max(ans, dp[b[i].second] + s.find(b[i].second + 1, n)); } 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...