Submission #1136101

#TimeUsernameProblemLanguageResultExecution timeMemory
1136101Zbyszek99Global Warming (CEOI18_glo)C++20
100 / 100
1439 ms51320 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define ld long double #define ull unsigned long long #define ff first #define ss second #define pii pair<int,int> #define pll pair<long long, long long> #define vi vector<int> #define vl vector<long long> #define pb push_back #define rep(i, b) for(int i = 0; i < (b); ++i) #define rep2(i,a,b) for(int i = a; i <= (b); ++i) #define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c) #define count_bits(x) __builtin_popcountll((x)) #define all(x) (x).begin(),(x).end() #define siz(x) (int)(x).size() #define forall(it,x) for(auto& it:(x)) using namespace __gnu_pbds; using namespace std; typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set; //mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());} //ll los(ll a, ll b) {return a + (mt() % (b-a+1));} const int INF = 1e9+50; const ll INF_L = 1e18+40; const ll MOD = 1e9+7; struct node { int l = 0; int r = 262'144-1; node* left; node* right; int max_val = 0; bool is_left = false; bool is_right = false; void uleft() { if(!is_left) { left = new node; left -> l = l; left -> r = (l+r)/2; } is_left = true; } void uright() { if(!is_right) { right = new node; right -> l = (l+r)/2+1; right -> r = r; } is_right = true; } int get_max(int p1, int p2) { if(p1 > p2) return 0; if(r < p1 || l > p2) return 0; if(l >= p1 && r <= p2) return max_val; if(p1 <= (l+r)/2) { uleft(); } if(p2 >= (l+r)/2+1) { uright(); } return max((is_left ? left->get_max(p1,p2) : 0),(is_right ? right->get_max(p1,p2) : 0)); } void upd_max(int p, int val) { if(l == r) { if(val == -1) { max_val = 0; } max_val = max(max_val,val); return; } if(p <= (l+r)/2) { uleft(); left -> upd_max(p,val); } else { uright(); right -> upd_max(p,val); } max_val = 0; if(is_left) { max_val = max(max_val,left -> max_val); } if(is_right) { max_val = max(max_val,right -> max_val); } } }; ll arr[200'001]; int sort_arr[200'001]; map<int,int> pom; int max_pref[200'001]; int max_suf[200'001]; node tree_; set<int> set_arr; int get_max(int l, int r) { auto itl = set_arr.lower_bound(l); auto itr = set_arr.upper_bound(r); if(itl != set_arr.end()) { l = pom[*itl]; } else { l = 200'000; } if(itr != set_arr.end()) { itr--; r = pom[*itr]; } else { r = 200'000; } return tree_.get_max(l,r); } int main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); //random_start(); int n; ll x; cin >> n >> x; rep(i,n) cin >> arr[i]; rep(i,n) { set_arr.insert(arr[i]); } set_arr.insert(0); map<int,int> m; rep(i,n) m[arr[i]] = 1; int cur = 1; forall(it,m) { pom[it.ff] = cur++; } rep(i,n) sort_arr[i] = pom[arr[i]]; rep(i,n) { int val = get_max(0,arr[i]-1)+1; max_pref[i] = val; tree_.upd_max(sort_arr[i],val); } rep(i,n) tree_.upd_max(sort_arr[i],-1); for(int i = n-1; i >= 0; i--) { int val = get_max(arr[i]+1,1e9)+1; max_suf[i] = val; tree_.upd_max(sort_arr[i],val); } rep(i,n) tree_.upd_max(sort_arr[i],-1); int ans = 0; for(int i = n-1; i >= 0; i--) { ans = max(ans,max_pref[i] + get_max(max(0LL,arr[i]-x+1),1e9+1)); tree_.upd_max(sort_arr[i],max_suf[i]); } rep(i,n) tree_.upd_max(sort_arr[i],-1); rep(i,n) { ans = max(ans,max_suf[i] + get_max(0,max(0LL,arr[i]-x-1))); tree_.upd_max(sort_arr[i],max_pref[i]); } cout << ans << "\n"; }
#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...