CSES - Shared codeLink to this code:
https://cses.fi/paste/8b589473ba482a5115f5a2/
#include <bits/stdc++.h>
using namespace std;
int32_t main()
{
ios_base::sync_with_stdio(false); cin.tie(0); //necessary to pass time limits
int x, n;
cin >> x >> n;
set<pair<int,int>> S;
multiset<int> dist;
dist.insert(x);
S.insert({0,x});
for(int i = 0; i < n; i++)
{
int y; cin >> y;
auto it = S.lower_bound({y, y}); it--; // nearest range with left point < y
auto Q = *it;
int A = Q.first, B = Q.second;
S.erase({A,B}); // remove the big range
S.insert({A,y}), S.insert({y,B}); // create two smaller ranges A - y and y - B
dist.erase(dist.find(B-A)); // remove old dist
dist.insert(y-A), dist.insert(B-y); // insert new ones
auto it3 = dist.end(); it3--; // largest value in the dist set
cout << *it3 << " ";
}
}