# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1099159 | Lakshya108 | Snowball (JOI21_ho_t2) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// https://oj.uz/problem/view/JOI21_ho_t2
// OI\JOI21\JOI21-Snowball.pdf
#include <bits/stdc++.h>
using namespace std;
// Macros
#define pb push_back
#define pf push_front
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define up(v) upper_bound(v)
#define low(v) lower_bound(v)
// #define rep(i, x, n) for(int i = x; i < n; ++i)
// #define rrep(i, x, n) for(int i = x; i >= n; --i)
// Read and Print
#define read(a, n) for(ll i = 0; i < n; ++i) cin >> a[i];
#define print(a, n) for(ll i = 0; i < n; ++i){ cout << a[i] << " ";} cout << "\n";
#define endl "\n"
#define sp " "
// Typedefs
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pii;
typedef vector <ll> vi;
typedef vector <vector <ll>> vvi;
using vec = vector<int>;
// Constants
const ll mxn = 1e6 + 5;
const ll mod = 1e9 + 7;
// Solve
const ll N = 1e18;
void solve() {
ll n, ll q;
vi a(n+2);
a[0] = -N;
a[n+1] = N;
for(int i = 1; i<=n; i++) cin>>a[i];
vi m(q+1), pref(q+1);
map<int, int> mp;
mp[0] = 0;
for(int i = 1; i<q; i++){
cin>>m[i];
prep[i] += pref[i-1] + m[i];
if(pref[i]>0){
auto it = mp.up(pref[i]);
if(it==mp.end()) mp[pref[i]] = i;
}
else{
auto it = mp.low(pref[i]);
if(it==mp.begin()) mp[pref[i]] = i;
}
}
for(int i = 1; i<=n; i++){
int left = a[i], right = a[i];
int l = a[i-1], r = a[i];
while(r-l>1){
int mid = (l+r)/2;
int x = mid - a[i];
int y = mid - a[i-1];
auto it1 = mp.up(x);
if(it1 == mp.begin()){
l = mid;
continue;
}
it1--;
auto it2 = mp.low(y);
if(it2 == mp.end()){
r = mid;
continue;
}
if((it1->ss)<(it2->ss)) r=mid;
else l = mid;
}
auto it1 = mp.up(r-1-a[i]);
auto it2 = mp.low(r-a[i-1]);
if(it1!=mp.begin()&&(it2==mp.end()||((--it1)->ss < it2->ss))) r--;
left = r;
l = a[i-1];
r = a[i];
while(r-l>1){
int mid = (l+r)/2;
int x = mid - a[i];
int y = mid - a[i+1];
auto it1 = mp.low(x);
if(it1 == mp.end()){
r = mid;
continue;
}
auto it2 = mp.up(y);
if(it2 == mp.begin()){
l = mid;
continue;
}
it2--;
if((it1->ss)<(it2->ss)) l=mid;
else r = mid;
}
auto it1 = mp.low(l-1-a[i]);
auto it2 = mp.up(l-a[i-1]);
if(it1!=mp.end()&&(it2==mp.end()||(it1->ss < (--it2)->ss))) l++;
right = l;
}
cout<<right - left <<endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
solve();
return 0;
}