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>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<(x.F)<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
#define int ll
const int N=2e5+99,inf=1e18,S=2;
int n,m,prt,ans=inf,a[N],l[N],t[N],r[N],cost[N],dist[N],seg[S][N<<1];
vector<int> g[N];
vector<pair<int,int> > v[S];
int merge(int u,int v,int s){
if(s==0){
if(n==max(u,v)) return u+v-n;
if(t[u]<t[v]) return u;
return v;
}
if(n==max(u,v)) return u+v-n;
if(t[u]>t[v]) return u;
return v;
}
void build(int s){
f(i,0,n){
seg[s][i+n]=v[s][i].S;
}
f_(i,n-1,1){
seg[s][i]=merge(seg[s][i<<1],seg[s][i<<1|1],s);
}
}
int get(int l, int r,int s){
int res=n;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l&1){
res=merge(res,seg[s][l++],s);
}
if (r&1){
res=merge(res,seg[s][--r],s);
}
}
return res;
}
void del(int x){
int id;
id=lower_bound(all(v[0]),mp(l[x]-t[x],x))-v[0].begin()+n;
seg[0][id]=n;
while(id>1){
seg[0][id>>1]=merge(seg[0][id],seg[0][id^1],0);
id>>=1;
}
id=lower_bound(all(v[1]),mp(l[x]+t[x],x))-v[1].begin()+n;
seg[1][id]=n;
while(id>1){
seg[1][id>>1]=merge(seg[1][id],seg[1][id^1],1);
id>>=1;
}
}
void bfs(){
set<pair<int,int> > s;
f(i,0,n){
if(l[i]==0){
del(i);
dist[i]=cost[i];
s.insert({dist[i],i});
}
}
while(s.size()){
int adj,u=(*s.begin()).S;
s.erase(*s.begin());
//cout<<u<<" -> "<<dist[u]<<endl;
if(r[u]==m) minm(ans,dist[u]);
prt=lower_bound(all(v[0]),mp(r[u]-t[u],inf))-v[0].begin();
while(get(0,prt,0)<n && t[get(0,prt,0)]<=t[u]){
adj=get(0,prt,0);
del(adj);
dist[adj]=dist[u]+cost[adj];
s.insert({dist[adj],adj});
}
prt=lower_bound(all(v[1]),mp(r[u]+t[u],inf))-v[1].begin();
while(get(0,prt,1)<n && t[u]<=t[get(0,prt,1)]){
adj=get(0,prt,1);
del(adj);
dist[adj]=dist[u]+cost[adj];
s.insert({dist[adj],adj});
}
}
}
main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>m>>n;
fill(dist,dist+N,inf);
f(i,0,n){
cin>>t[i]>>l[i]>>r[i]>>cost[i]; l[i]--;
v[0].pb({l[i]-t[i],i});
v[1].pb({l[i]+t[i],i});
}
sort(all(v[0]));
sort(all(v[1]));
build(0);
build(1);
bfs();
cout<<(ans==inf ? -1 : ans);
}
Compilation message (stderr)
treatment.cpp:106:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
106 | main(){
| ^~~~
# | 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... |