#include <bits/stdc++.h>
using namespace std;
#define pb push_back
using ll = long long;
const int mxM = (int)1e5+2;
const ll LINF = (ll)1e18;
using pii = pair<ll,int>;
int n, m;
ll dis[mxM], seg[mxM*2];
struct Project{ int t, l, r, c; } a[mxM];
priority_queue<pii,vector<pii>,greater<pii>> pq;
void upd(int x, int p=0, int l=0, int r=m-1){
if(l==r){ seg[p] = a[l].l+a[l].t; return; }
int mid = (l+r)/2; int rp = p+2*(mid-l+1);
if(x<=mid) upd(x,p+1,l,mid);
else upd(x,rp,mid+1,r);
seg[p] = min(seg[p+1],seg[rp]);
}
void query(int i, int j, ll k, int p=0, int l=0, int r=m-1){
if(i<a[l].l-a[l].t or j < seg[p]) return;
if(l==r){
if(dis[l]==LINF) dis[l] = k+a[l].c, pq.push({dis[l],l});
seg[p]=LINF; return;
}
int mid = (l+r)/2; int rp = p+2*(mid-l+1);
query(i,j,k,p+1,l,mid); query(i,j,k,rp,mid+1,r);
seg[p] = min(seg[p+1],seg[rp]);
}
int32_t main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
for(int i = 0; i < m; i++)
cin >> a[i].t >> a[i].l >> a[i].r >> a[i].c;
sort(a,a+m,[&](Project x, Project y){
if(x.l-x.t==y.l-y.t) return x.l+x.t < y.l+y.t;
return x.l-x.t < y.l-y.t;
});
fill(dis,dis+m,LINF); fill(seg,seg+2*m,LINF);
for(int i = 0; i < m; i++) if(a[i].l==1)
dis[i]=a[i].c, pq.push({a[i].c,i});
for(int i = 0; i < m; i++) upd(i);
while(!pq.empty()){
auto [D,s] = pq.top(); pq.pop();
query(a[s].r-a[s].t+1, a[s].r+a[s].t+1, D);
}
ll ans = LINF;
for(int i = 0; i < m; i++)
if(a[i].r==n) ans=min(ans,dis[i]);
cout << (ans==LINF?-1:ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
4148 KB |
Output is correct |
2 |
Correct |
68 ms |
5696 KB |
Output is correct |
3 |
Correct |
73 ms |
6404 KB |
Output is correct |
4 |
Correct |
78 ms |
6160 KB |
Output is correct |
5 |
Correct |
72 ms |
7740 KB |
Output is correct |
6 |
Correct |
70 ms |
5836 KB |
Output is correct |
7 |
Correct |
66 ms |
5708 KB |
Output is correct |
8 |
Correct |
52 ms |
5672 KB |
Output is correct |
9 |
Correct |
64 ms |
5620 KB |
Output is correct |
10 |
Correct |
54 ms |
5588 KB |
Output is correct |
11 |
Correct |
72 ms |
7852 KB |
Output is correct |
12 |
Correct |
74 ms |
7864 KB |
Output is correct |
13 |
Correct |
70 ms |
7720 KB |
Output is correct |
14 |
Correct |
71 ms |
7712 KB |
Output is correct |
15 |
Correct |
67 ms |
5600 KB |
Output is correct |
16 |
Correct |
72 ms |
5632 KB |
Output is correct |
17 |
Correct |
70 ms |
5528 KB |
Output is correct |
18 |
Correct |
67 ms |
7880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
4 ms |
724 KB |
Output is correct |
21 |
Correct |
4 ms |
724 KB |
Output is correct |
22 |
Correct |
4 ms |
704 KB |
Output is correct |
23 |
Correct |
4 ms |
596 KB |
Output is correct |
24 |
Correct |
4 ms |
836 KB |
Output is correct |
25 |
Correct |
5 ms |
724 KB |
Output is correct |
26 |
Correct |
4 ms |
724 KB |
Output is correct |
27 |
Correct |
4 ms |
724 KB |
Output is correct |
28 |
Correct |
4 ms |
860 KB |
Output is correct |
29 |
Correct |
5 ms |
724 KB |
Output is correct |
30 |
Correct |
3 ms |
732 KB |
Output is correct |
31 |
Correct |
3 ms |
728 KB |
Output is correct |
32 |
Correct |
4 ms |
852 KB |
Output is correct |
33 |
Correct |
4 ms |
820 KB |
Output is correct |
34 |
Correct |
4 ms |
596 KB |
Output is correct |
35 |
Correct |
4 ms |
852 KB |
Output is correct |
36 |
Correct |
4 ms |
856 KB |
Output is correct |
37 |
Correct |
4 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
4148 KB |
Output is correct |
2 |
Correct |
68 ms |
5696 KB |
Output is correct |
3 |
Correct |
73 ms |
6404 KB |
Output is correct |
4 |
Correct |
78 ms |
6160 KB |
Output is correct |
5 |
Correct |
72 ms |
7740 KB |
Output is correct |
6 |
Correct |
70 ms |
5836 KB |
Output is correct |
7 |
Correct |
66 ms |
5708 KB |
Output is correct |
8 |
Correct |
52 ms |
5672 KB |
Output is correct |
9 |
Correct |
64 ms |
5620 KB |
Output is correct |
10 |
Correct |
54 ms |
5588 KB |
Output is correct |
11 |
Correct |
72 ms |
7852 KB |
Output is correct |
12 |
Correct |
74 ms |
7864 KB |
Output is correct |
13 |
Correct |
70 ms |
7720 KB |
Output is correct |
14 |
Correct |
71 ms |
7712 KB |
Output is correct |
15 |
Correct |
67 ms |
5600 KB |
Output is correct |
16 |
Correct |
72 ms |
5632 KB |
Output is correct |
17 |
Correct |
70 ms |
5528 KB |
Output is correct |
18 |
Correct |
67 ms |
7880 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
328 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
4 ms |
724 KB |
Output is correct |
39 |
Correct |
4 ms |
724 KB |
Output is correct |
40 |
Correct |
4 ms |
704 KB |
Output is correct |
41 |
Correct |
4 ms |
596 KB |
Output is correct |
42 |
Correct |
4 ms |
836 KB |
Output is correct |
43 |
Correct |
5 ms |
724 KB |
Output is correct |
44 |
Correct |
4 ms |
724 KB |
Output is correct |
45 |
Correct |
4 ms |
724 KB |
Output is correct |
46 |
Correct |
4 ms |
860 KB |
Output is correct |
47 |
Correct |
5 ms |
724 KB |
Output is correct |
48 |
Correct |
3 ms |
732 KB |
Output is correct |
49 |
Correct |
3 ms |
728 KB |
Output is correct |
50 |
Correct |
4 ms |
852 KB |
Output is correct |
51 |
Correct |
4 ms |
820 KB |
Output is correct |
52 |
Correct |
4 ms |
596 KB |
Output is correct |
53 |
Correct |
4 ms |
852 KB |
Output is correct |
54 |
Correct |
4 ms |
856 KB |
Output is correct |
55 |
Correct |
4 ms |
596 KB |
Output is correct |
56 |
Correct |
73 ms |
6732 KB |
Output is correct |
57 |
Correct |
85 ms |
6268 KB |
Output is correct |
58 |
Correct |
78 ms |
5644 KB |
Output is correct |
59 |
Correct |
83 ms |
5752 KB |
Output is correct |
60 |
Correct |
94 ms |
5628 KB |
Output is correct |
61 |
Correct |
93 ms |
5624 KB |
Output is correct |
62 |
Correct |
86 ms |
6908 KB |
Output is correct |
63 |
Correct |
88 ms |
5608 KB |
Output is correct |
64 |
Correct |
82 ms |
5592 KB |
Output is correct |
65 |
Correct |
83 ms |
5584 KB |
Output is correct |
66 |
Correct |
89 ms |
5620 KB |
Output is correct |
67 |
Correct |
93 ms |
5752 KB |
Output is correct |
68 |
Correct |
90 ms |
5608 KB |
Output is correct |
69 |
Correct |
93 ms |
5684 KB |
Output is correct |
70 |
Correct |
92 ms |
5940 KB |
Output is correct |
71 |
Correct |
93 ms |
5584 KB |
Output is correct |
72 |
Correct |
87 ms |
5560 KB |
Output is correct |
73 |
Correct |
97 ms |
5872 KB |
Output is correct |
74 |
Correct |
64 ms |
5544 KB |
Output is correct |
75 |
Correct |
56 ms |
5580 KB |
Output is correct |
76 |
Correct |
76 ms |
7780 KB |
Output is correct |
77 |
Correct |
92 ms |
7756 KB |
Output is correct |
78 |
Correct |
76 ms |
7796 KB |
Output is correct |
79 |
Correct |
107 ms |
5668 KB |
Output is correct |
80 |
Correct |
68 ms |
5580 KB |
Output is correct |
81 |
Correct |
66 ms |
5668 KB |
Output is correct |
82 |
Correct |
75 ms |
5816 KB |
Output is correct |
83 |
Correct |
75 ms |
5828 KB |
Output is correct |
84 |
Correct |
90 ms |
5580 KB |
Output is correct |
85 |
Correct |
85 ms |
5628 KB |
Output is correct |
86 |
Correct |
91 ms |
5644 KB |
Output is correct |
87 |
Correct |
81 ms |
5556 KB |
Output is correct |
88 |
Correct |
77 ms |
5516 KB |
Output is correct |
89 |
Correct |
80 ms |
5576 KB |
Output is correct |
90 |
Correct |
81 ms |
7796 KB |
Output is correct |
91 |
Correct |
78 ms |
6736 KB |
Output is correct |
92 |
Correct |
76 ms |
5524 KB |
Output is correct |
93 |
Correct |
90 ms |
5752 KB |
Output is correct |
94 |
Correct |
76 ms |
5860 KB |
Output is correct |
95 |
Correct |
90 ms |
5652 KB |
Output is correct |
96 |
Correct |
80 ms |
7756 KB |
Output is correct |
97 |
Correct |
80 ms |
7780 KB |
Output is correct |
98 |
Correct |
77 ms |
7752 KB |
Output is correct |
99 |
Correct |
86 ms |
7752 KB |
Output is correct |
100 |
Correct |
85 ms |
7752 KB |
Output is correct |
101 |
Correct |
90 ms |
7800 KB |
Output is correct |
102 |
Correct |
79 ms |
7856 KB |
Output is correct |
103 |
Correct |
73 ms |
6000 KB |
Output is correct |