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("O3")
//#include "meetings.h"
#include <bits/stdc++.h>
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%I64d", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%I64d\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const ll INF = 1e18 + 7;
const int MAXN = 75e4 + 5;
const double eps = 1e-9;
int n, q;
vi qryL, qryR;
pii sp[20][MAXN];
vl ans;
vi adj[MAXN];
pll seg[MAXN*4], lz[MAXN*4];
int qry(int l, int r) {
int x = 31 - __builtin_clz(r-l+1);
return max(sp[x][l],sp[x][r-(1<<x)+1]).se;
}
void prop(ll l, ll r, int k) {
if (l != r) {
if (lz[k].fi) {
lz[k*2] = mp(lz[k].fi, 0ll);
lz[k*2+1] = mp(lz[k].fi, 0ll);
}
lz[k*2].se += lz[k].se;
lz[k*2+1].se += lz[k].se;
}
if (lz[k].fi) seg[k] = mp(l*lz[k].fi,r*lz[k].fi);
seg[k].fi += lz[k].se;
seg[k].se += lz[k].se;
lz[k] = mp(0ll,0ll);
}
void upd(int l, int r, int k, int a, int b, pll v, bool fl) {
if (lz[k] != mp(0ll,0ll)) prop(l,r,k);
if (r < a || b < l) return;
if (a <= l && r <= b) {
if (fl || r * v.fi + v.se < seg[k].se) {
lz[k] = v;
prop(l,r,k);
return;
}
if (seg[k].fi <= l * v.fi + v.se)
return;
}
int m = (l+r) / 2;
upd(l,m,k*2,a,b,v,fl);
upd(m+1,r,k*2+1,a,b,v,fl);
seg[k] = mp(seg[k*2].fi, seg[k*2+1].se);
}
ll qrySeg(int l, int r, int k, int a) {
if (lz[k] != mp(0ll,0ll)) prop(l,r,k);
if (r < a || a < l) return -INF;
if (a <= l && r <= a) return seg[k].fi;
int m = (l+r) / 2;
return max(qrySeg(l,m,k*2,a),qrySeg(m+1,r,k*2+1,a));
}
int solve(int l, int r) {
if (r < l) return -1;
int ind = abs(qry(l,r));
int l2 = solve(l,ind-1), r2 = solve(ind+1,r);
upd(0, n-1, 1, ind, ind, mp(1, -ind), 1);
for (int v: adj[ind])
ans[v] = min(ans[v], qrySeg(0,n-1,1,qryR[v]) + (ind-qryL[v]+1ll)*sp[0][ind].fi);
upd(0, n-1, 1, ind, r, mp(0, (ind-l+1ll) * sp[0][ind].fi), 1);
upd(0, n-1, 1, ind, r, mp(sp[0][ind].fi, (l2 != -1 ? qrySeg(0,n-1,1,ind-1) : 0ll) - (ind-1ll) * sp[0][ind].fi), 0);
return ind;
}
vl minimum_costs(vi h, vi l, vi r) {
n = h.size(), q = l.size();
for (int i = 0; i < q; i++) {
qryL.pb(l[i]);
qryR.pb(r[i]);
ans.pb(INF);
}
for (int i = 0; i < n; i++)
sp[0][i] = mp(h[i], i);
for (int i = 1; i < 20; i++)
for (int j = 0; j <= n-(1<<i); j++)
sp[i][j] = max(sp[i-1][j], sp[i-1][j+(1<<(i-1))]);
for (int i = 0; i < q; i++)
adj[qry(qryL[i],qryR[i])].pb(i);
solve(0, n-1);
reverse(adj, adj + n);
for (int i = 0; i < q; i++)
tie(qryL[i], qryR[i]) = mp(n-qryR[i]-1,n-qryL[i]-1);
for (int i = 0; i < n; i++)
sp[0][i] = mp(h[n-i-1], -i);
for (int i = 1; i < 20; i++)
for (int j = 0; j <= n-(1<<i); j++)
sp[i][j] = max(sp[i-1][j], sp[i-1][j+(1<<(i-1))]);
solve(0,n-1);
return ans;
}
Compilation message (stderr)
meetings.cpp: In function 'int solve(int, int)':
meetings.cpp:88:27: warning: unused variable 'r2' [-Wunused-variable]
int l2 = solve(l,ind-1), r2 = solve(ind+1,r);
^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |