# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
984391 | Mr_Husanboy | Snowball (JOI21_ho_t2) | C++17 | 109 ms | 15564 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.
//#pragma GCC optimize("Ofast,O3")
//#pragma GCC target("avx,avx2")
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debugger.cpp"
#else
#define debug(...)
#endif
template<class T>
int len(T &a){
return a.size();
}
using ll = long long;
using pii = pair<int,int>;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
string fileio = "";
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int MOD = 1e9 + 7;
const int inf = 1e9;
const ll infl = 1e18;
const int maxn = 1e5 + 1;
void solve(){
ll n, q; cin >> n >> q;
vector<ll> a(n);
for(int i = 0; i < n; i ++) cin >> a[i];
vector<ll> nq;
nq.push_back(0);
ll s = 0;
ll mn = 0, mx = 0;
for(int i = 1; i <= q; i ++){
ll x; cin >> x;
s += x;
if(s > mx) nq.push_back(s - mx), mx = s;
if(s < mn) nq.push_back(s - mn), mn = s;
}
q = len(nq);
vector<ll> lef(len(nq)), rig(len(nq)), pref(q);
for(int i = 1; i < len(nq); i ++){
lef[i] = lef[i - 1] + min(0ll, nq[i]);
rig[i] = rig[i - 1] + max(0ll, nq[i]);
pref[i] = pref[i - 1] + llabs(nq[i]);
}
debug(nq);
for(int i = 0; i < n; i ++){
ll lb = -infl, rb = infl;
if(i) lb = a[i - 1] - a[i];
if(i + 1 < n) rb = a[i + 1] - a[i];
//debug(lb, rb);
auto it = upper_bound(all(pref), rb) - pref.begin();
auto it2 = upper_bound(all(pref), -lb) - pref.begin();
//debug(it);
ll ans = rig[it - 1];
rb -= pref[it - 1];
if(it != q && nq[it] > 0) ans += rb;
ans += -lef[it2 - 1];
lb += pref[it2 - 1];
if(it2 != q && nq[it2] < 0) ans += -lb;
cout << ans << ' ';
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
auto start = chrono::high_resolution_clock::now();
#ifndef LOCAL
if(fileio.size()){
freopen((fileio + ".in").c_str(), "r", stdin);
freopen((fileio + ".out").c_str(), "w", stdout);
}
#endif
int testcases = 1;
#ifdef Tests
cin >> testcases;
#endif
while(testcases --){
solve();
if(testcases) cout << '\n';
#ifdef LOCAL
else cout << '\n';
cout << "_________________________" << endl;
#endif
}
#ifdef LOCAL
auto duration = chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - start);
cout << "time: " << (double)duration.count()/1000.0 << " milliseconds" << endl;
#endif
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |