/*input
10 5
2 5 10 3
1 1 6 5
5 2 8 3
7 6 10 4
4 1 3 1
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <cassert>
#include <time.h>
using namespace std;
const int64_t INFLL = 1e17;
const int MAXN = 100007, INF = 2e9 + 9;
int N, M, T[MAXN], R[MAXN], L[MAXN], C[MAXN];
int64_t dist[MAXN];
struct Point
{
int x, y, id;
Point (int _x, int _y, int _id): x(_x), y(_y), id(_id) {}
};
// bool compY(const Point &p, const Point &q) { return p.y == q.y ? p.x < q.x : p.y < q.y; }
vector<Point> pnts;
int min_y[MAXN << 2];
priority_queue<pair<int64_t, int>, vector<pair<int64_t, int>>, greater<pair<int64_t, int>>> pq;
void build(int rt = 1, int lo = 0, int hi = M - 1)
{
if (lo == hi) {
min_y[rt] = pnts[lo].y;
return;
}
int md = (lo + hi) >> 1;
build(rt << 1, lo, md);
build(rt << 1 | 1, md + 1, hi);
min_y[rt] = min(min_y[rt << 1], min_y[rt << 1 | 1]);
}
void upd(int rx, int ry, int64_t val, int rt = 1, int lo = 0, int hi = M - 1)
{
if (rx < pnts[lo].x || min_y[rt] > ry) return;
if (pnts[hi].x <= rx && lo == hi) {
int id = pnts[lo].id;
if (dist[id] > val + C[id]) {
dist[id] = val + C[id];
pq.emplace(dist[id], id);
}
min_y[rt] = INF;
return;
}
int md = (lo + hi) >> 1;
upd(rx, ry, val, rt << 1, lo, md);
upd(rx, ry, val, rt << 1 | 1, md + 1, hi);
min_y[rt] = min(min_y[rt << 1], min_y[rt << 1 | 1]);
}
int main()
{
// freopen("in.txt", "r", stdin);
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> M;
pnts.reserve(M);
for (int i = 1; i <= M; ++i) {
cin >> T[i] >> L[i] >> R[i] >> C[i];
pnts.emplace_back(L[i] - T[i], L[i] + T[i], i);
}
sort(pnts.begin(), pnts.end(), [&](const Point &p, const Point &q) { return p.x == q.x ? p.y < q.y : p.x < q.x; });
build();
for (int i = 1; i <= M; ++i) {
if (L[i] == 1) {
dist[i] = C[i];
pq.emplace(dist[i], i);
} else {
dist[i] = INFLL;
}
}
while (!pq.empty()) {
int u = pq.top().second;
assert(dist[u] == pq.top().first);
pq.pop();
upd(1 + R[u] - T[u], 1 + R[u] + T[u], dist[u]);
}
int64_t ans = INFLL;
for (int i = 1; i <= M; ++i) if (R[i] == N) {
ans = min(ans, dist[i]);
}
if (ans == INFLL) ans = -1;
cout << ans << endl;
cerr << 1.0 * clock() / CLOCKS_PER_SEC << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
4856 KB |
Output is correct |
2 |
Correct |
86 ms |
4856 KB |
Output is correct |
3 |
Correct |
92 ms |
6132 KB |
Output is correct |
4 |
Correct |
88 ms |
5472 KB |
Output is correct |
5 |
Correct |
86 ms |
7152 KB |
Output is correct |
6 |
Correct |
81 ms |
5112 KB |
Output is correct |
7 |
Correct |
79 ms |
4856 KB |
Output is correct |
8 |
Correct |
73 ms |
4984 KB |
Output is correct |
9 |
Correct |
62 ms |
4984 KB |
Output is correct |
10 |
Correct |
76 ms |
4984 KB |
Output is correct |
11 |
Correct |
126 ms |
7152 KB |
Output is correct |
12 |
Correct |
96 ms |
7152 KB |
Output is correct |
13 |
Correct |
98 ms |
7128 KB |
Output is correct |
14 |
Correct |
96 ms |
7068 KB |
Output is correct |
15 |
Correct |
85 ms |
5240 KB |
Output is correct |
16 |
Correct |
91 ms |
5012 KB |
Output is correct |
17 |
Correct |
73 ms |
4856 KB |
Output is correct |
18 |
Correct |
84 ms |
7024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
384 KB |
Output is correct |
2 |
Correct |
7 ms |
432 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
384 KB |
Output is correct |
2 |
Correct |
7 ms |
432 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
9 ms |
768 KB |
Output is correct |
21 |
Correct |
9 ms |
768 KB |
Output is correct |
22 |
Correct |
10 ms |
640 KB |
Output is correct |
23 |
Correct |
9 ms |
640 KB |
Output is correct |
24 |
Correct |
9 ms |
768 KB |
Output is correct |
25 |
Correct |
9 ms |
640 KB |
Output is correct |
26 |
Correct |
9 ms |
640 KB |
Output is correct |
27 |
Correct |
9 ms |
640 KB |
Output is correct |
28 |
Correct |
9 ms |
768 KB |
Output is correct |
29 |
Correct |
11 ms |
640 KB |
Output is correct |
30 |
Correct |
8 ms |
640 KB |
Output is correct |
31 |
Correct |
8 ms |
640 KB |
Output is correct |
32 |
Correct |
9 ms |
768 KB |
Output is correct |
33 |
Correct |
9 ms |
768 KB |
Output is correct |
34 |
Correct |
9 ms |
640 KB |
Output is correct |
35 |
Correct |
11 ms |
768 KB |
Output is correct |
36 |
Correct |
10 ms |
896 KB |
Output is correct |
37 |
Correct |
8 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
4856 KB |
Output is correct |
2 |
Correct |
86 ms |
4856 KB |
Output is correct |
3 |
Correct |
92 ms |
6132 KB |
Output is correct |
4 |
Correct |
88 ms |
5472 KB |
Output is correct |
5 |
Correct |
86 ms |
7152 KB |
Output is correct |
6 |
Correct |
81 ms |
5112 KB |
Output is correct |
7 |
Correct |
79 ms |
4856 KB |
Output is correct |
8 |
Correct |
73 ms |
4984 KB |
Output is correct |
9 |
Correct |
62 ms |
4984 KB |
Output is correct |
10 |
Correct |
76 ms |
4984 KB |
Output is correct |
11 |
Correct |
126 ms |
7152 KB |
Output is correct |
12 |
Correct |
96 ms |
7152 KB |
Output is correct |
13 |
Correct |
98 ms |
7128 KB |
Output is correct |
14 |
Correct |
96 ms |
7068 KB |
Output is correct |
15 |
Correct |
85 ms |
5240 KB |
Output is correct |
16 |
Correct |
91 ms |
5012 KB |
Output is correct |
17 |
Correct |
73 ms |
4856 KB |
Output is correct |
18 |
Correct |
84 ms |
7024 KB |
Output is correct |
19 |
Correct |
8 ms |
384 KB |
Output is correct |
20 |
Correct |
7 ms |
432 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
6 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
4 ms |
384 KB |
Output is correct |
35 |
Correct |
5 ms |
384 KB |
Output is correct |
36 |
Correct |
5 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
9 ms |
768 KB |
Output is correct |
39 |
Correct |
9 ms |
768 KB |
Output is correct |
40 |
Correct |
10 ms |
640 KB |
Output is correct |
41 |
Correct |
9 ms |
640 KB |
Output is correct |
42 |
Correct |
9 ms |
768 KB |
Output is correct |
43 |
Correct |
9 ms |
640 KB |
Output is correct |
44 |
Correct |
9 ms |
640 KB |
Output is correct |
45 |
Correct |
9 ms |
640 KB |
Output is correct |
46 |
Correct |
9 ms |
768 KB |
Output is correct |
47 |
Correct |
11 ms |
640 KB |
Output is correct |
48 |
Correct |
8 ms |
640 KB |
Output is correct |
49 |
Correct |
8 ms |
640 KB |
Output is correct |
50 |
Correct |
9 ms |
768 KB |
Output is correct |
51 |
Correct |
9 ms |
768 KB |
Output is correct |
52 |
Correct |
9 ms |
640 KB |
Output is correct |
53 |
Correct |
11 ms |
768 KB |
Output is correct |
54 |
Correct |
10 ms |
896 KB |
Output is correct |
55 |
Correct |
8 ms |
640 KB |
Output is correct |
56 |
Correct |
101 ms |
6132 KB |
Output is correct |
57 |
Correct |
103 ms |
6004 KB |
Output is correct |
58 |
Correct |
98 ms |
4968 KB |
Output is correct |
59 |
Correct |
113 ms |
4984 KB |
Output is correct |
60 |
Correct |
110 ms |
4856 KB |
Output is correct |
61 |
Correct |
98 ms |
6264 KB |
Output is correct |
62 |
Correct |
103 ms |
6128 KB |
Output is correct |
63 |
Correct |
96 ms |
4856 KB |
Output is correct |
64 |
Correct |
98 ms |
4984 KB |
Output is correct |
65 |
Correct |
98 ms |
4856 KB |
Output is correct |
66 |
Correct |
101 ms |
4984 KB |
Output is correct |
67 |
Correct |
111 ms |
5112 KB |
Output is correct |
68 |
Correct |
117 ms |
4984 KB |
Output is correct |
69 |
Correct |
110 ms |
4856 KB |
Output is correct |
70 |
Correct |
120 ms |
6392 KB |
Output is correct |
71 |
Correct |
118 ms |
4984 KB |
Output is correct |
72 |
Correct |
121 ms |
4856 KB |
Output is correct |
73 |
Correct |
143 ms |
5096 KB |
Output is correct |
74 |
Correct |
79 ms |
4856 KB |
Output is correct |
75 |
Correct |
73 ms |
4844 KB |
Output is correct |
76 |
Correct |
92 ms |
7024 KB |
Output is correct |
77 |
Correct |
117 ms |
8944 KB |
Output is correct |
78 |
Correct |
98 ms |
7024 KB |
Output is correct |
79 |
Correct |
152 ms |
4984 KB |
Output is correct |
80 |
Correct |
88 ms |
4984 KB |
Output is correct |
81 |
Correct |
72 ms |
4984 KB |
Output is correct |
82 |
Correct |
97 ms |
5112 KB |
Output is correct |
83 |
Correct |
89 ms |
5112 KB |
Output is correct |
84 |
Correct |
134 ms |
5112 KB |
Output is correct |
85 |
Correct |
102 ms |
4984 KB |
Output is correct |
86 |
Correct |
100 ms |
5752 KB |
Output is correct |
87 |
Correct |
113 ms |
4856 KB |
Output is correct |
88 |
Correct |
183 ms |
4984 KB |
Output is correct |
89 |
Correct |
106 ms |
4984 KB |
Output is correct |
90 |
Correct |
117 ms |
7152 KB |
Output is correct |
91 |
Correct |
132 ms |
6132 KB |
Output is correct |
92 |
Correct |
116 ms |
4856 KB |
Output is correct |
93 |
Correct |
120 ms |
5088 KB |
Output is correct |
94 |
Correct |
101 ms |
6008 KB |
Output is correct |
95 |
Correct |
126 ms |
4988 KB |
Output is correct |
96 |
Correct |
108 ms |
7024 KB |
Output is correct |
97 |
Correct |
126 ms |
7024 KB |
Output is correct |
98 |
Correct |
106 ms |
7152 KB |
Output is correct |
99 |
Correct |
108 ms |
7152 KB |
Output is correct |
100 |
Correct |
98 ms |
7792 KB |
Output is correct |
101 |
Correct |
111 ms |
8028 KB |
Output is correct |
102 |
Correct |
101 ms |
7132 KB |
Output is correct |
103 |
Correct |
86 ms |
5240 KB |
Output is correct |