# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
765779 | khshg | Highway Tolls (IOI18_highway) | 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.
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<ld, ld>;
#define mp make_pair
#define ff first
#define ss second
#define ar array
template<class T> using V = vector<T>;
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<ld>;
using vs = V<str>;
using vpi = V<pi>;
using vpl = V<pl>;
using vpd = V<pd>;
#define sz(x) (int)((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for(int i = (b) - 1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define rep(a) F0R(_, a)
#define trav(a, x) for(auto& a : x)
template<class T> bool ckmin(T& a, const T& b) { return (b < a ? a = b, 1 : 0); }
template<class T> bool ckmax(T& a, const T& b) { return (b > a ? a = b, 1 : 0); }
const ll INF = 0x3f3f3f3f3f3f3f3f;
vl minimum_costs(vi H, vi L, vi R) {
int N = sz(H);
V<vpl> toL(N), toR(N);
F0R(i, N) {
if(i) toL[i] = toL[i - 1];
while(sz(toL[i]) && H[toL[i].bk.ff] <= H[i]) {
toL[i].pop_back();
}
toL[i].eb(i, 0);
}
R0F(i, N) {
if(i + 1 < N) toR[i] = toR[i + 1];
while(sz(toR[i]) && H[toR[i].bk.ff] <= H[i]) {
toR[i].pop_back();
}
toR[i].eb(i, 0);
}
F0R(i, N) {
reverse(all(toL[i]));
toL[i].eb(-1, 0);
F0R(j, sz(toL[i]) - 1) {
toL[i][j].ss = (toL[i][j].ff - toL[i][j + 1].ff) * H[toL[i][j].ff] + (j ? toL[i][j - 1].ss : 0);
}
reverse(all(toL[i]));
toR[i].eb(N, 0);
F0R(j, sz(toR[i]) - 1) {
toR[i][j].ss = (toR[i][j + 1].ss - toR[i][j].ss) * H[toR[i][j].ff] + (j ? toR[i][j - 1].ss : 0);
}
}
int Q = sz(L);
vl ans(Q, INF);
F0R(q, Q) {
FOR(i, L[q], R[q] + 1) {
ll x = lb(all(toR[i]), mp((ll)R[q], 0LL)) - bg(toR[i]);
ll rsd = (x > 0 ? toR[i][x - 1].ss : 0LL) + 1LL * (R[q] - x + 1) * H[toR[i][x].ff];
ll y = lb(all(toL[i]), mp((ll)L[q], 0LL)) - bg(toL[i]);
ll lsd = (y + 1 < sz(toL[i]) ? toL[i][y + 1].ss : 0LL) + (toL[i][y].ff - i + 1) * H[toL[i][y].ff];
ckmin(ans[q], lsd + rsd - (ll)H[i]);
}
}
return ans;
}