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>
#include "meetings.h"
using namespace std;
using lint = long long;
using pi = pair<int, int>;
const int MAXN = 750005;
const int MAXT = 2100000;
const int inf = 1e9 + 10;
struct seg{
int lim;
pi tree[MAXT];
void init(vector<int> &v){
for(lim = 1; lim <= v.size(); lim <<= 1);
fill(tree, tree + MAXT, pi(-1e9, 0));
for(int i=0; i<v.size(); i++) tree[i + lim] = pi(v[i], i);
for(int i=lim-1; i; i--) tree[i] = max(tree[2*i], tree[2*i+1]);
}
pi query(int s, int e){
s += lim;
e += lim;
pi ret(-1e9, 0);
while(s < e){
if(s%2 == 1) ret = max(ret, tree[s++]);
if(e%2 == 0) ret = max(ret, tree[e--]);
s >>= 1;
e >>= 1;
}
if(s == e) ret = max(ret, tree[s]);
return ret;
}
}seg;
struct node{
int s, e, rep; // node range
int lp, rp; // pointers
int h; // height
}tr[MAXN];
int ptr, loc[MAXN], parL[MAXN], parR[MAXN];
lint dp[MAXN], lgo[MAXN], rgo[MAXN];
lint L_intercept[MAXN], R_intercept[MAXN];
void make_tree(int s, int e, int v){
int h, m;
tie(h, m) = seg.query(s, e);
loc[m] = v;
tr[v] = {s, e, m, -1, -1, h};
if(s <= m - 1){
ptr++;
tr[v].lp = ptr;
make_tree(s, m-1, tr[v].lp);
}
if(m + 1 <= e){
ptr++;
tr[v].rp = ptr;
make_tree(m+1, e, tr[v].rp);
}
}
lint go_left(int pos, int root){
int L = pos;
int v = loc[pos];
root = loc[root];
lint ret = 1e18;
while(v != root){
ret = min(ret, 1ll * tr[v].h * -L + L_intercept[v]);
v = parL[v];
}
ret -= lgo[tr[root].lp];
// printf("go_left %d -> %d = %lld\n", L, root, ret);
return ret;
}
lint go_right(int pos, int root){
int R = pos;
int v = loc[pos];
root = loc[root];
lint ret = 1e18;
while(v != root){
ret = min(ret, 1ll * tr[v].h * R + R_intercept[v]);
v = parR[v];
}
ret -= rgo[tr[root].rp];
// printf("go_right %d -> %d = %lld\n", R, root, ret);
return ret;
}
vector<lint> minimum_costs(vector<int> H, vector<int> L, vector<int> R){
int n = H.size();
int q = L.size();
seg.init(H);
make_tree(0, n-1, 0);
for(int i=ptr; i>=0; i--){
dp[i] = 1ll * H[tr[i].rep] * (tr[i].e - tr[i].s + 1);
if(~tr[i].lp){
dp[i] = min(dp[i], dp[tr[i].lp] + 1ll * H[tr[i].rep] * (tr[i].e - tr[i].rep + 1));
}
if(~tr[i].rp){
dp[i] = min(dp[i], dp[tr[i].rp] + 1ll * H[tr[i].rep] * (tr[i].rep - tr[i].s + 1));
}
}
for(int i=0; i<=ptr; i++){
if(tr[i].s - 1 >= 0) parR[i] = loc[tr[i].s - 1];
else parR[i] = -1;
if(tr[i].e + 1 < n) parL[i] = loc[tr[i].e + 1];
else parL[i] = -1;
if(~tr[i].lp){
lgo[tr[i].lp] = lgo[i] + 1ll * H[tr[i].rep] * (tr[i].e - tr[i].rep + 1);
rgo[tr[i].lp] = rgo[i];
}
if(~tr[i].rp){
lgo[tr[i].rp] = lgo[i];
rgo[tr[i].rp] = rgo[i] + 1ll * H[tr[i].rep] * (tr[i].rep - tr[i].s + 1);
}
L_intercept[i] = lgo[i] + 1ll * tr[i].h * (tr[i].e + 1);
if(~tr[i].rp){
L_intercept[i] = min(L_intercept[i], lgo[i] + 1ll * tr[i].h * (tr[i].rep + 1) + dp[tr[i].rp]);
}
R_intercept[i] = rgo[i] + 1ll * tr[i].h * (1 - tr[i].s);
if(~tr[i].lp){
R_intercept[i] = min(R_intercept[i], rgo[i] + 1ll * tr[i].h * (1 - tr[i].rep) + dp[tr[i].lp]);
}
}
vector<lint> ret(q);
for(int i=0; i<q; i++){
pi p = seg.query(L[i], R[i]);
ret[i] = min({1ll * (R[i] - L[i] + 1) * p.first,
go_left(L[i], p.second) + 1ll * (R[i] - p.second + 1) * p.first,
go_right(R[i], p.second) + 1ll * (p.second - L[i] + 1) * p.first});
}
return ret;
}
Compilation message (stderr)
meetings.cpp: In member function 'void seg::init(std::vector<int>&)':
meetings.cpp:14:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(lim = 1; lim <= v.size(); lim <<= 1);
~~~~^~~~~~~~~~~
meetings.cpp:16:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<v.size(); i++) tree[i + lim] = pi(v[i], i);
~^~~~~~~~~
# | 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... |