#include "nile.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
struct elm
{
ll w,ca, cb;
bool operator<(const elm& other) const
{
return w < other.w;
}
};
const int tree_siz = 15;
ll cur_D = 0;
elm elms[tree_siz/2+1];
struct node
{
int l,r;
ll dp[3][3];
node()
{
rep(d1,3) rep(d2,3) dp[d1][d2] = 1e18;
}
node operator+(const node& other)
{
node res;
res.l = l;
res.r = other.r;
rep(lb,3)
{
rep(rb,3)
{
if(res.l + lb > res.r-rb)
{
continue;
}
if(res.r-rb < other.l)
{
res.dp[lb][rb] = dp[lb][r - (res.r-rb)];
continue;
}
if(res.l+lb >= other.l)
{
res.dp[lb][rb] = other.dp[(res.l+lb) - other.l][rb];
continue;
}
res.dp[lb][rb] = dp[lb][0] + other.dp[0][rb];
// 0 0
if(abs(elms[r].w - elms[r+1].w) <= cur_D) res.dp[lb][rb] = min(res.dp[lb][rb],(l + lb <= r - 1 ? dp[lb][1] : 0) + elms[r].cb + elms[r+1].cb + (other.l + 1 <= other.r-rb ? other.dp[1][rb] : 0));
// 0 1
if(other.l + 1 <=other.r -rb && abs(elms[r].w - elms[r+2].w) <= cur_D)
{
res.dp[lb][rb] = min(res.dp[lb][rb],(l + lb <= r - 1 ? dp[lb][1] : 0) + elms[r].cb + elms[r+1].ca + elms[r+2].cb + (other.l + 2 <= other.r-rb ? other.dp[2][rb] : 0));
}
// 1 0
if(r - 1 >= l + lb && abs(elms[r-1].w - elms[r+1].w) <= cur_D) res.dp[lb][rb] = min(res.dp[lb][rb],(l + lb <= r - 2 ? dp[lb][2] : 0) + elms[r-1].cb + elms[r].ca + elms[r+1].cb + (other.l + 1 <= other.r-rb ? other.dp[1][rb] : 0));
}
}
return res;
}
};
node nodes[tree_siz+1];
ll query_ans[100'001];
void upd(int v)
{
nodes[v] = nodes[v*2] + nodes[v*2+1];
if(v != 1) upd(v/2);
}
vector<long long> calculate_costs(vector<int> W, vector<int> A, vector<int> B, vector<int> E)
{
int n = siz(W);
rep(i,tree_siz/2+1) elms[i] = {(ll)1e10,0,0};
rep(i,n)
{
elms[i] = {W[i],A[i],B[i]};
}
sort(elms,elms+n);
rep2(i,tree_siz/2+1,tree_siz)
{
nodes[i].dp[0][0] = elms[i-(tree_siz/2+1)].ca;
nodes[i].l = i-(tree_siz/2+1);
nodes[i].r = i-(tree_siz/2+1);
}
for(int i = tree_siz/2; i >= 1; i--)
{
nodes[i] = nodes[i*2] + nodes[i*2+1];
}
vector<pii> events;
rep(i,n-1)
{
events.pb({abs(elms[i].w - elms[i+1].w),i});
events.pb({abs(elms[i].w - elms[i+1].w),i+1});
if(i != n-2)
{
events.pb({abs(elms[i].w - elms[i+2].w),i});
events.pb({abs(elms[i].w - elms[i+2].w),i+2});
}
}
sort(all(events));
int cur_e = 0;
vector<pii> queries;
rep(i,siz(E))
{
queries.pb({E[i],i});
}
sort(all(queries));
forall(it,queries)
{
cur_D = it.ff;
while(cur_e < siz(events) && events[cur_e].ff <= it.ff)
{
upd((tree_siz/2+1+events[cur_e].ss)/2);
cur_e++;
}
query_ans[it.ss] = nodes[1].dp[0][0];
}
vl ans;
rep(i,siz(E)) ans.pb(query_ans[i]);
return ans;
}
# | 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... |