#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*70];
long long dis[maxn*70];
ll x[maxn], l[maxn], r[maxn], w[maxn];
namespace SegTree
{
ll n, crr, St[maxn*10]={};
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[++crr].pb(St[i]), St[i]=crr, A[St[i]].pb(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), A[St[i]].pb(St[i*2]);
else update(i*2+1, mid+1, End, idx, val), 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())
{
long long 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 |
1 |
Correct |
555 ms |
253652 KB |
Output is correct |
2 |
Correct |
366 ms |
251280 KB |
Output is correct |
3 |
Correct |
473 ms |
249496 KB |
Output is correct |
4 |
Correct |
479 ms |
249448 KB |
Output is correct |
5 |
Correct |
272 ms |
216648 KB |
Output is correct |
6 |
Correct |
322 ms |
224700 KB |
Output is correct |
7 |
Correct |
413 ms |
238760 KB |
Output is correct |
8 |
Correct |
177 ms |
213852 KB |
Output is correct |
9 |
Correct |
196 ms |
226176 KB |
Output is correct |
10 |
Correct |
233 ms |
240660 KB |
Output is correct |
11 |
Correct |
486 ms |
256520 KB |
Output is correct |
12 |
Correct |
512 ms |
264512 KB |
Output is correct |
13 |
Correct |
633 ms |
264476 KB |
Output is correct |
14 |
Correct |
612 ms |
264504 KB |
Output is correct |
15 |
Correct |
609 ms |
262412 KB |
Output is correct |
16 |
Correct |
604 ms |
262392 KB |
Output is correct |
17 |
Correct |
599 ms |
262344 KB |
Output is correct |
18 |
Correct |
574 ms |
261620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
169540 KB |
Output is correct |
2 |
Correct |
42 ms |
169408 KB |
Output is correct |
3 |
Correct |
39 ms |
169300 KB |
Output is correct |
4 |
Correct |
40 ms |
169308 KB |
Output is correct |
5 |
Correct |
39 ms |
169532 KB |
Output is correct |
6 |
Correct |
40 ms |
169528 KB |
Output is correct |
7 |
Correct |
38 ms |
169532 KB |
Output is correct |
8 |
Correct |
41 ms |
169308 KB |
Output is correct |
9 |
Correct |
39 ms |
169412 KB |
Output is correct |
10 |
Correct |
39 ms |
169416 KB |
Output is correct |
11 |
Correct |
41 ms |
169300 KB |
Output is correct |
12 |
Correct |
42 ms |
169532 KB |
Output is correct |
13 |
Correct |
41 ms |
169536 KB |
Output is correct |
14 |
Correct |
43 ms |
169532 KB |
Output is correct |
15 |
Correct |
42 ms |
169300 KB |
Output is correct |
16 |
Correct |
42 ms |
169308 KB |
Output is correct |
17 |
Correct |
41 ms |
169396 KB |
Output is correct |
18 |
Correct |
40 ms |
169300 KB |
Output is correct |
19 |
Correct |
39 ms |
169420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
169540 KB |
Output is correct |
2 |
Correct |
42 ms |
169408 KB |
Output is correct |
3 |
Correct |
39 ms |
169300 KB |
Output is correct |
4 |
Correct |
40 ms |
169308 KB |
Output is correct |
5 |
Correct |
39 ms |
169532 KB |
Output is correct |
6 |
Correct |
40 ms |
169528 KB |
Output is correct |
7 |
Correct |
38 ms |
169532 KB |
Output is correct |
8 |
Correct |
41 ms |
169308 KB |
Output is correct |
9 |
Correct |
39 ms |
169412 KB |
Output is correct |
10 |
Correct |
39 ms |
169416 KB |
Output is correct |
11 |
Correct |
41 ms |
169300 KB |
Output is correct |
12 |
Correct |
42 ms |
169532 KB |
Output is correct |
13 |
Correct |
41 ms |
169536 KB |
Output is correct |
14 |
Correct |
43 ms |
169532 KB |
Output is correct |
15 |
Correct |
42 ms |
169300 KB |
Output is correct |
16 |
Correct |
42 ms |
169308 KB |
Output is correct |
17 |
Correct |
41 ms |
169396 KB |
Output is correct |
18 |
Correct |
40 ms |
169300 KB |
Output is correct |
19 |
Correct |
39 ms |
169420 KB |
Output is correct |
20 |
Correct |
54 ms |
172304 KB |
Output is correct |
21 |
Correct |
55 ms |
172312 KB |
Output is correct |
22 |
Correct |
51 ms |
172388 KB |
Output is correct |
23 |
Correct |
53 ms |
172188 KB |
Output is correct |
24 |
Correct |
56 ms |
172272 KB |
Output is correct |
25 |
Correct |
58 ms |
172248 KB |
Output is correct |
26 |
Correct |
54 ms |
172152 KB |
Output is correct |
27 |
Correct |
54 ms |
172204 KB |
Output is correct |
28 |
Correct |
58 ms |
172536 KB |
Output is correct |
29 |
Correct |
54 ms |
172320 KB |
Output is correct |
30 |
Correct |
51 ms |
172244 KB |
Output is correct |
31 |
Correct |
52 ms |
172264 KB |
Output is correct |
32 |
Correct |
59 ms |
172472 KB |
Output is correct |
33 |
Correct |
57 ms |
172536 KB |
Output is correct |
34 |
Correct |
55 ms |
172492 KB |
Output is correct |
35 |
Correct |
55 ms |
172500 KB |
Output is correct |
36 |
Correct |
55 ms |
172508 KB |
Output is correct |
37 |
Correct |
59 ms |
172456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
555 ms |
253652 KB |
Output is correct |
2 |
Correct |
366 ms |
251280 KB |
Output is correct |
3 |
Correct |
473 ms |
249496 KB |
Output is correct |
4 |
Correct |
479 ms |
249448 KB |
Output is correct |
5 |
Correct |
272 ms |
216648 KB |
Output is correct |
6 |
Correct |
322 ms |
224700 KB |
Output is correct |
7 |
Correct |
413 ms |
238760 KB |
Output is correct |
8 |
Correct |
177 ms |
213852 KB |
Output is correct |
9 |
Correct |
196 ms |
226176 KB |
Output is correct |
10 |
Correct |
233 ms |
240660 KB |
Output is correct |
11 |
Correct |
486 ms |
256520 KB |
Output is correct |
12 |
Correct |
512 ms |
264512 KB |
Output is correct |
13 |
Correct |
633 ms |
264476 KB |
Output is correct |
14 |
Correct |
612 ms |
264504 KB |
Output is correct |
15 |
Correct |
609 ms |
262412 KB |
Output is correct |
16 |
Correct |
604 ms |
262392 KB |
Output is correct |
17 |
Correct |
599 ms |
262344 KB |
Output is correct |
18 |
Correct |
574 ms |
261620 KB |
Output is correct |
19 |
Correct |
42 ms |
169540 KB |
Output is correct |
20 |
Correct |
42 ms |
169408 KB |
Output is correct |
21 |
Correct |
39 ms |
169300 KB |
Output is correct |
22 |
Correct |
40 ms |
169308 KB |
Output is correct |
23 |
Correct |
39 ms |
169532 KB |
Output is correct |
24 |
Correct |
40 ms |
169528 KB |
Output is correct |
25 |
Correct |
38 ms |
169532 KB |
Output is correct |
26 |
Correct |
41 ms |
169308 KB |
Output is correct |
27 |
Correct |
39 ms |
169412 KB |
Output is correct |
28 |
Correct |
39 ms |
169416 KB |
Output is correct |
29 |
Correct |
41 ms |
169300 KB |
Output is correct |
30 |
Correct |
42 ms |
169532 KB |
Output is correct |
31 |
Correct |
41 ms |
169536 KB |
Output is correct |
32 |
Correct |
43 ms |
169532 KB |
Output is correct |
33 |
Correct |
42 ms |
169300 KB |
Output is correct |
34 |
Correct |
42 ms |
169308 KB |
Output is correct |
35 |
Correct |
41 ms |
169396 KB |
Output is correct |
36 |
Correct |
40 ms |
169300 KB |
Output is correct |
37 |
Correct |
39 ms |
169420 KB |
Output is correct |
38 |
Correct |
54 ms |
172304 KB |
Output is correct |
39 |
Correct |
55 ms |
172312 KB |
Output is correct |
40 |
Correct |
51 ms |
172388 KB |
Output is correct |
41 |
Correct |
53 ms |
172188 KB |
Output is correct |
42 |
Correct |
56 ms |
172272 KB |
Output is correct |
43 |
Correct |
58 ms |
172248 KB |
Output is correct |
44 |
Correct |
54 ms |
172152 KB |
Output is correct |
45 |
Correct |
54 ms |
172204 KB |
Output is correct |
46 |
Correct |
58 ms |
172536 KB |
Output is correct |
47 |
Correct |
54 ms |
172320 KB |
Output is correct |
48 |
Correct |
51 ms |
172244 KB |
Output is correct |
49 |
Correct |
52 ms |
172264 KB |
Output is correct |
50 |
Correct |
59 ms |
172472 KB |
Output is correct |
51 |
Correct |
57 ms |
172536 KB |
Output is correct |
52 |
Correct |
55 ms |
172492 KB |
Output is correct |
53 |
Correct |
55 ms |
172500 KB |
Output is correct |
54 |
Correct |
55 ms |
172508 KB |
Output is correct |
55 |
Correct |
59 ms |
172456 KB |
Output is correct |
56 |
Correct |
516 ms |
256400 KB |
Output is correct |
57 |
Correct |
485 ms |
256848 KB |
Output is correct |
58 |
Correct |
464 ms |
255020 KB |
Output is correct |
59 |
Correct |
444 ms |
256236 KB |
Output is correct |
60 |
Correct |
580 ms |
260988 KB |
Output is correct |
61 |
Correct |
457 ms |
255284 KB |
Output is correct |
62 |
Correct |
503 ms |
256548 KB |
Output is correct |
63 |
Correct |
463 ms |
264892 KB |
Output is correct |
64 |
Correct |
441 ms |
257680 KB |
Output is correct |
65 |
Correct |
488 ms |
259988 KB |
Output is correct |
66 |
Correct |
392 ms |
257212 KB |
Output is correct |
67 |
Correct |
596 ms |
258752 KB |
Output is correct |
68 |
Correct |
563 ms |
258740 KB |
Output is correct |
69 |
Correct |
540 ms |
259008 KB |
Output is correct |
70 |
Correct |
595 ms |
258616 KB |
Output is correct |
71 |
Correct |
557 ms |
258532 KB |
Output is correct |
72 |
Correct |
532 ms |
259416 KB |
Output is correct |
73 |
Correct |
600 ms |
262736 KB |
Output is correct |
74 |
Correct |
282 ms |
261936 KB |
Output is correct |
75 |
Correct |
269 ms |
261312 KB |
Output is correct |
76 |
Correct |
511 ms |
265308 KB |
Output is correct |
77 |
Correct |
587 ms |
265252 KB |
Output is correct |
78 |
Correct |
607 ms |
264916 KB |
Output is correct |
79 |
Correct |
606 ms |
261512 KB |
Output is correct |
80 |
Correct |
559 ms |
263548 KB |
Output is correct |
81 |
Correct |
330 ms |
261288 KB |
Output is correct |
82 |
Correct |
577 ms |
262852 KB |
Output is correct |
83 |
Correct |
578 ms |
262212 KB |
Output is correct |
84 |
Correct |
646 ms |
261864 KB |
Output is correct |
85 |
Correct |
420 ms |
261820 KB |
Output is correct |
86 |
Correct |
482 ms |
261220 KB |
Output is correct |
87 |
Correct |
425 ms |
261904 KB |
Output is correct |
88 |
Correct |
532 ms |
259596 KB |
Output is correct |
89 |
Correct |
483 ms |
261612 KB |
Output is correct |
90 |
Correct |
553 ms |
264384 KB |
Output is correct |
91 |
Correct |
482 ms |
263104 KB |
Output is correct |
92 |
Correct |
506 ms |
261560 KB |
Output is correct |
93 |
Correct |
557 ms |
261404 KB |
Output is correct |
94 |
Correct |
536 ms |
260416 KB |
Output is correct |
95 |
Correct |
520 ms |
261896 KB |
Output is correct |
96 |
Correct |
570 ms |
265252 KB |
Output is correct |
97 |
Correct |
563 ms |
265408 KB |
Output is correct |
98 |
Correct |
612 ms |
265832 KB |
Output is correct |
99 |
Correct |
569 ms |
265440 KB |
Output is correct |
100 |
Correct |
545 ms |
261424 KB |
Output is correct |
101 |
Correct |
608 ms |
265240 KB |
Output is correct |
102 |
Correct |
543 ms |
265680 KB |
Output is correct |
103 |
Correct |
443 ms |
262452 KB |
Output is correct |