Submission #814166

#TimeUsernameProblemLanguageResultExecution timeMemory
814166MohamedAhmed04Snowball (JOI21_ho_t2)C++14
100 / 100
480 ms15328 KiB
#include <bits/stdc++.h> using namespace std ; const int MAX = 2e5 + 10 ; long long a[MAX] , b[MAX] ; int n , q ; long long pref[MAX] , mxpref[MAX] , mnpref[MAX] ; bool check(int i , long long x) { int l = 1 , r = q ; int idx = r+1 ; while(l <= r) { int mid = (l + r) >> 1 ; if(mxpref[mid] >= x - a[i]) idx = mid , r = mid-1 ; else l = mid+1 ; } if(idx <= q && a[i+1] + mnpref[idx] >= x) return true ; else return false ; } int main() { ios_base::sync_with_stdio(0) ; cin.tie(0) ; cin>>n>>q ; for(int i = 1 ; i <= n ; ++i) cin>>a[i] ; for(int i = 1 ; i <= q ; ++i) cin>>b[i] ; mnpref[0] = mxpref[0] = 0 ; for(int i = 1 ; i <= q ; ++i) { pref[i] = pref[i-1] + b[i] ; mxpref[i] = max(mxpref[i-1] , pref[i]) ; mnpref[i] = min(mnpref[i-1] , pref[i]) ; } a[0] = -2e18 , a[n+1] = 2e18 ; long long prv = -2e18 ; for(int i = 1 ; i <= n ; ++i) { long long ans = max(0ll , min(a[i] - prv , mnpref[q] * -1)) ; long long l = a[i] , r = a[i+1] ; long long last = a[i] ; while(l <= r) { long long mid = (l + r) >> 1ll ; if(check(i , mid)) last = mid , l = mid+1ll ; else r = mid-1ll ; } ans += last - a[i] ; prv = last ; cout<<ans<<"\n" ; } return 0 ; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...