#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll inf = 1e18;
const int l2 = 2e9 + 10;
struct SegmentTree {
int n;
vector<pair<int, int>> t;
vector<multiset<pair<int, int>>> mst;
SegmentTree(int _n) {
n = _n;
t.assign(4 * n, {l2, l2});
mst.resize(n + 1);
}
void update(int x, int lx, int rx, int i, pair<int, int> v, bool erase) {
if (lx == rx) {
if (erase) mst[lx].erase(mst[lx].find(v));
else mst[lx].insert(v);
if (mst[lx].empty()) t[x] = {l2, l2};
else t[x] = *mst[lx].begin();
return;
}
int mx = (lx + rx) >> 1;
if (i <= mx) update(x << 1, lx, mx, i, v, erase);
else update(x << 1 | 1, mx + 1, rx, i, v, erase);
t[x] = min(t[x << 1], t[x << 1 | 1]);
}
pair<int, int> query(int x, int lx, int rx, int l, int r) {
if (l > rx || lx > r || l > r) return {l2, l2};
if (l <= lx && r >= rx) return t[x];
int mx = (lx + rx) >> 1;
return min(query(x << 1, lx, mx, l, r), query(x << 1 | 1, mx + 1, rx, l, r));
}
};
void testCase() {
int n, m;
cin >> n >> m;
vector<int> t(m), l(m), r(m), c(m), all, tt(m);
for (int i = 0; i < m; i++) {
cin >> t[i] >> l[i] >> r[i] >> c[i];
all.push_back(t[i]);
}
sort(all.begin(), all.end());
for (int i = 0; i < m; i++) {
tt[i] = lower_bound(all.begin(), all.end(), t[i]) - all.begin() + 1;
}
SegmentTree st1(m), st2(m);
for (int i = 0; i < m; i++) {
st1.update(1, 1, m, tt[i], {l[i] - t[i], i}, false);
st2.update(1, 1, m, tt[i], {l[i] + t[i], i}, false);
}
priority_queue<pair<ll, int>> pq;
vector<bool> vis(m);
vector<ll> dist(m, inf);
for (int i = 0; i < m; i++) {
if (l[i] == 1) {
st1.update(1, 1, m, tt[i], {l[i] - t[i], i}, true);
st2.update(1, 1, m, tt[i], {l[i] + t[i], i}, true);
dist[i] = c[i];
pq.push({-dist[i], i});
}
}
while (!pq.empty()) {
auto [_, u] = pq.top();
pq.pop();
if (vis[u]) continue;
vis[u] = true;
auto q = st1.query(1, 1, m, 1, tt[u]);
while (q.first <= r[u] - t[u] + 1) {
auto [v, i] = q;
st1.update(1, 1, m, tt[i], {l[i] - t[i], i}, true);
st2.update(1, 1, m, tt[i], {l[i] + t[i], i}, true);
dist[i] = dist[u] + c[i];
pq.push({-dist[i], i});
q = st1.query(1, 1, m, 1, tt[u]);
}
q = st2.query(1, 1, m, tt[u], m);
while (q.first <= r[u] + t[u] + 1) {
auto [v, i] = q;
st1.update(1, 1, m, tt[i], {l[i] - t[i], i}, true);
st2.update(1, 1, m, tt[i], {l[i] + t[i], i}, true);
dist[i] = dist[u] + c[i];
pq.push({-dist[i], i});
q = st2.query(1, 1, m, tt[u], m);
}
}
ll ans = inf;
for (int i = 0; i < m; i++) {
if (r[i] == n) ans = min(ans, dist[i]);
}
cout << (ans == inf ? -1 : ans) << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
testCase();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
164 ms |
28400 KB |
Output is correct |
2 |
Correct |
123 ms |
31824 KB |
Output is correct |
3 |
Correct |
155 ms |
31876 KB |
Output is correct |
4 |
Correct |
166 ms |
31944 KB |
Output is correct |
5 |
Correct |
160 ms |
33716 KB |
Output is correct |
6 |
Correct |
137 ms |
31772 KB |
Output is correct |
7 |
Correct |
145 ms |
31620 KB |
Output is correct |
8 |
Correct |
96 ms |
31780 KB |
Output is correct |
9 |
Correct |
82 ms |
31648 KB |
Output is correct |
10 |
Correct |
81 ms |
31704 KB |
Output is correct |
11 |
Correct |
175 ms |
34000 KB |
Output is correct |
12 |
Correct |
202 ms |
33764 KB |
Output is correct |
13 |
Correct |
213 ms |
33884 KB |
Output is correct |
14 |
Correct |
168 ms |
33924 KB |
Output is correct |
15 |
Correct |
175 ms |
31816 KB |
Output is correct |
16 |
Correct |
190 ms |
31700 KB |
Output is correct |
17 |
Correct |
171 ms |
31028 KB |
Output is correct |
18 |
Correct |
169 ms |
33252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 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 |
428 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 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 |
428 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
8 ms |
1976 KB |
Output is correct |
21 |
Correct |
8 ms |
1884 KB |
Output is correct |
22 |
Correct |
9 ms |
2004 KB |
Output is correct |
23 |
Correct |
7 ms |
2088 KB |
Output is correct |
24 |
Correct |
8 ms |
2140 KB |
Output is correct |
25 |
Correct |
8 ms |
1944 KB |
Output is correct |
26 |
Correct |
10 ms |
2028 KB |
Output is correct |
27 |
Correct |
9 ms |
2336 KB |
Output is correct |
28 |
Correct |
9 ms |
2140 KB |
Output is correct |
29 |
Correct |
11 ms |
1884 KB |
Output is correct |
30 |
Correct |
4 ms |
1880 KB |
Output is correct |
31 |
Correct |
5 ms |
1880 KB |
Output is correct |
32 |
Correct |
9 ms |
2140 KB |
Output is correct |
33 |
Correct |
10 ms |
2136 KB |
Output is correct |
34 |
Correct |
9 ms |
1884 KB |
Output is correct |
35 |
Correct |
10 ms |
2508 KB |
Output is correct |
36 |
Correct |
9 ms |
2140 KB |
Output is correct |
37 |
Correct |
9 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
164 ms |
28400 KB |
Output is correct |
2 |
Correct |
123 ms |
31824 KB |
Output is correct |
3 |
Correct |
155 ms |
31876 KB |
Output is correct |
4 |
Correct |
166 ms |
31944 KB |
Output is correct |
5 |
Correct |
160 ms |
33716 KB |
Output is correct |
6 |
Correct |
137 ms |
31772 KB |
Output is correct |
7 |
Correct |
145 ms |
31620 KB |
Output is correct |
8 |
Correct |
96 ms |
31780 KB |
Output is correct |
9 |
Correct |
82 ms |
31648 KB |
Output is correct |
10 |
Correct |
81 ms |
31704 KB |
Output is correct |
11 |
Correct |
175 ms |
34000 KB |
Output is correct |
12 |
Correct |
202 ms |
33764 KB |
Output is correct |
13 |
Correct |
213 ms |
33884 KB |
Output is correct |
14 |
Correct |
168 ms |
33924 KB |
Output is correct |
15 |
Correct |
175 ms |
31816 KB |
Output is correct |
16 |
Correct |
190 ms |
31700 KB |
Output is correct |
17 |
Correct |
171 ms |
31028 KB |
Output is correct |
18 |
Correct |
169 ms |
33252 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 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 |
428 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
8 ms |
1976 KB |
Output is correct |
39 |
Correct |
8 ms |
1884 KB |
Output is correct |
40 |
Correct |
9 ms |
2004 KB |
Output is correct |
41 |
Correct |
7 ms |
2088 KB |
Output is correct |
42 |
Correct |
8 ms |
2140 KB |
Output is correct |
43 |
Correct |
8 ms |
1944 KB |
Output is correct |
44 |
Correct |
10 ms |
2028 KB |
Output is correct |
45 |
Correct |
9 ms |
2336 KB |
Output is correct |
46 |
Correct |
9 ms |
2140 KB |
Output is correct |
47 |
Correct |
11 ms |
1884 KB |
Output is correct |
48 |
Correct |
4 ms |
1880 KB |
Output is correct |
49 |
Correct |
5 ms |
1880 KB |
Output is correct |
50 |
Correct |
9 ms |
2140 KB |
Output is correct |
51 |
Correct |
10 ms |
2136 KB |
Output is correct |
52 |
Correct |
9 ms |
1884 KB |
Output is correct |
53 |
Correct |
10 ms |
2508 KB |
Output is correct |
54 |
Correct |
9 ms |
2140 KB |
Output is correct |
55 |
Correct |
9 ms |
1884 KB |
Output is correct |
56 |
Correct |
269 ms |
32092 KB |
Output is correct |
57 |
Correct |
267 ms |
32276 KB |
Output is correct |
58 |
Correct |
222 ms |
30972 KB |
Output is correct |
59 |
Correct |
239 ms |
31188 KB |
Output is correct |
60 |
Correct |
274 ms |
32196 KB |
Output is correct |
61 |
Correct |
210 ms |
30908 KB |
Output is correct |
62 |
Correct |
226 ms |
32080 KB |
Output is correct |
63 |
Correct |
205 ms |
31868 KB |
Output is correct |
64 |
Correct |
207 ms |
31880 KB |
Output is correct |
65 |
Correct |
171 ms |
31872 KB |
Output is correct |
66 |
Correct |
220 ms |
31868 KB |
Output is correct |
67 |
Correct |
274 ms |
32896 KB |
Output is correct |
68 |
Correct |
245 ms |
32536 KB |
Output is correct |
69 |
Correct |
212 ms |
32392 KB |
Output is correct |
70 |
Correct |
251 ms |
32572 KB |
Output is correct |
71 |
Correct |
232 ms |
32376 KB |
Output is correct |
72 |
Correct |
211 ms |
32544 KB |
Output is correct |
73 |
Correct |
289 ms |
32908 KB |
Output is correct |
74 |
Correct |
119 ms |
32312 KB |
Output is correct |
75 |
Correct |
104 ms |
32344 KB |
Output is correct |
76 |
Correct |
326 ms |
34164 KB |
Output is correct |
77 |
Correct |
276 ms |
34752 KB |
Output is correct |
78 |
Correct |
306 ms |
34260 KB |
Output is correct |
79 |
Correct |
291 ms |
32692 KB |
Output is correct |
80 |
Correct |
269 ms |
31952 KB |
Output is correct |
81 |
Correct |
133 ms |
32344 KB |
Output is correct |
82 |
Correct |
304 ms |
31564 KB |
Output is correct |
83 |
Correct |
316 ms |
31956 KB |
Output is correct |
84 |
Correct |
263 ms |
31612 KB |
Output is correct |
85 |
Correct |
210 ms |
32536 KB |
Output is correct |
86 |
Correct |
223 ms |
32360 KB |
Output is correct |
87 |
Correct |
216 ms |
32432 KB |
Output is correct |
88 |
Correct |
254 ms |
32292 KB |
Output is correct |
89 |
Correct |
206 ms |
32372 KB |
Output is correct |
90 |
Correct |
284 ms |
34696 KB |
Output is correct |
91 |
Correct |
294 ms |
33912 KB |
Output is correct |
92 |
Correct |
242 ms |
32476 KB |
Output is correct |
93 |
Correct |
305 ms |
32716 KB |
Output is correct |
94 |
Correct |
289 ms |
32572 KB |
Output is correct |
95 |
Correct |
265 ms |
32488 KB |
Output is correct |
96 |
Correct |
293 ms |
34516 KB |
Output is correct |
97 |
Correct |
312 ms |
34516 KB |
Output is correct |
98 |
Correct |
274 ms |
34924 KB |
Output is correct |
99 |
Correct |
315 ms |
34556 KB |
Output is correct |
100 |
Correct |
298 ms |
34744 KB |
Output is correct |
101 |
Correct |
292 ms |
34640 KB |
Output is correct |
102 |
Correct |
301 ms |
34700 KB |
Output is correct |
103 |
Correct |
225 ms |
32956 KB |
Output is correct |