#include<bits/stdc++.h>
#define ll long long
const int nmax = 1e5 + 5, N = 1e5;
const ll oo = 1e18 + 5, base = 311;
const int lg = 17,mod = 1e9 +7;
#define pii pair<ll, ll>
#define fi first
#define se second
#define endl "\n"
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' '; cout << "\n";
using namespace std;
int n, m;
struct node{
ll t, l, r, c;
}a[nmax];
ll dp[nmax];
ll tree[nmax << 2][2];
priority_queue<pii, vector<pii>, greater<pii>> q;
void build(int id, int l, int r){
if(l == r){
if(dp[l] == oo){
tree[id][0] = a[l].l - a[l].t;
tree[id][1] = a[l].l + a[l].t;
}
else tree[id][0] =tree[id][1] = oo;
return;
}
int mid = r+ l >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
for(int e = 0; e < 2; ++e) tree[id][e] = min(tree[id << 1][e], tree[id << 1 | 1][e]);
}
void update(int id, int l, int r, int u, int v,ll val, int k, int idx){
if(l > v || r < u || u > v)return;
if(tree[id][idx] > k) return;
if(l == r){
dp[l] = val + a[l].c;
// cout << l << ' ' << dp[l] << endl;
q.push({dp[l], l});
tree[id][0] = tree[id][1] = oo;
return;
}
int mid = r+ l >> 1;
update(id << 1, l, mid, u,v ,val, k, idx);
update(id << 1 | 1, mid + 1, r, u, v,val, k, idx);
for(int e = 0; e < 2; ++e) tree[id][e] = min(tree[id << 1][e], tree[id << 1 | 1][e]);
}
main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("ROBOT.inp", "r", stdin);
// freopen("ROBOT.out", "w", stdout);
cin >> n >> m;
for(int i = 1; i <= m; ++i) cin >> a[i].t >> a[i].l >> a[i].r >> a[i].c;
sort(a + 1, a + 1 + m, [](node a, node b){
return a.t < b.t;
});
for(int i = 1; i <= m; ++i) dp[i] = oo;
for(int i = 1; i <= m; ++i){
if(a[i].l == 1){
dp[i] = a[i].c;
q.push({dp[i], i});
}
}
build(1, 1, m);
while(q.size()){
pii tmp = q.top();q.pop();
int u = tmp.se;
ll val = tmp.fi;
update(1, 1, m, 1, u - 1, val, a[u].r - a[u].t + 1, 0);
update(1, 1, m, u + 1, n, val, a[u].r + a[u].t + 1, 1);
}
ll ans = oo;
for(int i = 1; i <= m; ++i){
if(a[i].r == n){
ans = min(ans, dp[i]);
}
}
if(ans == oo) cout << -1;
else cout << ans;
}
/*
3 3
899
*/
Compilation message
treatment.cpp: In function 'void build(int, int, int)':
treatment.cpp:29:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
29 | int mid = r+ l >> 1;
| ~^~~
treatment.cpp: In function 'void update(int, int, int, int, int, long long int, int, int)':
treatment.cpp:45:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
45 | int mid = r+ l >> 1;
| ~^~~
treatment.cpp: At global scope:
treatment.cpp:50:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
50 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
11672 KB |
Output is correct |
2 |
Correct |
43 ms |
11600 KB |
Output is correct |
3 |
Correct |
51 ms |
10964 KB |
Output is correct |
4 |
Correct |
49 ms |
11860 KB |
Output is correct |
5 |
Correct |
59 ms |
13696 KB |
Output is correct |
6 |
Correct |
53 ms |
11692 KB |
Output is correct |
7 |
Correct |
53 ms |
11348 KB |
Output is correct |
8 |
Correct |
31 ms |
11612 KB |
Output is correct |
9 |
Correct |
31 ms |
11444 KB |
Output is correct |
10 |
Correct |
27 ms |
11612 KB |
Output is correct |
11 |
Correct |
50 ms |
13648 KB |
Output is correct |
12 |
Correct |
58 ms |
13576 KB |
Output is correct |
13 |
Correct |
59 ms |
13520 KB |
Output is correct |
14 |
Correct |
60 ms |
13772 KB |
Output is correct |
15 |
Correct |
59 ms |
11612 KB |
Output is correct |
16 |
Correct |
57 ms |
11600 KB |
Output is correct |
17 |
Correct |
58 ms |
10588 KB |
Output is correct |
18 |
Correct |
42 ms |
13004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
464 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
464 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
860 KB |
Output is correct |
21 |
Correct |
3 ms |
860 KB |
Output is correct |
22 |
Correct |
3 ms |
1008 KB |
Output is correct |
23 |
Correct |
3 ms |
1112 KB |
Output is correct |
24 |
Correct |
4 ms |
1116 KB |
Output is correct |
25 |
Correct |
3 ms |
1112 KB |
Output is correct |
26 |
Correct |
3 ms |
1116 KB |
Output is correct |
27 |
Correct |
3 ms |
888 KB |
Output is correct |
28 |
Correct |
3 ms |
1112 KB |
Output is correct |
29 |
Correct |
4 ms |
1116 KB |
Output is correct |
30 |
Correct |
3 ms |
1120 KB |
Output is correct |
31 |
Correct |
2 ms |
1112 KB |
Output is correct |
32 |
Correct |
3 ms |
1116 KB |
Output is correct |
33 |
Correct |
3 ms |
1308 KB |
Output is correct |
34 |
Correct |
3 ms |
1120 KB |
Output is correct |
35 |
Correct |
3 ms |
1116 KB |
Output is correct |
36 |
Correct |
3 ms |
1116 KB |
Output is correct |
37 |
Correct |
3 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
11672 KB |
Output is correct |
2 |
Correct |
43 ms |
11600 KB |
Output is correct |
3 |
Correct |
51 ms |
10964 KB |
Output is correct |
4 |
Correct |
49 ms |
11860 KB |
Output is correct |
5 |
Correct |
59 ms |
13696 KB |
Output is correct |
6 |
Correct |
53 ms |
11692 KB |
Output is correct |
7 |
Correct |
53 ms |
11348 KB |
Output is correct |
8 |
Correct |
31 ms |
11612 KB |
Output is correct |
9 |
Correct |
31 ms |
11444 KB |
Output is correct |
10 |
Correct |
27 ms |
11612 KB |
Output is correct |
11 |
Correct |
50 ms |
13648 KB |
Output is correct |
12 |
Correct |
58 ms |
13576 KB |
Output is correct |
13 |
Correct |
59 ms |
13520 KB |
Output is correct |
14 |
Correct |
60 ms |
13772 KB |
Output is correct |
15 |
Correct |
59 ms |
11612 KB |
Output is correct |
16 |
Correct |
57 ms |
11600 KB |
Output is correct |
17 |
Correct |
58 ms |
10588 KB |
Output is correct |
18 |
Correct |
42 ms |
13004 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
464 KB |
Output is correct |
27 |
Correct |
0 ms |
600 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
344 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
2 ms |
860 KB |
Output is correct |
39 |
Correct |
3 ms |
860 KB |
Output is correct |
40 |
Correct |
3 ms |
1008 KB |
Output is correct |
41 |
Correct |
3 ms |
1112 KB |
Output is correct |
42 |
Correct |
4 ms |
1116 KB |
Output is correct |
43 |
Correct |
3 ms |
1112 KB |
Output is correct |
44 |
Correct |
3 ms |
1116 KB |
Output is correct |
45 |
Correct |
3 ms |
888 KB |
Output is correct |
46 |
Correct |
3 ms |
1112 KB |
Output is correct |
47 |
Correct |
4 ms |
1116 KB |
Output is correct |
48 |
Correct |
3 ms |
1120 KB |
Output is correct |
49 |
Correct |
2 ms |
1112 KB |
Output is correct |
50 |
Correct |
3 ms |
1116 KB |
Output is correct |
51 |
Correct |
3 ms |
1308 KB |
Output is correct |
52 |
Correct |
3 ms |
1120 KB |
Output is correct |
53 |
Correct |
3 ms |
1116 KB |
Output is correct |
54 |
Correct |
3 ms |
1116 KB |
Output is correct |
55 |
Correct |
3 ms |
860 KB |
Output is correct |
56 |
Correct |
45 ms |
11480 KB |
Output is correct |
57 |
Correct |
49 ms |
12440 KB |
Output is correct |
58 |
Correct |
54 ms |
10592 KB |
Output is correct |
59 |
Correct |
68 ms |
11096 KB |
Output is correct |
60 |
Correct |
63 ms |
11864 KB |
Output is correct |
61 |
Correct |
60 ms |
10776 KB |
Output is correct |
62 |
Correct |
49 ms |
11484 KB |
Output is correct |
63 |
Correct |
59 ms |
11660 KB |
Output is correct |
64 |
Correct |
61 ms |
11680 KB |
Output is correct |
65 |
Correct |
63 ms |
11604 KB |
Output is correct |
66 |
Correct |
58 ms |
11792 KB |
Output is correct |
67 |
Correct |
71 ms |
12368 KB |
Output is correct |
68 |
Correct |
72 ms |
12112 KB |
Output is correct |
69 |
Correct |
68 ms |
12368 KB |
Output is correct |
70 |
Correct |
69 ms |
12368 KB |
Output is correct |
71 |
Correct |
71 ms |
12112 KB |
Output is correct |
72 |
Correct |
65 ms |
12140 KB |
Output is correct |
73 |
Correct |
77 ms |
12372 KB |
Output is correct |
74 |
Correct |
35 ms |
12124 KB |
Output is correct |
75 |
Correct |
43 ms |
12124 KB |
Output is correct |
76 |
Correct |
56 ms |
14032 KB |
Output is correct |
77 |
Correct |
61 ms |
14456 KB |
Output is correct |
78 |
Correct |
65 ms |
14028 KB |
Output is correct |
79 |
Correct |
84 ms |
12220 KB |
Output is correct |
80 |
Correct |
63 ms |
11856 KB |
Output is correct |
81 |
Correct |
37 ms |
12116 KB |
Output is correct |
82 |
Correct |
55 ms |
11408 KB |
Output is correct |
83 |
Correct |
58 ms |
11520 KB |
Output is correct |
84 |
Correct |
70 ms |
11600 KB |
Output is correct |
85 |
Correct |
57 ms |
12128 KB |
Output is correct |
86 |
Correct |
55 ms |
12300 KB |
Output is correct |
87 |
Correct |
61 ms |
12116 KB |
Output is correct |
88 |
Correct |
67 ms |
12284 KB |
Output is correct |
89 |
Correct |
60 ms |
12116 KB |
Output is correct |
90 |
Correct |
55 ms |
14304 KB |
Output is correct |
91 |
Correct |
61 ms |
13348 KB |
Output is correct |
92 |
Correct |
64 ms |
12116 KB |
Output is correct |
93 |
Correct |
72 ms |
12116 KB |
Output is correct |
94 |
Correct |
63 ms |
12384 KB |
Output is correct |
95 |
Correct |
70 ms |
12116 KB |
Output is correct |
96 |
Correct |
61 ms |
14288 KB |
Output is correct |
97 |
Correct |
60 ms |
14488 KB |
Output is correct |
98 |
Correct |
58 ms |
14544 KB |
Output is correct |
99 |
Correct |
58 ms |
14384 KB |
Output is correct |
100 |
Correct |
56 ms |
14288 KB |
Output is correct |
101 |
Correct |
63 ms |
14288 KB |
Output is correct |
102 |
Correct |
56 ms |
14532 KB |
Output is correct |
103 |
Correct |
48 ms |
12712 KB |
Output is correct |