#include "nile.h"
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100001
#define nl '\n'
#define ff first
#define ss second
#define ll long long
#define ld long double
#define terminator main
#define pll pair<ll,ll>
#define append push_back
#define pii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define L0TA ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
ll x;
int a[MAXN];
int b[MAXN];
int w[MAXN];
int l[MAXN];
int r[MAXN];
ll pre[MAXN];
ll val[MAXN];
int MIN[MAXN];
int par[MAXN];
void get(int v){
ll ans = pre[r[v]] - pre[l[v] - 1];
if((r[v] + l[v]) % 2 == 0)
ans += min(MIN[v], min(a[l[v]] - b[l[v]], a[r[v]] - b[r[v]]));
val[v] = ans;
x += val[v];
}
int root(int v){
if(!par[v]) return v;
par[v] = root(par[v]);
return par[v];
}
void join(int v, int u){
v = root(v), u = root(u);
MIN[v] = min(MIN[v], MIN[u]);
r[v] = r[u];
x -= val[v];
x -= val[u];
par[u] = v;
get(v);
}
vector<ll> calculate_costs(vector<int> W, vector<int> A, vector<int> B, vector<int> e){
pll p;
int t, u;
vector<ll> R;
int n = A.size();
vector<pair<int, pii>> v;
for(int i = x = 0; i < n; i++)
v.append({W[i], {A[i], B[i]}});
sort(all(v));
for(int i = 0; i < n; i++){
a[i + 1] = v[i].ss.ff;
b[i + 1] = v[i].ss.ss;
w[i + 1] = v[i].ff;
}
v.clear();
for(int i = 1; i <= n; i++){
pre[i] = pre[i - 1] + b[i];
l[i] = r[i] = i;
val[i] = a[i];
MIN[i] = 1e9;
x += a[i];
if(i > 1)
v.append({w[i] - w[i - 1], {i - 1, i}});
if(i > 2)
v.append({w[i] - w[i - 2], {i - 2, i}});
}
sort(all(v));
vector<pll> ans {{0, -x}};
for(auto & [i, j] : v){
if(j.ff + 1 == j.ss)
join(j.ff, j.ss);
else{
u = j.ff + 1;
t = root(u);
MIN[t] = min(MIN[t], a[u] - b[u]);
x -= val[t];
get(t);
}
ans.append({i, -x});
}
for(auto & d : e){
p = {d, 1};
p = *(--lower_bound(all(ans), p));
R.append(-p.ss);
}
return 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |