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 FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x,y) (lower_bound(all(x),y)-x.begin())
#define ubd(x,y) (upper_bound(all(x),y)-x.begin())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }
typedef long long ll;
typedef long double ld;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
typedef pair<ll,ll>pi; typedef pair<ll,pi>spi; typedef pair<pi,pi>dpi;
#define LLINF ((long long)1e18)
#define INF int(1e9+1e6)
#define MAXN (100006)
ll N,n,C[MAXN],dist[MAXN];
spi A[MAXN];
vector<int> d;
vector<int> v; // fill with the indexes u want to push
bitset<MAXN>done;
struct node{
int s,e,m;
priority_queue<pi,vector<pi>,greater<pi>> one, two;
node*l,*r;
node(int S,int E) {
s=S,e=E,m=(s+e)>>1;
while(one.size())one.pop();
while(two.size())two.pop();
if(s^e)l=new node(s,m),r=new node(m+1,e);
}
void update(int x,int ind){
one.emplace(A[ind].f+A[ind].s.f-1, ind);
two.emplace(A[ind].s.f-A[ind].f-1, ind);
if(s==e)return;
if(x>m)r->update(x,ind);
else l->update(x,ind);
}
void rmq(int x,int y,ll val,int co){
if(x>y)return;
if(s==x&&e==y){
if(co==1){
while(one.size()){
if(done[one.top().s]){one.pop();continue;}
if(val<one.top().f)break;
ll x=one.top().s; one.pop();
done[x]=1;
v.pb(x);
}
}else{
while(two.size()){
if(done[two.top().s]){two.pop();continue;}
if(val<two.top().f)break;
ll x=two.top().s; two.pop();
done[x]=1;
v.pb(x);
}
}
return;
}
if(x>m)return r->rmq(x,y,val,co);
else if(y<=m)return l->rmq(x,y,val,co);
else return l->rmq(x,m,val,co),r->rmq(m+1,y,val,co);
}
}*seg;
int main(){
FAST
cin>>N>>n;
FOR(i,1,n)cin>>A[i].f>>A[i].s.f>>A[i].s.s>>C[i],d.pb(A[i].f);
sort(all(d)),d.resize(unique(all(d))-d.begin());
seg=new node(0,siz(d)-1);
FOR(i,1,n)seg->update(lbd(d,A[i].f),i);
mmst(dist,-1);
priority_queue<pi,vector<pi>,greater<pi>> pq;
FOR(i,1,n)if(A[i].s.f==1)dist[i]=C[i],pq.emplace(dist[i],i),done[i]=1;
while(pq.size()){
ll x=pq.top().s,di=pq.top().f;
pq.pop();
if(dist[x]^di) assert(0);
v.clear();
seg->rmq(0,lbd(d,A[x].f),A[x].s.s-A[x].f,2); // if T1 >= T2
for(auto i:v) {assert(done[i]);
dist[i]=di+C[i],pq.emplace(dist[i],i);
}
v.clear();
seg->rmq(lbd(d,A[x].f)+1,siz(d)-1,A[x].f+A[x].s.s,1); // if T1 < T2
for(auto i:v){assert(done[i]);
dist[i]=di+C[i],pq.emplace(dist[i],i);
}
}
ll ans=LLINF;
FOR(i,1,n)if(A[i].s.s==N&&(~dist[i]))ans=min(ans,dist[i]);
if(ans==LLINF)ans=-1;
cout<<ans<<'\n';
}
# | 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... |