#include <bits/stdc++.h>
using namespace std;
const int N = 4e5;
const int M = 4 * N;
set<pair<int, int>> all[2][N];
struct node {
pair<int, int> mn;
pair<int, int> mx;
} st[2][M];
node comb(node a, node b) {
node res;
res.mn = min(a.mn, b.mn);
res.mx = max(a.mx, b.mx);
return res;
}
void Build(int t, int x, int l, int r) {
st[t][x].mn = {1.00001e9, -1};
st[t][x].mx = {-1.00001e9, -1};
if (l == r) {
return;
}
int mid = l + r >> 1;
Build(t, x * 2, l, mid);
Build(t, x * 2 + 1, mid + 1, r);
}
void Modify(int t, int x, int l, int r, int p, pair<int, int> v) {
if (l == r) {
all[t][l].insert(v);
st[t][x].mn = *all[t][l].begin();
st[t][x].mx = *prev(all[t][l].end());
return;
}
int mid = l + r >> 1;
if (p <= mid) {
Modify(t, x * 2, l, mid, p, v);
} else {
Modify(t, x * 2 + 1, mid + 1, r, p, v);
}
st[t][x] = comb(st[t][x * 2], st[t][x * 2 + 1]);
}
void Remove(int t, int x, int l, int r, int p, pair<int, int> v) {
if (l == r) {
all[t][l].erase(all[t][l].find(v));
if (all[t][l].empty()) {
st[t][x].mn = {1.00001e9, -1};
st[t][x].mx = {-1.00001e9, -1};
} else {
st[t][x].mn = *all[t][l].begin();
st[t][x].mx = *prev(all[t][l].end());
}
return;
}
int mid = l + r >> 1;
if (p <= mid) {
Remove(t, x * 2, l, mid, p, v);
} else {
Remove(t, x * 2 + 1, mid + 1, r, p, v);
}
st[t][x] = comb(st[t][x * 2], st[t][x * 2 + 1]);
}
node Query(int t, int x, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) {
return st[t][x];
}
int mid = l + r >> 1;
if (qr <= mid) {
return Query(t, x * 2, l, mid, ql, qr);
} else if (ql > mid) {
return Query(t, x * 2 + 1, mid + 1, r, ql, qr);
} else {
return comb(Query(t, x * 2, l, mid, ql, qr), Query(t, x * 2 + 1, mid + 1, r, ql, qr));
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> t(m), l(m), r(m), c(m);
for (int i = 0; i < m; i++) {
cin >> t[i] >> l[i] >> r[i] >> c[i];
--l[i];
}
vector<int> xs;
for (int i = 0; i < m; i++) {
xs.push_back(l[i] - t[i]);
xs.push_back(l[i] + t[i]);
xs.push_back(r[i] - t[i]);
xs.push_back(r[i] + t[i]);
}
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
auto GetVal = [&](int x) {
return (int) (lower_bound(xs.begin(), xs.end(), x) - xs.begin());
};
int k = (int) xs.size();
Build(0, 1, 0, k - 1);
Build(1, 1, 0, k - 1);
for (int i = 0; i < m; i++) {
Modify(0, 1, 0, k - 1, GetVal(l[i] - t[i]), {t[i], i});
Modify(1, 1, 0, k - 1, GetVal(l[i] + t[i]), {t[i], i});
}
const long long inf = (long long) 1e18;
vector<long long> dist(m, inf);
set<pair<long long, int>> st;
for (int i = 0; i < m; i++) {
if (l[i] == 0) {
dist[i] = c[i];
st.emplace(dist[i], i);
Remove(0, 1, 0, k - 1, GetVal(l[i] - t[i]), {t[i], i});
Remove(1, 1, 0, k - 1, GetVal(l[i] + t[i]), {t[i], i});
}
}
while (!st.empty()) {
auto it = st.begin();
int i = it->second;
st.erase(it);
while (true) {
{
node nd = Query(0, 1, 0, k - 1, 0, GetVal(r[i] - t[i]));
if (nd.mn.first <= t[i]) {
int j = nd.mn.second;
dist[j] = dist[i] + c[j];
Remove(0, 1, 0, k - 1, GetVal(l[j] - t[j]), {t[j], j});
Remove(1, 1, 0, k - 1, GetVal(l[j] + t[j]), {t[j], j});
st.emplace(dist[j], j);
continue;
}
}
{
node nd = Query(1, 1, 0, k - 1, 0, GetVal(r[i] + t[i]));
if (nd.mx.first >= t[i]) {
int j = nd.mx.second;
dist[j] = dist[i] + c[j];
Remove(0, 1, 0, k - 1, GetVal(l[j] - t[j]), {t[j], j});
Remove(1, 1, 0, k - 1, GetVal(l[j] + t[j]), {t[j], j});
st.emplace(dist[j], j);
continue;
}
}
break;
}
}
long long res = inf;
for (int i = 0; i < m; i++) {
if (r[i] == n) {
res = min(res, dist[i]);
}
}
cout << (res == inf ? -1 : res) << '\n';
return 0;
}
Compilation message
treatment.cpp: In function 'void Build(int, int, int, int)':
treatment.cpp:29:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
29 | int mid = l + r >> 1;
| ~~^~~
treatment.cpp: In function 'void Modify(int, int, int, int, int, std::pair<int, int>)':
treatment.cpp:41:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
41 | int mid = l + r >> 1;
| ~~^~~
treatment.cpp: In function 'void Remove(int, int, int, int, int, std::pair<int, int>)':
treatment.cpp:62:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
62 | int mid = l + r >> 1;
| ~~^~~
treatment.cpp: In function 'node Query(int, int, int, int, int, int)':
treatment.cpp:75:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
75 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
338 ms |
72324 KB |
Output is correct |
2 |
Correct |
282 ms |
73844 KB |
Output is correct |
3 |
Correct |
293 ms |
60584 KB |
Output is correct |
4 |
Correct |
256 ms |
60696 KB |
Output is correct |
5 |
Correct |
193 ms |
57288 KB |
Output is correct |
6 |
Correct |
167 ms |
57292 KB |
Output is correct |
7 |
Correct |
176 ms |
59336 KB |
Output is correct |
8 |
Correct |
104 ms |
57036 KB |
Output is correct |
9 |
Correct |
120 ms |
57288 KB |
Output is correct |
10 |
Correct |
105 ms |
61352 KB |
Output is correct |
11 |
Correct |
457 ms |
90188 KB |
Output is correct |
12 |
Correct |
454 ms |
90320 KB |
Output is correct |
13 |
Correct |
453 ms |
90056 KB |
Output is correct |
14 |
Correct |
460 ms |
90156 KB |
Output is correct |
15 |
Correct |
399 ms |
90132 KB |
Output is correct |
16 |
Correct |
392 ms |
90180 KB |
Output is correct |
17 |
Correct |
393 ms |
89372 KB |
Output is correct |
18 |
Correct |
429 ms |
89404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
40536 KB |
Output is correct |
2 |
Correct |
8 ms |
40540 KB |
Output is correct |
3 |
Correct |
8 ms |
40540 KB |
Output is correct |
4 |
Correct |
9 ms |
40540 KB |
Output is correct |
5 |
Correct |
8 ms |
40540 KB |
Output is correct |
6 |
Correct |
8 ms |
40524 KB |
Output is correct |
7 |
Correct |
8 ms |
40540 KB |
Output is correct |
8 |
Correct |
8 ms |
40540 KB |
Output is correct |
9 |
Correct |
8 ms |
40540 KB |
Output is correct |
10 |
Correct |
8 ms |
40644 KB |
Output is correct |
11 |
Correct |
8 ms |
40612 KB |
Output is correct |
12 |
Correct |
8 ms |
40536 KB |
Output is correct |
13 |
Correct |
8 ms |
40540 KB |
Output is correct |
14 |
Correct |
8 ms |
40544 KB |
Output is correct |
15 |
Correct |
8 ms |
40636 KB |
Output is correct |
16 |
Correct |
8 ms |
40544 KB |
Output is correct |
17 |
Correct |
9 ms |
40540 KB |
Output is correct |
18 |
Correct |
8 ms |
40700 KB |
Output is correct |
19 |
Correct |
10 ms |
40540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
40536 KB |
Output is correct |
2 |
Correct |
8 ms |
40540 KB |
Output is correct |
3 |
Correct |
8 ms |
40540 KB |
Output is correct |
4 |
Correct |
9 ms |
40540 KB |
Output is correct |
5 |
Correct |
8 ms |
40540 KB |
Output is correct |
6 |
Correct |
8 ms |
40524 KB |
Output is correct |
7 |
Correct |
8 ms |
40540 KB |
Output is correct |
8 |
Correct |
8 ms |
40540 KB |
Output is correct |
9 |
Correct |
8 ms |
40540 KB |
Output is correct |
10 |
Correct |
8 ms |
40644 KB |
Output is correct |
11 |
Correct |
8 ms |
40612 KB |
Output is correct |
12 |
Correct |
8 ms |
40536 KB |
Output is correct |
13 |
Correct |
8 ms |
40540 KB |
Output is correct |
14 |
Correct |
8 ms |
40544 KB |
Output is correct |
15 |
Correct |
8 ms |
40636 KB |
Output is correct |
16 |
Correct |
8 ms |
40544 KB |
Output is correct |
17 |
Correct |
9 ms |
40540 KB |
Output is correct |
18 |
Correct |
8 ms |
40700 KB |
Output is correct |
19 |
Correct |
10 ms |
40540 KB |
Output is correct |
20 |
Correct |
19 ms |
43768 KB |
Output is correct |
21 |
Correct |
20 ms |
43712 KB |
Output is correct |
22 |
Correct |
20 ms |
43612 KB |
Output is correct |
23 |
Correct |
19 ms |
43612 KB |
Output is correct |
24 |
Correct |
22 ms |
43868 KB |
Output is correct |
25 |
Correct |
22 ms |
43780 KB |
Output is correct |
26 |
Correct |
24 ms |
43756 KB |
Output is correct |
27 |
Correct |
18 ms |
43612 KB |
Output is correct |
28 |
Correct |
22 ms |
43840 KB |
Output is correct |
29 |
Correct |
22 ms |
43868 KB |
Output is correct |
30 |
Correct |
15 ms |
43608 KB |
Output is correct |
31 |
Correct |
13 ms |
43612 KB |
Output is correct |
32 |
Correct |
22 ms |
43868 KB |
Output is correct |
33 |
Correct |
22 ms |
43616 KB |
Output is correct |
34 |
Correct |
21 ms |
43616 KB |
Output is correct |
35 |
Correct |
22 ms |
43612 KB |
Output is correct |
36 |
Correct |
24 ms |
43748 KB |
Output is correct |
37 |
Correct |
24 ms |
43876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
338 ms |
72324 KB |
Output is correct |
2 |
Correct |
282 ms |
73844 KB |
Output is correct |
3 |
Correct |
293 ms |
60584 KB |
Output is correct |
4 |
Correct |
256 ms |
60696 KB |
Output is correct |
5 |
Correct |
193 ms |
57288 KB |
Output is correct |
6 |
Correct |
167 ms |
57292 KB |
Output is correct |
7 |
Correct |
176 ms |
59336 KB |
Output is correct |
8 |
Correct |
104 ms |
57036 KB |
Output is correct |
9 |
Correct |
120 ms |
57288 KB |
Output is correct |
10 |
Correct |
105 ms |
61352 KB |
Output is correct |
11 |
Correct |
457 ms |
90188 KB |
Output is correct |
12 |
Correct |
454 ms |
90320 KB |
Output is correct |
13 |
Correct |
453 ms |
90056 KB |
Output is correct |
14 |
Correct |
460 ms |
90156 KB |
Output is correct |
15 |
Correct |
399 ms |
90132 KB |
Output is correct |
16 |
Correct |
392 ms |
90180 KB |
Output is correct |
17 |
Correct |
393 ms |
89372 KB |
Output is correct |
18 |
Correct |
429 ms |
89404 KB |
Output is correct |
19 |
Correct |
8 ms |
40536 KB |
Output is correct |
20 |
Correct |
8 ms |
40540 KB |
Output is correct |
21 |
Correct |
8 ms |
40540 KB |
Output is correct |
22 |
Correct |
9 ms |
40540 KB |
Output is correct |
23 |
Correct |
8 ms |
40540 KB |
Output is correct |
24 |
Correct |
8 ms |
40524 KB |
Output is correct |
25 |
Correct |
8 ms |
40540 KB |
Output is correct |
26 |
Correct |
8 ms |
40540 KB |
Output is correct |
27 |
Correct |
8 ms |
40540 KB |
Output is correct |
28 |
Correct |
8 ms |
40644 KB |
Output is correct |
29 |
Correct |
8 ms |
40612 KB |
Output is correct |
30 |
Correct |
8 ms |
40536 KB |
Output is correct |
31 |
Correct |
8 ms |
40540 KB |
Output is correct |
32 |
Correct |
8 ms |
40544 KB |
Output is correct |
33 |
Correct |
8 ms |
40636 KB |
Output is correct |
34 |
Correct |
8 ms |
40544 KB |
Output is correct |
35 |
Correct |
9 ms |
40540 KB |
Output is correct |
36 |
Correct |
8 ms |
40700 KB |
Output is correct |
37 |
Correct |
10 ms |
40540 KB |
Output is correct |
38 |
Correct |
19 ms |
43768 KB |
Output is correct |
39 |
Correct |
20 ms |
43712 KB |
Output is correct |
40 |
Correct |
20 ms |
43612 KB |
Output is correct |
41 |
Correct |
19 ms |
43612 KB |
Output is correct |
42 |
Correct |
22 ms |
43868 KB |
Output is correct |
43 |
Correct |
22 ms |
43780 KB |
Output is correct |
44 |
Correct |
24 ms |
43756 KB |
Output is correct |
45 |
Correct |
18 ms |
43612 KB |
Output is correct |
46 |
Correct |
22 ms |
43840 KB |
Output is correct |
47 |
Correct |
22 ms |
43868 KB |
Output is correct |
48 |
Correct |
15 ms |
43608 KB |
Output is correct |
49 |
Correct |
13 ms |
43612 KB |
Output is correct |
50 |
Correct |
22 ms |
43868 KB |
Output is correct |
51 |
Correct |
22 ms |
43616 KB |
Output is correct |
52 |
Correct |
21 ms |
43616 KB |
Output is correct |
53 |
Correct |
22 ms |
43612 KB |
Output is correct |
54 |
Correct |
24 ms |
43748 KB |
Output is correct |
55 |
Correct |
24 ms |
43876 KB |
Output is correct |
56 |
Correct |
336 ms |
73284 KB |
Output is correct |
57 |
Correct |
333 ms |
73656 KB |
Output is correct |
58 |
Correct |
285 ms |
72892 KB |
Output is correct |
59 |
Correct |
288 ms |
73156 KB |
Output is correct |
60 |
Correct |
353 ms |
90428 KB |
Output is correct |
61 |
Correct |
283 ms |
72908 KB |
Output is correct |
62 |
Correct |
333 ms |
73640 KB |
Output is correct |
63 |
Correct |
306 ms |
90180 KB |
Output is correct |
64 |
Correct |
326 ms |
90484 KB |
Output is correct |
65 |
Correct |
318 ms |
74004 KB |
Output is correct |
66 |
Correct |
312 ms |
90312 KB |
Output is correct |
67 |
Correct |
436 ms |
90824 KB |
Output is correct |
68 |
Correct |
422 ms |
90776 KB |
Output is correct |
69 |
Correct |
340 ms |
90812 KB |
Output is correct |
70 |
Correct |
450 ms |
91032 KB |
Output is correct |
71 |
Correct |
392 ms |
90724 KB |
Output is correct |
72 |
Correct |
311 ms |
90892 KB |
Output is correct |
73 |
Correct |
456 ms |
91072 KB |
Output is correct |
74 |
Correct |
187 ms |
90904 KB |
Output is correct |
75 |
Correct |
151 ms |
90748 KB |
Output is correct |
76 |
Correct |
528 ms |
90668 KB |
Output is correct |
77 |
Correct |
503 ms |
91032 KB |
Output is correct |
78 |
Correct |
457 ms |
90628 KB |
Output is correct |
79 |
Correct |
476 ms |
90916 KB |
Output is correct |
80 |
Correct |
397 ms |
90512 KB |
Output is correct |
81 |
Correct |
245 ms |
90888 KB |
Output is correct |
82 |
Correct |
410 ms |
89756 KB |
Output is correct |
83 |
Correct |
445 ms |
90056 KB |
Output is correct |
84 |
Correct |
497 ms |
90224 KB |
Output is correct |
85 |
Correct |
362 ms |
90892 KB |
Output is correct |
86 |
Correct |
361 ms |
90776 KB |
Output is correct |
87 |
Correct |
374 ms |
91128 KB |
Output is correct |
88 |
Correct |
359 ms |
90828 KB |
Output is correct |
89 |
Correct |
354 ms |
90752 KB |
Output is correct |
90 |
Correct |
490 ms |
91044 KB |
Output is correct |
91 |
Correct |
450 ms |
90940 KB |
Output is correct |
92 |
Correct |
364 ms |
90940 KB |
Output is correct |
93 |
Correct |
457 ms |
90984 KB |
Output is correct |
94 |
Correct |
377 ms |
91340 KB |
Output is correct |
95 |
Correct |
437 ms |
90928 KB |
Output is correct |
96 |
Correct |
505 ms |
91056 KB |
Output is correct |
97 |
Correct |
483 ms |
91084 KB |
Output is correct |
98 |
Correct |
486 ms |
91080 KB |
Output is correct |
99 |
Correct |
507 ms |
90920 KB |
Output is correct |
100 |
Correct |
393 ms |
90824 KB |
Output is correct |
101 |
Correct |
499 ms |
90824 KB |
Output is correct |
102 |
Correct |
508 ms |
91080 KB |
Output is correct |
103 |
Correct |
353 ms |
91336 KB |
Output is correct |