#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
const ll inf = 0x3f3f3f3f3f3f3f3fLL;
ll N, M, sz;
pair<pii, pii> arr[1600005];
pair<pii, int> v[1600005];
vector<ll> xpos;
ll dist[1600005];
set<pii> seg[1600005];
void add(int n, int s, int e, int idx, pii val) {
if (idx > e || idx < s) return;
if (s == e) {
seg[n].insert(val);
return;
}
int m = (s + e) >> 1;
add(n * 2, s, m, idx, val);
add(n * 2 + 1, m + 1, e, idx, val);
seg[n].insert(val);
return;
}
void del(int n, int s, int e, int idx, pii val) {
if (idx > e || idx < s) return;
if (s == e) {
seg[n].erase(val);
return;
}
int m = (s + e) >> 1;
del(n * 2, s, m, idx, val);
del(n * 2 + 1, m + 1, e, idx, val);
seg[n].erase(val);
return;
}
void query(int n, int s, int e, int l, int r, ll y, vector<int> &d) {
if (l > e || r < s) return;
if (l <= s && e <= r) {
for (pii i: seg[n]) {
if (i.first > y) break;
d.push_back(i.second);
}
return;
}
int m = (s + e) >> 1;
query(n * 2, s, m, l, r, y, d);
query(n * 2 + 1, m + 1, e, l, r, y, d);
return;
}
void Dijkstra() {
memset(dist, 0x3f, sizeof(dist));
priority_queue<pii, vector<pii>, greater<>> pq;
vector<int> d;
for (int i = 0; i < M; i++) {
int idx = v[i].second;
if (arr[idx].first.second == 1) {
dist[i] = arr[idx].second.second;
pq.push({dist[i], i});
d.push_back(i);
}
}
for (int i: d) {
ll x = v[i].first.first, y = v[i].first.second, idx = v[i].second;
int pos = lower_bound(xpos.begin(), xpos.end(), x) - xpos.begin();
del(1, 0, sz - 1, pos, {y, idx});
}
while (pq.size()) {
pii t = pq.top();
pq.pop();
ll now = t.second, val = t.first;
int idx = v[now].second;
ll x = arr[idx].second.first - arr[idx].first.first + 1, y = arr[idx].second.first + arr[idx].first.first + 1;
d.clear();
int pos = upper_bound(xpos.begin(), xpos.end(), x) - xpos.begin() - 1;
query(1, 0, sz - 1, 0, pos, y, d);
for (int i: d) {
ll x = v[i].first.first, y = v[i].first.second, idx = v[i].second;
int pos = lower_bound(xpos.begin(), xpos.end(), x) - xpos.begin();
del(1, 0, sz - 1, pos, {y, idx});
dist[i] = val + arr[idx].second.second;
pq.push({dist[i], i});
}
}
return;
}
void solve() {
cin >> N >> M;
for (int i = 0; i < M; i++) {
ll T, L, R, C;
cin >> T >> L >> R >> C;
ll x = L - T, y = L + T;
arr[i] = {{T, L},
{R, C}};
v[i] = {{x, y}, i};
xpos.push_back(x);
xpos.push_back(x + 1);
}
sz = xpos.size();
sort(xpos.begin(), xpos.end());
xpos.erase(unique(xpos.begin(), xpos.end()), xpos.end());
for (int i = 0; i < M; i++) {
ll x = v[i].first.first, y = v[i].first.second, idx = v[i].second;
int pos = lower_bound(xpos.begin(), xpos.end(), x) - xpos.begin();
add(1, 0, sz - 1, pos, {y, idx});
}
Dijkstra();
ll ans = inf;
for (int i = 0; i < M; i++) {
int idx = v[i].second;
if (arr[idx].second.first == N) ans = min(ans, dist[i]);
}
if (ans == inf) cout << -1;
else cout << ans;
return;
}
int main(void) {
#ifndef ONLINE_JUDGE
//freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
//cin >> t;
while (t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
640 ms |
215012 KB |
Output is correct |
2 |
Correct |
630 ms |
215256 KB |
Output is correct |
3 |
Correct |
604 ms |
217028 KB |
Output is correct |
4 |
Correct |
601 ms |
216260 KB |
Output is correct |
5 |
Correct |
464 ms |
218312 KB |
Output is correct |
6 |
Correct |
436 ms |
215496 KB |
Output is correct |
7 |
Correct |
417 ms |
215240 KB |
Output is correct |
8 |
Correct |
409 ms |
215288 KB |
Output is correct |
9 |
Correct |
332 ms |
215268 KB |
Output is correct |
10 |
Correct |
398 ms |
215280 KB |
Output is correct |
11 |
Correct |
787 ms |
219248 KB |
Output is correct |
12 |
Correct |
794 ms |
219012 KB |
Output is correct |
13 |
Correct |
797 ms |
218604 KB |
Output is correct |
14 |
Correct |
804 ms |
218312 KB |
Output is correct |
15 |
Correct |
766 ms |
215352 KB |
Output is correct |
16 |
Correct |
741 ms |
215236 KB |
Output is correct |
17 |
Correct |
740 ms |
214488 KB |
Output is correct |
18 |
Correct |
778 ms |
218296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
87888 KB |
Output is correct |
2 |
Correct |
36 ms |
87884 KB |
Output is correct |
3 |
Correct |
37 ms |
87892 KB |
Output is correct |
4 |
Correct |
37 ms |
87900 KB |
Output is correct |
5 |
Correct |
37 ms |
87888 KB |
Output is correct |
6 |
Correct |
43 ms |
87900 KB |
Output is correct |
7 |
Correct |
36 ms |
87892 KB |
Output is correct |
8 |
Correct |
35 ms |
87900 KB |
Output is correct |
9 |
Correct |
36 ms |
87892 KB |
Output is correct |
10 |
Correct |
36 ms |
87916 KB |
Output is correct |
11 |
Correct |
34 ms |
88100 KB |
Output is correct |
12 |
Correct |
40 ms |
88048 KB |
Output is correct |
13 |
Correct |
35 ms |
87896 KB |
Output is correct |
14 |
Correct |
51 ms |
87972 KB |
Output is correct |
15 |
Correct |
33 ms |
87896 KB |
Output is correct |
16 |
Correct |
34 ms |
87888 KB |
Output is correct |
17 |
Correct |
35 ms |
87900 KB |
Output is correct |
18 |
Correct |
36 ms |
88156 KB |
Output is correct |
19 |
Correct |
52 ms |
87900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
87888 KB |
Output is correct |
2 |
Correct |
36 ms |
87884 KB |
Output is correct |
3 |
Correct |
37 ms |
87892 KB |
Output is correct |
4 |
Correct |
37 ms |
87900 KB |
Output is correct |
5 |
Correct |
37 ms |
87888 KB |
Output is correct |
6 |
Correct |
43 ms |
87900 KB |
Output is correct |
7 |
Correct |
36 ms |
87892 KB |
Output is correct |
8 |
Correct |
35 ms |
87900 KB |
Output is correct |
9 |
Correct |
36 ms |
87892 KB |
Output is correct |
10 |
Correct |
36 ms |
87916 KB |
Output is correct |
11 |
Correct |
34 ms |
88100 KB |
Output is correct |
12 |
Correct |
40 ms |
88048 KB |
Output is correct |
13 |
Correct |
35 ms |
87896 KB |
Output is correct |
14 |
Correct |
51 ms |
87972 KB |
Output is correct |
15 |
Correct |
33 ms |
87896 KB |
Output is correct |
16 |
Correct |
34 ms |
87888 KB |
Output is correct |
17 |
Correct |
35 ms |
87900 KB |
Output is correct |
18 |
Correct |
36 ms |
88156 KB |
Output is correct |
19 |
Correct |
52 ms |
87900 KB |
Output is correct |
20 |
Correct |
66 ms |
93064 KB |
Output is correct |
21 |
Correct |
57 ms |
92972 KB |
Output is correct |
22 |
Correct |
61 ms |
92916 KB |
Output is correct |
23 |
Correct |
59 ms |
93008 KB |
Output is correct |
24 |
Correct |
63 ms |
93248 KB |
Output is correct |
25 |
Correct |
60 ms |
93008 KB |
Output is correct |
26 |
Correct |
62 ms |
92952 KB |
Output is correct |
27 |
Correct |
55 ms |
93020 KB |
Output is correct |
28 |
Correct |
59 ms |
93040 KB |
Output is correct |
29 |
Correct |
58 ms |
93180 KB |
Output is correct |
30 |
Correct |
53 ms |
93012 KB |
Output is correct |
31 |
Correct |
50 ms |
93156 KB |
Output is correct |
32 |
Correct |
59 ms |
93220 KB |
Output is correct |
33 |
Correct |
57 ms |
93264 KB |
Output is correct |
34 |
Correct |
53 ms |
92960 KB |
Output is correct |
35 |
Correct |
56 ms |
93272 KB |
Output is correct |
36 |
Correct |
59 ms |
93264 KB |
Output is correct |
37 |
Correct |
55 ms |
93012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
640 ms |
215012 KB |
Output is correct |
2 |
Correct |
630 ms |
215256 KB |
Output is correct |
3 |
Correct |
604 ms |
217028 KB |
Output is correct |
4 |
Correct |
601 ms |
216260 KB |
Output is correct |
5 |
Correct |
464 ms |
218312 KB |
Output is correct |
6 |
Correct |
436 ms |
215496 KB |
Output is correct |
7 |
Correct |
417 ms |
215240 KB |
Output is correct |
8 |
Correct |
409 ms |
215288 KB |
Output is correct |
9 |
Correct |
332 ms |
215268 KB |
Output is correct |
10 |
Correct |
398 ms |
215280 KB |
Output is correct |
11 |
Correct |
787 ms |
219248 KB |
Output is correct |
12 |
Correct |
794 ms |
219012 KB |
Output is correct |
13 |
Correct |
797 ms |
218604 KB |
Output is correct |
14 |
Correct |
804 ms |
218312 KB |
Output is correct |
15 |
Correct |
766 ms |
215352 KB |
Output is correct |
16 |
Correct |
741 ms |
215236 KB |
Output is correct |
17 |
Correct |
740 ms |
214488 KB |
Output is correct |
18 |
Correct |
778 ms |
218296 KB |
Output is correct |
19 |
Correct |
36 ms |
87888 KB |
Output is correct |
20 |
Correct |
36 ms |
87884 KB |
Output is correct |
21 |
Correct |
37 ms |
87892 KB |
Output is correct |
22 |
Correct |
37 ms |
87900 KB |
Output is correct |
23 |
Correct |
37 ms |
87888 KB |
Output is correct |
24 |
Correct |
43 ms |
87900 KB |
Output is correct |
25 |
Correct |
36 ms |
87892 KB |
Output is correct |
26 |
Correct |
35 ms |
87900 KB |
Output is correct |
27 |
Correct |
36 ms |
87892 KB |
Output is correct |
28 |
Correct |
36 ms |
87916 KB |
Output is correct |
29 |
Correct |
34 ms |
88100 KB |
Output is correct |
30 |
Correct |
40 ms |
88048 KB |
Output is correct |
31 |
Correct |
35 ms |
87896 KB |
Output is correct |
32 |
Correct |
51 ms |
87972 KB |
Output is correct |
33 |
Correct |
33 ms |
87896 KB |
Output is correct |
34 |
Correct |
34 ms |
87888 KB |
Output is correct |
35 |
Correct |
35 ms |
87900 KB |
Output is correct |
36 |
Correct |
36 ms |
88156 KB |
Output is correct |
37 |
Correct |
52 ms |
87900 KB |
Output is correct |
38 |
Correct |
66 ms |
93064 KB |
Output is correct |
39 |
Correct |
57 ms |
92972 KB |
Output is correct |
40 |
Correct |
61 ms |
92916 KB |
Output is correct |
41 |
Correct |
59 ms |
93008 KB |
Output is correct |
42 |
Correct |
63 ms |
93248 KB |
Output is correct |
43 |
Correct |
60 ms |
93008 KB |
Output is correct |
44 |
Correct |
62 ms |
92952 KB |
Output is correct |
45 |
Correct |
55 ms |
93020 KB |
Output is correct |
46 |
Correct |
59 ms |
93040 KB |
Output is correct |
47 |
Correct |
58 ms |
93180 KB |
Output is correct |
48 |
Correct |
53 ms |
93012 KB |
Output is correct |
49 |
Correct |
50 ms |
93156 KB |
Output is correct |
50 |
Correct |
59 ms |
93220 KB |
Output is correct |
51 |
Correct |
57 ms |
93264 KB |
Output is correct |
52 |
Correct |
53 ms |
92960 KB |
Output is correct |
53 |
Correct |
56 ms |
93272 KB |
Output is correct |
54 |
Correct |
59 ms |
93264 KB |
Output is correct |
55 |
Correct |
55 ms |
93012 KB |
Output is correct |
56 |
Correct |
736 ms |
216600 KB |
Output is correct |
57 |
Correct |
870 ms |
216772 KB |
Output is correct |
58 |
Correct |
712 ms |
214548 KB |
Output is correct |
59 |
Correct |
678 ms |
214796 KB |
Output is correct |
60 |
Correct |
875 ms |
215500 KB |
Output is correct |
61 |
Correct |
666 ms |
214720 KB |
Output is correct |
62 |
Correct |
745 ms |
216776 KB |
Output is correct |
63 |
Correct |
702 ms |
215240 KB |
Output is correct |
64 |
Correct |
696 ms |
215236 KB |
Output is correct |
65 |
Correct |
692 ms |
215432 KB |
Output is correct |
66 |
Correct |
890 ms |
215492 KB |
Output is correct |
67 |
Correct |
1094 ms |
216008 KB |
Output is correct |
68 |
Correct |
1037 ms |
215896 KB |
Output is correct |
69 |
Correct |
680 ms |
216092 KB |
Output is correct |
70 |
Correct |
1112 ms |
216308 KB |
Output is correct |
71 |
Correct |
949 ms |
216008 KB |
Output is correct |
72 |
Correct |
642 ms |
216008 KB |
Output is correct |
73 |
Correct |
1140 ms |
216032 KB |
Output is correct |
74 |
Correct |
612 ms |
216004 KB |
Output is correct |
75 |
Correct |
488 ms |
215976 KB |
Output is correct |
76 |
Correct |
806 ms |
219588 KB |
Output is correct |
77 |
Correct |
1228 ms |
219592 KB |
Output is correct |
78 |
Correct |
821 ms |
218608 KB |
Output is correct |
79 |
Correct |
1328 ms |
215920 KB |
Output is correct |
80 |
Correct |
799 ms |
215744 KB |
Output is correct |
81 |
Correct |
927 ms |
216084 KB |
Output is correct |
82 |
Correct |
788 ms |
215120 KB |
Output is correct |
83 |
Correct |
995 ms |
215240 KB |
Output is correct |
84 |
Correct |
1299 ms |
215240 KB |
Output is correct |
85 |
Correct |
1030 ms |
216004 KB |
Output is correct |
86 |
Correct |
1053 ms |
216040 KB |
Output is correct |
87 |
Correct |
1114 ms |
215948 KB |
Output is correct |
88 |
Correct |
798 ms |
215972 KB |
Output is correct |
89 |
Correct |
1031 ms |
215840 KB |
Output is correct |
90 |
Correct |
1170 ms |
219848 KB |
Output is correct |
91 |
Correct |
1214 ms |
218040 KB |
Output is correct |
92 |
Correct |
1139 ms |
216008 KB |
Output is correct |
93 |
Correct |
1238 ms |
215912 KB |
Output is correct |
94 |
Correct |
864 ms |
216244 KB |
Output is correct |
95 |
Correct |
1206 ms |
216004 KB |
Output is correct |
96 |
Correct |
1278 ms |
219596 KB |
Output is correct |
97 |
Correct |
1268 ms |
219204 KB |
Output is correct |
98 |
Correct |
1250 ms |
219136 KB |
Output is correct |
99 |
Correct |
1236 ms |
219196 KB |
Output is correct |
100 |
Correct |
998 ms |
220516 KB |
Output is correct |
101 |
Correct |
1246 ms |
219276 KB |
Output is correct |
102 |
Correct |
1175 ms |
220028 KB |
Output is correct |
103 |
Correct |
1102 ms |
216540 KB |
Output is correct |