#include "train.h"
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using pii=pair<int,int>;
#define f first
#define s second
#define eb emplace_back
#define sz(x) (int)x.size()
#define add(x,y) ((x+y)%md)
#define Add(x,y) (x=add(x,y))
#define mul(x,y) ((x*y)%md)
#define Mul(x,y) (x=mul(x,y))
template<class T> T chmn(T &x,T y){ return x=min(x,y); }
template<class T> T chmx(T &x,T y){ return x=max(x,y); }
const int inf=1e9;
const ll linf=1e18;
const ll md=1e9+7;
struct segment{
struct node{
node *l,*r;
int x;
node(int x=0):x(x),l(l),r(r){}
};
using pnode=node*;
vector<pnode> rt;
int l0,r0;
void init(int n,int l,int r){
rt.resize(n+5);
l0=l,r0=r;
build(rt[0],l0,r0);
}
void build(pnode &t,int il,int ir){
t=new node();
if(il==ir) return;
int mid=il+ir>>1;
build(t->l,il,mid), build(t->r,mid+1,ir);
}
void upd(int t0,int t1,int id,int x){ upd(rt[t0],rt[t1],l0,r0,id,x); }
void upd(pnode t0,pnode &t1,int il,int ir,int id,int x){
t1=new node(*t0);
t1->x+=x;
if(il==ir) return;
int mid=il+ir>>1;
if(id<=mid) upd(t0->l,t1->l,il,mid,id,x);
else upd(t0->r,t1->r,mid+1,ir,id,x);
}
int qr(int t,int l,int r){ return qr(rt[t],l0,r0,l,r); }
int qr(pnode t,int il,int ir,int l,int r){
if(il>r||ir<l) return 0;
if(l<=il&&ir<=r) return t->x;
int mid=il+ir>>1;
return qr(t->l,il,mid,l,r)+qr(t->r,mid+1,ir,l,r);
}
}t;
vector<pair<ll,int>> dp[100005];
int L[100005],R[100005],Lcmp[100005];
long long solve(int N, int M, int W, std::vector<int> T, std::vector<int> X, std::vector<int> Y,
std::vector<int> A, std::vector<int> B, std::vector<int> C, std::vector<int> L_,
std::vector<int> R_) {
t.init(W+5,0,W-1);
vector<int> v(W);
iota(v.begin(),v.end(),0);
sort(v.begin(),v.end(),[&](const int &l,const int &r){ return R_[l]<R_[r]; });
for(int i=0;i<W;++i) Lcmp[i]=L_[i];
sort(Lcmp,Lcmp+W);
for(int i=0;i<W;++i){
L[i]=L_[v[i]], R[i]=R_[v[i]];
t.upd(i,i+1,lower_bound(Lcmp,Lcmp+W,L[i])-Lcmp,1);
}
dp[0].eb(0,-1);
v.resize(N);
iota(v.begin(),v.end(),0);
sort(v.begin(),v.end(),[&](const int &l,const int &r){ return B[l]==B[r] ? A[l]<A[r] : B[l]<B[r]; });
for(auto &i:v){
int l=-1,r=sz(dp[X[i]])-1;
while(l<r){
int mid=l+r+1>>1;
if(dp[X[i]][mid].s>A[i]) r=mid-1;
else l=mid;
}
if(l!=-1){
l=0;
int ver=lower_bound(R,R+W,A[i])-R;
while(l<r){
int mid=l+r>>1;
int id=upper_bound(Lcmp,Lcmp+W,dp[X[i]][mid].s)-Lcmp;
int id2=upper_bound(Lcmp,Lcmp+W,dp[X[i]][mid+1].s)-Lcmp;
if(dp[X[i]][mid].f+(ll)T[X[i]]*t.qr(ver,id,W) < dp[X[i]][mid+1].f+(ll)T[X[i]]*t.qr(ver,id2,W)) r=mid;
else l=mid+1;
}
int id=upper_bound(Lcmp,Lcmp+W,dp[X[i]][l].s)-Lcmp;
ll cost = dp[X[i]][l].f+(ll)T[X[i]]*t.qr(ver,id,W);
dp[Y[i]].eb(C[i]+cost,B[i]);
}
}
ll res=1e18;
for(auto &[c,b]:dp[N-1]){
int id=upper_bound(Lcmp,Lcmp+W,b)-Lcmp;
chmn(res,c+(ll)T[N-1]*t.qr(W,id,W));
}
return res;
}
# | 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... |