#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define int long long
const int mxM = (int)1e5+10;
const int LINF = (int)1e18;
using pii = pair<int,int>;
vector<pii> v;
int n, m, ans=LINF, dis[mxM];
struct Project{ int t, l, r, c; } a[mxM];
priority_queue<pii,vector<pii>,greater<pii>> pq;
int segTree[mxM*2];
void upd(int x, int p=0, int l=0, int r=m-1){
if(l==r){ segTree[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);
segTree[p] = min(segTree[p+1],segTree[rp]);
}
void query(int i, int j, int k, int p=0, int l=0, int r=m-1){
if(i<a[l].l-a[l].t or j < segTree[p]) return;
if(l==r){
if(dis[l]==LINF) dis[l] = k+a[l].c, pq.push({dis[l],l});
segTree[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);
segTree[p] = min(segTree[p+1],segTree[rp]);
}
int dijkstra(vector<pii> v){
fill(dis,dis+m,LINF);
for(auto [u,w] : v) dis[u]=w, pq.push({w,u});
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);
}
int d = LINF;
for(int i = 0; i < m; i++)
if(a[i].r==n) d=min(d,dis[i]);
return d;
}
int32_t main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
fill(segTree,segTree+2*m,LINF);
for(int i = 0; i < m; i++)
cin >> a[i].t >> a[i].l >> a[i].r >> a[i].c,a[i].t*=-1;
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;
});
for(int i = 0; i < m; i++)
if(a[i].l==1) v.pb({i,a[i].c});
ans = dijkstra(v); cout << (ans==LINF?-1:ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
8992 KB |
Output is correct |
2 |
Correct |
69 ms |
8976 KB |
Output is correct |
3 |
Correct |
81 ms |
9560 KB |
Output is correct |
4 |
Correct |
98 ms |
9460 KB |
Output is correct |
5 |
Correct |
78 ms |
11036 KB |
Output is correct |
6 |
Correct |
67 ms |
9036 KB |
Output is correct |
7 |
Correct |
65 ms |
8896 KB |
Output is correct |
8 |
Correct |
61 ms |
8912 KB |
Output is correct |
9 |
Correct |
57 ms |
8772 KB |
Output is correct |
10 |
Correct |
54 ms |
8780 KB |
Output is correct |
11 |
Correct |
81 ms |
11044 KB |
Output is correct |
12 |
Correct |
82 ms |
11040 KB |
Output is correct |
13 |
Correct |
75 ms |
11052 KB |
Output is correct |
14 |
Correct |
76 ms |
11064 KB |
Output is correct |
15 |
Correct |
71 ms |
8880 KB |
Output is correct |
16 |
Correct |
76 ms |
9036 KB |
Output is correct |
17 |
Correct |
64 ms |
8132 KB |
Output is correct |
18 |
Correct |
87 ms |
10316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 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 |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
336 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 |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 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 |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
336 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 |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
6 ms |
732 KB |
Output is correct |
21 |
Correct |
4 ms |
792 KB |
Output is correct |
22 |
Correct |
4 ms |
688 KB |
Output is correct |
23 |
Correct |
4 ms |
688 KB |
Output is correct |
24 |
Correct |
5 ms |
852 KB |
Output is correct |
25 |
Correct |
5 ms |
736 KB |
Output is correct |
26 |
Correct |
5 ms |
724 KB |
Output is correct |
27 |
Correct |
4 ms |
692 KB |
Output is correct |
28 |
Correct |
7 ms |
948 KB |
Output is correct |
29 |
Correct |
6 ms |
792 KB |
Output is correct |
30 |
Correct |
4 ms |
736 KB |
Output is correct |
31 |
Correct |
4 ms |
732 KB |
Output is correct |
32 |
Correct |
7 ms |
980 KB |
Output is correct |
33 |
Correct |
4 ms |
992 KB |
Output is correct |
34 |
Correct |
5 ms |
732 KB |
Output is correct |
35 |
Correct |
5 ms |
852 KB |
Output is correct |
36 |
Correct |
6 ms |
852 KB |
Output is correct |
37 |
Correct |
6 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
8992 KB |
Output is correct |
2 |
Correct |
69 ms |
8976 KB |
Output is correct |
3 |
Correct |
81 ms |
9560 KB |
Output is correct |
4 |
Correct |
98 ms |
9460 KB |
Output is correct |
5 |
Correct |
78 ms |
11036 KB |
Output is correct |
6 |
Correct |
67 ms |
9036 KB |
Output is correct |
7 |
Correct |
65 ms |
8896 KB |
Output is correct |
8 |
Correct |
61 ms |
8912 KB |
Output is correct |
9 |
Correct |
57 ms |
8772 KB |
Output is correct |
10 |
Correct |
54 ms |
8780 KB |
Output is correct |
11 |
Correct |
81 ms |
11044 KB |
Output is correct |
12 |
Correct |
82 ms |
11040 KB |
Output is correct |
13 |
Correct |
75 ms |
11052 KB |
Output is correct |
14 |
Correct |
76 ms |
11064 KB |
Output is correct |
15 |
Correct |
71 ms |
8880 KB |
Output is correct |
16 |
Correct |
76 ms |
9036 KB |
Output is correct |
17 |
Correct |
64 ms |
8132 KB |
Output is correct |
18 |
Correct |
87 ms |
10316 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 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 |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
336 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 |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
6 ms |
732 KB |
Output is correct |
39 |
Correct |
4 ms |
792 KB |
Output is correct |
40 |
Correct |
4 ms |
688 KB |
Output is correct |
41 |
Correct |
4 ms |
688 KB |
Output is correct |
42 |
Correct |
5 ms |
852 KB |
Output is correct |
43 |
Correct |
5 ms |
736 KB |
Output is correct |
44 |
Correct |
5 ms |
724 KB |
Output is correct |
45 |
Correct |
4 ms |
692 KB |
Output is correct |
46 |
Correct |
7 ms |
948 KB |
Output is correct |
47 |
Correct |
6 ms |
792 KB |
Output is correct |
48 |
Correct |
4 ms |
736 KB |
Output is correct |
49 |
Correct |
4 ms |
732 KB |
Output is correct |
50 |
Correct |
7 ms |
980 KB |
Output is correct |
51 |
Correct |
4 ms |
992 KB |
Output is correct |
52 |
Correct |
5 ms |
732 KB |
Output is correct |
53 |
Correct |
5 ms |
852 KB |
Output is correct |
54 |
Correct |
6 ms |
852 KB |
Output is correct |
55 |
Correct |
6 ms |
724 KB |
Output is correct |
56 |
Correct |
92 ms |
9800 KB |
Output is correct |
57 |
Correct |
89 ms |
10640 KB |
Output is correct |
58 |
Correct |
89 ms |
8108 KB |
Output is correct |
59 |
Correct |
90 ms |
8540 KB |
Output is correct |
60 |
Correct |
94 ms |
9080 KB |
Output is correct |
61 |
Correct |
85 ms |
8220 KB |
Output is correct |
62 |
Correct |
80 ms |
9804 KB |
Output is correct |
63 |
Correct |
82 ms |
8920 KB |
Output is correct |
64 |
Correct |
86 ms |
8996 KB |
Output is correct |
65 |
Correct |
96 ms |
9128 KB |
Output is correct |
66 |
Correct |
104 ms |
9072 KB |
Output is correct |
67 |
Correct |
90 ms |
9700 KB |
Output is correct |
68 |
Correct |
127 ms |
9624 KB |
Output is correct |
69 |
Correct |
101 ms |
9572 KB |
Output is correct |
70 |
Correct |
91 ms |
9896 KB |
Output is correct |
71 |
Correct |
96 ms |
9656 KB |
Output is correct |
72 |
Correct |
93 ms |
9580 KB |
Output is correct |
73 |
Correct |
96 ms |
9800 KB |
Output is correct |
74 |
Correct |
58 ms |
9548 KB |
Output is correct |
75 |
Correct |
60 ms |
9548 KB |
Output is correct |
76 |
Correct |
77 ms |
11488 KB |
Output is correct |
77 |
Correct |
98 ms |
11888 KB |
Output is correct |
78 |
Correct |
81 ms |
11412 KB |
Output is correct |
79 |
Correct |
113 ms |
9704 KB |
Output is correct |
80 |
Correct |
73 ms |
9200 KB |
Output is correct |
81 |
Correct |
66 ms |
9596 KB |
Output is correct |
82 |
Correct |
71 ms |
8656 KB |
Output is correct |
83 |
Correct |
75 ms |
8892 KB |
Output is correct |
84 |
Correct |
123 ms |
8860 KB |
Output is correct |
85 |
Correct |
80 ms |
9664 KB |
Output is correct |
86 |
Correct |
95 ms |
9604 KB |
Output is correct |
87 |
Correct |
92 ms |
9656 KB |
Output is correct |
88 |
Correct |
82 ms |
9644 KB |
Output is correct |
89 |
Correct |
99 ms |
9704 KB |
Output is correct |
90 |
Correct |
101 ms |
11824 KB |
Output is correct |
91 |
Correct |
80 ms |
10756 KB |
Output is correct |
92 |
Correct |
104 ms |
9572 KB |
Output is correct |
93 |
Correct |
106 ms |
9560 KB |
Output is correct |
94 |
Correct |
87 ms |
9788 KB |
Output is correct |
95 |
Correct |
97 ms |
9620 KB |
Output is correct |
96 |
Correct |
130 ms |
11844 KB |
Output is correct |
97 |
Correct |
96 ms |
11848 KB |
Output is correct |
98 |
Correct |
88 ms |
11828 KB |
Output is correct |
99 |
Correct |
88 ms |
11752 KB |
Output is correct |
100 |
Correct |
84 ms |
12120 KB |
Output is correct |
101 |
Correct |
82 ms |
11832 KB |
Output is correct |
102 |
Correct |
83 ms |
11812 KB |
Output is correct |
103 |
Correct |
72 ms |
9972 KB |
Output is correct |