#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
template <class T, class... U> void bug_h(const T& t, const U&... u) { cerr << t; ((cerr << " | " << u), ...); cerr << endl; }
#define bug(...) cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: ", bug_h(__VA_ARGS__)
#else
#define cerr if (0) cerr
#define bug(...)
#endif
#define ar array
#define all(v) std::begin(v), std::end(v)
#define sz(v) int(std::size(v))
typedef long long i64;
typedef vector<int> vi;
typedef pair<int, int> pi;
vector<vector<pi>> P, Q;
struct segt {
int l, r;
segt *lc = NULL, *rc = NULL;
vector<pi> one, two;
segt(int l, int r) : l(l), r(r) {
if (l == r) {
one = move(P[l]), two = move(Q[l]);
sort(all(one), greater<>()), sort(all(two), greater<>());
} else {
int m = (l + r) / 2;
lc = new segt(l, m), rc = new segt(m + 1, r);
one.resize(sz(lc->one) + sz(rc->one));
std::merge(all(lc->one), all(rc->one), begin(one), greater<>());
two.resize(sz(lc->two) + sz(rc->two));
std::merge(all(lc->two), all(rc->two), begin(two), greater<>());
}
}
void qry_one(int ql, int qr, int x, vi& v) {
if (ql <= l && qr >= r) {
while (sz(one) && one.back().first <= x) {
v.push_back(one.back().second);
one.pop_back();
}
return;
}
if (ql <= lc->r) lc->qry_one(ql, qr, x, v);
if (qr >= rc->l) rc->qry_one(ql, qr, x, v);
}
void qry_two(int ql, int qr, int x, vi& v) {
if (ql <= l && qr >= r) {
while (sz(two) && two.back().first <= x) {
v.push_back(two.back().second);
two.pop_back();
}
return;
}
if (ql <= lc->r) lc->qry_two(ql, qr, x, v);
if (qr >= rc->l) rc->qry_two(ql, qr, x, v);
}
};
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int N, M;
cin >> N >> M;
vi T(M), L(M), R(M), C(M), index(M);
for (int i = 0; i < M; i++) cin >> T[i] >> L[i] >> R[i] >> C[i], L[i]--;
vi t = T;
sort(all(t)), t.erase(unique(all(t)), end(t));
P.resize(sz(t)), Q.resize(sz(t));
for (int i = 0; i < M; i++) {
index[i] = lower_bound(all(t), T[i]) - begin(t);
P[index[i]].push_back({L[i] + T[i], i});
Q[index[i]].push_back({L[i] - T[i], i});
}
segt *tree = new segt(0, sz(t) - 1);
vector<i64> dist(M, -1);
typedef pair<i64, int> U;
priority_queue<U, vector<U>, greater<U>> pq;
for (int i = 0; i < M; i++) if (L[i] == 0) pq.push({dist[i] = C[i], i});
while (sz(pq)) {
auto [d, i] = pq.top(); pq.pop();
vi v;
tree->qry_one(index[i], sz(t) - 1, R[i] + T[i], v);
tree->qry_two(0, index[i], R[i] - T[i], v);
for (int j : v) if (dist[j] == -1) pq.push({dist[j] = dist[i] + C[j], j});
}
i64 ans = LLONG_MAX;
for (int i = 0; i < M; i++) if (~dist[i] && R[i] == N)
ans = min(ans, dist[i]);
cout << (ans == LLONG_MAX ? -1 : ans) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
5312 KB |
Output is correct |
2 |
Correct |
48 ms |
5308 KB |
Output is correct |
3 |
Correct |
59 ms |
6848 KB |
Output is correct |
4 |
Correct |
57 ms |
6080 KB |
Output is correct |
5 |
Correct |
59 ms |
7272 KB |
Output is correct |
6 |
Correct |
54 ms |
5312 KB |
Output is correct |
7 |
Correct |
49 ms |
5376 KB |
Output is correct |
8 |
Correct |
45 ms |
5308 KB |
Output is correct |
9 |
Correct |
45 ms |
5308 KB |
Output is correct |
10 |
Correct |
42 ms |
5308 KB |
Output is correct |
11 |
Correct |
61 ms |
7908 KB |
Output is correct |
12 |
Correct |
62 ms |
7836 KB |
Output is correct |
13 |
Correct |
60 ms |
7276 KB |
Output is correct |
14 |
Correct |
61 ms |
7296 KB |
Output is correct |
15 |
Correct |
54 ms |
5512 KB |
Output is correct |
16 |
Correct |
54 ms |
5312 KB |
Output is correct |
17 |
Correct |
51 ms |
5376 KB |
Output is correct |
18 |
Correct |
58 ms |
7872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
0 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 |
0 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 |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
0 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 |
0 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 |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
5 ms |
2140 KB |
Output is correct |
21 |
Correct |
6 ms |
2140 KB |
Output is correct |
22 |
Correct |
5 ms |
1572 KB |
Output is correct |
23 |
Correct |
4 ms |
1372 KB |
Output is correct |
24 |
Correct |
7 ms |
2908 KB |
Output is correct |
25 |
Correct |
7 ms |
3040 KB |
Output is correct |
26 |
Correct |
7 ms |
2908 KB |
Output is correct |
27 |
Correct |
7 ms |
2908 KB |
Output is correct |
28 |
Correct |
7 ms |
2908 KB |
Output is correct |
29 |
Correct |
7 ms |
2908 KB |
Output is correct |
30 |
Correct |
4 ms |
2908 KB |
Output is correct |
31 |
Correct |
4 ms |
2908 KB |
Output is correct |
32 |
Correct |
7 ms |
3028 KB |
Output is correct |
33 |
Correct |
7 ms |
3164 KB |
Output is correct |
34 |
Correct |
7 ms |
2396 KB |
Output is correct |
35 |
Correct |
7 ms |
3052 KB |
Output is correct |
36 |
Correct |
8 ms |
3164 KB |
Output is correct |
37 |
Correct |
7 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
5312 KB |
Output is correct |
2 |
Correct |
48 ms |
5308 KB |
Output is correct |
3 |
Correct |
59 ms |
6848 KB |
Output is correct |
4 |
Correct |
57 ms |
6080 KB |
Output is correct |
5 |
Correct |
59 ms |
7272 KB |
Output is correct |
6 |
Correct |
54 ms |
5312 KB |
Output is correct |
7 |
Correct |
49 ms |
5376 KB |
Output is correct |
8 |
Correct |
45 ms |
5308 KB |
Output is correct |
9 |
Correct |
45 ms |
5308 KB |
Output is correct |
10 |
Correct |
42 ms |
5308 KB |
Output is correct |
11 |
Correct |
61 ms |
7908 KB |
Output is correct |
12 |
Correct |
62 ms |
7836 KB |
Output is correct |
13 |
Correct |
60 ms |
7276 KB |
Output is correct |
14 |
Correct |
61 ms |
7296 KB |
Output is correct |
15 |
Correct |
54 ms |
5512 KB |
Output is correct |
16 |
Correct |
54 ms |
5312 KB |
Output is correct |
17 |
Correct |
51 ms |
5376 KB |
Output is correct |
18 |
Correct |
58 ms |
7872 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 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 |
0 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 |
0 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 |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
5 ms |
2140 KB |
Output is correct |
39 |
Correct |
6 ms |
2140 KB |
Output is correct |
40 |
Correct |
5 ms |
1572 KB |
Output is correct |
41 |
Correct |
4 ms |
1372 KB |
Output is correct |
42 |
Correct |
7 ms |
2908 KB |
Output is correct |
43 |
Correct |
7 ms |
3040 KB |
Output is correct |
44 |
Correct |
7 ms |
2908 KB |
Output is correct |
45 |
Correct |
7 ms |
2908 KB |
Output is correct |
46 |
Correct |
7 ms |
2908 KB |
Output is correct |
47 |
Correct |
7 ms |
2908 KB |
Output is correct |
48 |
Correct |
4 ms |
2908 KB |
Output is correct |
49 |
Correct |
4 ms |
2908 KB |
Output is correct |
50 |
Correct |
7 ms |
3028 KB |
Output is correct |
51 |
Correct |
7 ms |
3164 KB |
Output is correct |
52 |
Correct |
7 ms |
2396 KB |
Output is correct |
53 |
Correct |
7 ms |
3052 KB |
Output is correct |
54 |
Correct |
8 ms |
3164 KB |
Output is correct |
55 |
Correct |
7 ms |
2396 KB |
Output is correct |
56 |
Correct |
139 ms |
46700 KB |
Output is correct |
57 |
Correct |
143 ms |
47036 KB |
Output is correct |
58 |
Correct |
123 ms |
45776 KB |
Output is correct |
59 |
Correct |
130 ms |
46444 KB |
Output is correct |
60 |
Correct |
94 ms |
33740 KB |
Output is correct |
61 |
Correct |
129 ms |
45816 KB |
Output is correct |
62 |
Correct |
136 ms |
46784 KB |
Output is correct |
63 |
Correct |
130 ms |
62736 KB |
Output is correct |
64 |
Correct |
127 ms |
62672 KB |
Output is correct |
65 |
Correct |
57 ms |
9956 KB |
Output is correct |
66 |
Correct |
86 ms |
33084 KB |
Output is correct |
67 |
Correct |
222 ms |
63032 KB |
Output is correct |
68 |
Correct |
176 ms |
63052 KB |
Output is correct |
69 |
Correct |
166 ms |
59988 KB |
Output is correct |
70 |
Correct |
216 ms |
62800 KB |
Output is correct |
71 |
Correct |
182 ms |
62936 KB |
Output is correct |
72 |
Correct |
164 ms |
62548 KB |
Output is correct |
73 |
Correct |
230 ms |
62976 KB |
Output is correct |
74 |
Correct |
91 ms |
62696 KB |
Output is correct |
75 |
Correct |
91 ms |
62548 KB |
Output is correct |
76 |
Correct |
182 ms |
53264 KB |
Output is correct |
77 |
Correct |
229 ms |
65168 KB |
Output is correct |
78 |
Correct |
207 ms |
52816 KB |
Output is correct |
79 |
Correct |
257 ms |
63276 KB |
Output is correct |
80 |
Correct |
218 ms |
51060 KB |
Output is correct |
81 |
Correct |
122 ms |
62548 KB |
Output is correct |
82 |
Correct |
210 ms |
50976 KB |
Output is correct |
83 |
Correct |
221 ms |
62152 KB |
Output is correct |
84 |
Correct |
230 ms |
62036 KB |
Output is correct |
85 |
Correct |
196 ms |
62792 KB |
Output is correct |
86 |
Correct |
161 ms |
62676 KB |
Output is correct |
87 |
Correct |
185 ms |
62864 KB |
Output is correct |
88 |
Correct |
189 ms |
63404 KB |
Output is correct |
89 |
Correct |
176 ms |
62756 KB |
Output is correct |
90 |
Correct |
256 ms |
65060 KB |
Output is correct |
91 |
Correct |
199 ms |
64100 KB |
Output is correct |
92 |
Correct |
175 ms |
62804 KB |
Output is correct |
93 |
Correct |
232 ms |
62696 KB |
Output is correct |
94 |
Correct |
208 ms |
63292 KB |
Output is correct |
95 |
Correct |
213 ms |
62692 KB |
Output is correct |
96 |
Correct |
221 ms |
65248 KB |
Output is correct |
97 |
Correct |
222 ms |
64932 KB |
Output is correct |
98 |
Correct |
213 ms |
65004 KB |
Output is correct |
99 |
Correct |
227 ms |
64968 KB |
Output is correct |
100 |
Correct |
217 ms |
65140 KB |
Output is correct |
101 |
Correct |
217 ms |
64912 KB |
Output is correct |
102 |
Correct |
215 ms |
65152 KB |
Output is correct |
103 |
Correct |
163 ms |
63104 KB |
Output is correct |