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>
#define f first
#define s second
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
using namespace std;
const int N=3e5+5;
ll dr[100001]{0},dl[100001]{0};
struct segment{
ll t[2*N];
void build(){
for(int i=0;i<2*N;i++)t[i]=1e16;
}
void upd(int i,ll amt,int sz){
i+=sz;t[i]=min(t[i],amt);
for(i>>=1;i;i>>=1){
t[i]=min(t[2*i],t[2*i+1]);
}
}
ll qr(int l,int r,int sz,ll res=1e16){
for(l+=sz,r+=sz;l<r;l>>=1,r>>=1){
if(l&1)res=min(res,t[l++]);
if(r&1)res=min(res,t[--r]);
}return res;
}
}tl,tr;
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
int m,n;cin>>m>>n;tl.build();tr.build();
int a[m],b[m],c[m],d[m];
for(int i=0;i<m;i++)cin>>a[i]>>b[i]>>c[i]>>d[i];
vector<int>co;
for(int i=0;i<m;i++)co.pb(a[i]),co.pb(b[i]),co.pb(c[i]);
sort(co.begin(),co.end());
int M=3*m;ll ans=1e16;
for(int i=0;i<m;i++){
dl[i]=dr[i]=1e16;
ll mnl,mnr;mnl=mnr=1e16;
if(a[i]==1)dl[i]=d[i],mnl=0;
if(b[i]==n)dr[i]=d[i],mnr=0;
a[i]=lower_bound(all(co),a[i])-co.begin();
b[i]=lower_bound(all(co),b[i])-co.begin();
c[i]=lower_bound(all(co),c[i])-co.begin();
ll le=tl.qr(a[i],b[i]+1,M);
ll re=tr.qr(a[i],b[i]+1,M);
dl[i]=min(dl[i],le+d[i]);
dr[i]=min(dr[i],re+d[i]);
mnl=min(mnl,le);mnr=min(mnr,re);
tl.upd(c[i],dl[i],M);
tr.upd(c[i],dr[i],M);
ans=min(ans,mnl+mnr+d[i]);
}cout<<(ans==1e16?-1: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... |