#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
 
typedef long long ll;
 
// Global arrays for prefix minimum/maximum.
vector<ll> minp;
vector<ll> maxp;
 
// Binary search function:
// Finds the smallest t in [1, q] such that the wind–induced range 
// (maxp[t] – minp[t]) is at least (b – a).
// (If none is found, we return q, meaning “use the full effect”.)
int search(ll a, ll b, int q) {
    int low = 1;
    int high = q;
    int ans = q; // default to full wind effect if condition never met
    while(low <= high) {
        int mid = low + (high - low) / 2;
        if ((maxp[mid] - minp[mid]) >= (b - a)) {
            ans = mid;
            high = mid - 1;
        } else {
            low = mid + 1;
        }
    }
    return ans;
}
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
 
    int n, q;
    cin >> n >> q;
    vector<ll> arr(n);
    for (int i = 0; i < n; i++){
        cin >> arr[i];
    }
    sort(arr.begin(), arr.end());
 
    vector<ll> wind(q);
    vector<ll> prefix(q+1, 0);
    // Resize and initialize minp and maxp; note we use 0 as the starting value.
    minp.resize(q+1, 0);
    maxp.resize(q+1, 0);
    minp[0] = 0;
    maxp[0] = 0;
    for (int i = 0; i < q; i++){
        cin >> wind[i];
        prefix[i+1] = prefix[i] + wind[i];
        minp[i+1] = min(minp[i], prefix[i+1]);
        maxp[i+1] = max(maxp[i], prefix[i+1]);
    }
 
    // keypoints[i] = {L, R} will represent the effective interval (left/right boundaries)
    // in which snowball i collects snow.
    vector<pair<ll, ll>> keypoints(n, {0,0});
    // For the first snowball, use the full wind effect.
    keypoints[0] = { arr[0] + minp[q], arr[0] + maxp[q] };
 
    // For each subsequent snowball, determine the day t when the gap between
    // the previous and current snowball is bridged, and adjust the merging boundary.
    for (int i = 1; i < n; i++){
        int t = search(arr[i-1], arr[i], q);
        // Initially set keypoints[i] using day t.
        keypoints[i] = { arr[i] + minp[t], arr[i] + maxp[t] };
        // Adjust the common boundary between snowball i-1 and i.
        // (Note: t is at least 1, so wind[t-1] is valid.)
        if(wind[t-1] < 0) {
            // When wind on day t is blowing west, use the minimum prefix.
            keypoints[i-1].second = arr[i-1] + minp[t];
            keypoints[i].first   = arr[i]   + minp[t];
        } else {
            // Otherwise (wind blowing east), use the maximum prefix.
            keypoints[i-1].second = arr[i-1] + maxp[t];
            keypoints[i].first   = arr[i]   + maxp[t];
        }
    }
 
    // The full wind–induced range if a snowball were isolated.
    ll fullRange = maxp[q] - minp[q];
    // For each snowball, the final weight is the minimum of fullRange and the length of its effective interval.
    for (int i = 0; i < n; i++){
        ll diff = keypoints[i].second - keypoints[i].first;
        ll ans = min(fullRange, diff);
        cout << ans << "\n";
    }
 
    return 0;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |