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 ll int
#define pll pair<long long, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
#define sz(a) ((ll)(a).size())
using namespace std;
const ll maxn=100005;
const long long inf=1e18;
struct seg
{
ll id, type, x, y;
seg(){}
seg(ll id, ll type, ll x, ll y): id(id), type(type), x(x), y(y){}
bool operator < (const seg &o) const
{
if (x!=o.x) return x<o.x;
if (type!=o.type) return type<o.type;
return y<o.y;
}
};
vector <seg> p;
vector <ll> A[maxn*40];
long long dis[maxn*40];
ll x[maxn], l[maxn], r[maxn], w[maxn];
namespace SegTree
{
ll n, crr, St[maxn*4]={};
void init(ll sz, ll m) {n=sz, crr=m;}
void update(ll i, ll Start, ll End, ll idx, ll val)
{
if (Start>idx || End<idx) return;
if (Start==End) {A[val].pb(St[i]), St[i]=val; return;}
ll mid=(Start+End)/2;
A[++crr].pb(St[i]), St[i]=crr;
if (idx<=mid) update(i*2, Start, mid, idx, val);
else update(i*2+1, mid+1, End, idx, val);
A[St[i]].pb(St[i*2]), A[St[i]].pb(St[i*2+1]);
}
void update(ll idx, ll val){update(1, 1, n, idx, val);}
void query(ll i, ll Start, ll End, ll l, ll r, ll val)
{
if (Start>r || End<l) return;
if (Start>=l && End<=r) {A[val].pb(St[i]); return;}
ll mid=(Start+End)/2;
if (mid>=l) query(i*2, Start, mid, l, r, val);
if (mid+1<=r) query(i*2+1, mid+1, End, l, r, val);
}
void query(ll l, ll r, ll val){query(1, 1, n, l, r, val);}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n, m; cin >> n >> m;
for (ll i=1; i<=m; i++)
{
cin >> x[i] >> l[i] >> r[i] >> w[i];
p.emplace_back(i, 0, x[i]+l[i], l[i]-x[i]);
p.emplace_back(i, 1, x[i]+r[i]+1, r[i]+1-x[i]);
}
vector <ll> cx, cy;
for (seg &s:p) cx.pb(s.x), cy.pb(s.y);
sort(cx.begin(), cx.end());
cx.resize(unique(cx.begin(), cx.end())-cx.begin());
sort(cy.begin(), cy.end());
cy.resize(unique(cy.begin(), cy.end())-cy.begin());
for (seg &s: p)
{
s.x=lower_bound(cx.begin(), cx.end(), s.x)-cx.begin()+1;
s.y=lower_bound(cy.begin(), cy.end(), s.y)-cy.begin()+1;
}
sort(p.begin(), p.end());
SegTree::init(sz(cy), m);
for (seg &s: p)
{
ll id=s.id, t=s.type, p=s.y;
if (!t) SegTree::update(p, id);
else SegTree::query(1, p, id);
}
ll k=SegTree::crr;
priority_queue <pll, vector <pll>, greater<pll>> q;
for (ll i=1; i<=k; i++)
{
if (i<=m && l[i]==1) dis[i]=w[i], q.push({dis[i], i});
else dis[i]=inf;
}
while (!q.empty())
{
ll u=q.top().se, disu=q.top().fi; q.pop();
if (dis[u]!=disu) continue;
for (ll v:A[u])
if (dis[v]>disu+(v<=m?w[v]:0))
dis[v]=disu+(v<=m?w[v]:0), q.push({dis[v], v});
}
long long ans=inf;
for (ll i=1; i<=m; i++)
if (r[i]==n) ans=min(ans, dis[i]);
if (ans==inf) cout << "-1";
else cout << 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... |