#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int M = 1e5 + 5, MAX = 2e9 + 2;
const long long inf = 1e18;
int n, m, k;
int t[M], l[M], r[M], c[M], pos[M];
array<int, 2> A[4 * M], B[4 * M];
bool del[M];
long long d[M];
vector<array<int, 2>> pf[M], sf[M];
void upd(vector<array<int, 2>> &cands) {
while (cands.size() > 1 && del[cands.back()[1]]) {
cands.pop_back();
}
}
void pul(int id) {
A[id] = min(A[id * 2], A[id * 2 + 1]);
B[id] = min(B[id * 2], B[id * 2 + 1]);
}
void bld(int id = 1, int l = 1, int r = k) {
if (l == r) {
A[id] = pf[l].back();
B[id] = sf[l].back();
return;
}
int md = (l + r) / 2;
bld(id * 2, l, md);
bld(id * 2 + 1, md + 1, r);
pul(id);
}
void ers(int i, int id = 1, int l = 1, int r = k) {
if (l == r) {
upd(pf[i]);
upd(sf[i]);
A[id] = pf[i].back();
B[id] = sf[i].back();
return;
}
int md = (l + r) / 2;
if (i <= md) {
ers(i, id * 2, l, md);
} else {
ers(i, id * 2 + 1, md + 1, r);
}
pul(id);
}
array<int, 2> qry(int u, int v, array<int, 2> *s, int id = 1, int l = 1, int r = k) {
if (u <= l && r <= v) {
return s[id];
}
int md = (l + r) / 2;
if (v <= md) {
return qry(u, v, s, id * 2, l, md);
}
if (md < u) {
return qry(u, v, s, id * 2 + 1, md + 1, r);
}
return min(qry(u, v, s, id * 2, l, md), qry(u, v, s, id * 2 + 1, md + 1, r));
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> m;
vector<int> comp;
for (int i = 1; i <= m; ++i) {
cin >> t[i] >> l[i] >> r[i] >> c[i];
comp.push_back(t[i]);
}
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
k = comp.size();
for (int i = 1; i <= m; ++i) {
pos[i] = lower_bound(comp.begin(), comp.end(), t[i]) - comp.begin() + 1;
pf[pos[i]].push_back({l[i] - t[i], i});
sf[pos[i]].push_back({l[i] + t[i], i});
}
for (int i = 1; i <= k; ++i) {
pf[i].push_back({MAX, -1});
sf[i].push_back({MAX, -1});
sort(pf[i].rbegin(), pf[i].rend());
sort(sf[i].rbegin(), sf[i].rend());
}
using T = pair<long long, int>;
priority_queue<T, vector<T>, greater<T>> pq;
bld();;
fill(d + 1, d + m + 1, inf);
for (int i = 1; i <= m; ++i) {
if (l[i] == 1) {
del[i] = 1;
ers(pos[i]);
pq.push({d[i] = c[i], i});
}
}
while (pq.size()) {
auto [x, u] = pq.top(); pq.pop();
if (x != d[u]) {
continue;
}
while (1) {
auto dat = qry(1, pos[u], A);
if (dat[0] > r[u] - t[u] + 1) {
break;
}
int v = dat[1];
pq.push({d[v] = x + c[v], v});
del[v] = 1;
ers(pos[v]);
}
while (1) {
auto dat = qry(pos[u], k, B);
if (dat[0] > r[u] + t[u] + 1) {
break;
}
int v = dat[1];
pq.push({d[v] = x + c[v], v});
del[v] = 1;
ers(pos[v]);
}
}
auto res = inf;
for (int i = 1; i <= m; ++i) {
if (r[i] == n) {
res = min(res, d[i]);
}
}
cout << (res == inf ? -1 : res);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
18228 KB |
Output is correct |
2 |
Correct |
39 ms |
18144 KB |
Output is correct |
3 |
Correct |
57 ms |
17872 KB |
Output is correct |
4 |
Correct |
48 ms |
17240 KB |
Output is correct |
5 |
Correct |
53 ms |
19280 KB |
Output is correct |
6 |
Correct |
47 ms |
17868 KB |
Output is correct |
7 |
Correct |
42 ms |
17868 KB |
Output is correct |
8 |
Correct |
40 ms |
17876 KB |
Output is correct |
9 |
Correct |
39 ms |
17868 KB |
Output is correct |
10 |
Correct |
36 ms |
18132 KB |
Output is correct |
11 |
Correct |
54 ms |
19456 KB |
Output is correct |
12 |
Correct |
51 ms |
19280 KB |
Output is correct |
13 |
Correct |
60 ms |
19276 KB |
Output is correct |
14 |
Correct |
51 ms |
19276 KB |
Output is correct |
15 |
Correct |
45 ms |
18120 KB |
Output is correct |
16 |
Correct |
49 ms |
17868 KB |
Output is correct |
17 |
Correct |
53 ms |
17356 KB |
Output is correct |
18 |
Correct |
55 ms |
18520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
1 ms |
10844 KB |
Output is correct |
3 |
Correct |
1 ms |
10844 KB |
Output is correct |
4 |
Correct |
1 ms |
10844 KB |
Output is correct |
5 |
Correct |
1 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
1 ms |
10844 KB |
Output is correct |
8 |
Correct |
1 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10884 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10840 KB |
Output is correct |
12 |
Correct |
1 ms |
10844 KB |
Output is correct |
13 |
Correct |
1 ms |
10844 KB |
Output is correct |
14 |
Correct |
1 ms |
10844 KB |
Output is correct |
15 |
Correct |
1 ms |
10844 KB |
Output is correct |
16 |
Correct |
1 ms |
10844 KB |
Output is correct |
17 |
Correct |
1 ms |
10844 KB |
Output is correct |
18 |
Correct |
1 ms |
10844 KB |
Output is correct |
19 |
Correct |
1 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
1 ms |
10844 KB |
Output is correct |
3 |
Correct |
1 ms |
10844 KB |
Output is correct |
4 |
Correct |
1 ms |
10844 KB |
Output is correct |
5 |
Correct |
1 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
1 ms |
10844 KB |
Output is correct |
8 |
Correct |
1 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10884 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10840 KB |
Output is correct |
12 |
Correct |
1 ms |
10844 KB |
Output is correct |
13 |
Correct |
1 ms |
10844 KB |
Output is correct |
14 |
Correct |
1 ms |
10844 KB |
Output is correct |
15 |
Correct |
1 ms |
10844 KB |
Output is correct |
16 |
Correct |
1 ms |
10844 KB |
Output is correct |
17 |
Correct |
1 ms |
10844 KB |
Output is correct |
18 |
Correct |
1 ms |
10844 KB |
Output is correct |
19 |
Correct |
1 ms |
10844 KB |
Output is correct |
20 |
Correct |
5 ms |
11356 KB |
Output is correct |
21 |
Correct |
6 ms |
11492 KB |
Output is correct |
22 |
Correct |
5 ms |
11276 KB |
Output is correct |
23 |
Correct |
4 ms |
11356 KB |
Output is correct |
24 |
Correct |
5 ms |
11612 KB |
Output is correct |
25 |
Correct |
5 ms |
11356 KB |
Output is correct |
26 |
Correct |
5 ms |
11356 KB |
Output is correct |
27 |
Correct |
5 ms |
11356 KB |
Output is correct |
28 |
Correct |
5 ms |
11612 KB |
Output is correct |
29 |
Correct |
5 ms |
11356 KB |
Output is correct |
30 |
Correct |
4 ms |
11356 KB |
Output is correct |
31 |
Correct |
4 ms |
11356 KB |
Output is correct |
32 |
Correct |
6 ms |
11612 KB |
Output is correct |
33 |
Correct |
5 ms |
11612 KB |
Output is correct |
34 |
Correct |
6 ms |
11368 KB |
Output is correct |
35 |
Correct |
6 ms |
11612 KB |
Output is correct |
36 |
Correct |
6 ms |
11612 KB |
Output is correct |
37 |
Correct |
6 ms |
11356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
18228 KB |
Output is correct |
2 |
Correct |
39 ms |
18144 KB |
Output is correct |
3 |
Correct |
57 ms |
17872 KB |
Output is correct |
4 |
Correct |
48 ms |
17240 KB |
Output is correct |
5 |
Correct |
53 ms |
19280 KB |
Output is correct |
6 |
Correct |
47 ms |
17868 KB |
Output is correct |
7 |
Correct |
42 ms |
17868 KB |
Output is correct |
8 |
Correct |
40 ms |
17876 KB |
Output is correct |
9 |
Correct |
39 ms |
17868 KB |
Output is correct |
10 |
Correct |
36 ms |
18132 KB |
Output is correct |
11 |
Correct |
54 ms |
19456 KB |
Output is correct |
12 |
Correct |
51 ms |
19280 KB |
Output is correct |
13 |
Correct |
60 ms |
19276 KB |
Output is correct |
14 |
Correct |
51 ms |
19276 KB |
Output is correct |
15 |
Correct |
45 ms |
18120 KB |
Output is correct |
16 |
Correct |
49 ms |
17868 KB |
Output is correct |
17 |
Correct |
53 ms |
17356 KB |
Output is correct |
18 |
Correct |
55 ms |
18520 KB |
Output is correct |
19 |
Correct |
2 ms |
10844 KB |
Output is correct |
20 |
Correct |
1 ms |
10844 KB |
Output is correct |
21 |
Correct |
1 ms |
10844 KB |
Output is correct |
22 |
Correct |
1 ms |
10844 KB |
Output is correct |
23 |
Correct |
1 ms |
10844 KB |
Output is correct |
24 |
Correct |
2 ms |
10844 KB |
Output is correct |
25 |
Correct |
1 ms |
10844 KB |
Output is correct |
26 |
Correct |
1 ms |
10844 KB |
Output is correct |
27 |
Correct |
2 ms |
10884 KB |
Output is correct |
28 |
Correct |
2 ms |
10844 KB |
Output is correct |
29 |
Correct |
2 ms |
10840 KB |
Output is correct |
30 |
Correct |
1 ms |
10844 KB |
Output is correct |
31 |
Correct |
1 ms |
10844 KB |
Output is correct |
32 |
Correct |
1 ms |
10844 KB |
Output is correct |
33 |
Correct |
1 ms |
10844 KB |
Output is correct |
34 |
Correct |
1 ms |
10844 KB |
Output is correct |
35 |
Correct |
1 ms |
10844 KB |
Output is correct |
36 |
Correct |
1 ms |
10844 KB |
Output is correct |
37 |
Correct |
1 ms |
10844 KB |
Output is correct |
38 |
Correct |
5 ms |
11356 KB |
Output is correct |
39 |
Correct |
6 ms |
11492 KB |
Output is correct |
40 |
Correct |
5 ms |
11276 KB |
Output is correct |
41 |
Correct |
4 ms |
11356 KB |
Output is correct |
42 |
Correct |
5 ms |
11612 KB |
Output is correct |
43 |
Correct |
5 ms |
11356 KB |
Output is correct |
44 |
Correct |
5 ms |
11356 KB |
Output is correct |
45 |
Correct |
5 ms |
11356 KB |
Output is correct |
46 |
Correct |
5 ms |
11612 KB |
Output is correct |
47 |
Correct |
5 ms |
11356 KB |
Output is correct |
48 |
Correct |
4 ms |
11356 KB |
Output is correct |
49 |
Correct |
4 ms |
11356 KB |
Output is correct |
50 |
Correct |
6 ms |
11612 KB |
Output is correct |
51 |
Correct |
5 ms |
11612 KB |
Output is correct |
52 |
Correct |
6 ms |
11368 KB |
Output is correct |
53 |
Correct |
6 ms |
11612 KB |
Output is correct |
54 |
Correct |
6 ms |
11612 KB |
Output is correct |
55 |
Correct |
6 ms |
11356 KB |
Output is correct |
56 |
Correct |
100 ms |
23756 KB |
Output is correct |
57 |
Correct |
104 ms |
24184 KB |
Output is correct |
58 |
Correct |
85 ms |
22480 KB |
Output is correct |
59 |
Correct |
94 ms |
22992 KB |
Output is correct |
60 |
Correct |
73 ms |
19152 KB |
Output is correct |
61 |
Correct |
80 ms |
22352 KB |
Output is correct |
62 |
Correct |
96 ms |
23896 KB |
Output is correct |
63 |
Correct |
82 ms |
24016 KB |
Output is correct |
64 |
Correct |
92 ms |
23956 KB |
Output is correct |
65 |
Correct |
48 ms |
17768 KB |
Output is correct |
66 |
Correct |
60 ms |
18900 KB |
Output is correct |
67 |
Correct |
100 ms |
24784 KB |
Output is correct |
68 |
Correct |
113 ms |
24784 KB |
Output is correct |
69 |
Correct |
104 ms |
24684 KB |
Output is correct |
70 |
Correct |
108 ms |
24872 KB |
Output is correct |
71 |
Correct |
104 ms |
24524 KB |
Output is correct |
72 |
Correct |
98 ms |
24640 KB |
Output is correct |
73 |
Correct |
124 ms |
24796 KB |
Output is correct |
74 |
Correct |
53 ms |
24544 KB |
Output is correct |
75 |
Correct |
48 ms |
24784 KB |
Output is correct |
76 |
Correct |
111 ms |
25940 KB |
Output is correct |
77 |
Correct |
116 ms |
26956 KB |
Output is correct |
78 |
Correct |
114 ms |
25680 KB |
Output is correct |
79 |
Correct |
140 ms |
24816 KB |
Output is correct |
80 |
Correct |
110 ms |
23768 KB |
Output is correct |
81 |
Correct |
57 ms |
24768 KB |
Output is correct |
82 |
Correct |
114 ms |
23120 KB |
Output is correct |
83 |
Correct |
115 ms |
24028 KB |
Output is correct |
84 |
Correct |
111 ms |
24012 KB |
Output is correct |
85 |
Correct |
88 ms |
24620 KB |
Output is correct |
86 |
Correct |
91 ms |
24624 KB |
Output is correct |
87 |
Correct |
93 ms |
24744 KB |
Output is correct |
88 |
Correct |
107 ms |
24724 KB |
Output is correct |
89 |
Correct |
97 ms |
24532 KB |
Output is correct |
90 |
Correct |
121 ms |
26788 KB |
Output is correct |
91 |
Correct |
109 ms |
25872 KB |
Output is correct |
92 |
Correct |
89 ms |
24972 KB |
Output is correct |
93 |
Correct |
107 ms |
24784 KB |
Output is correct |
94 |
Correct |
117 ms |
24800 KB |
Output is correct |
95 |
Correct |
105 ms |
24836 KB |
Output is correct |
96 |
Correct |
111 ms |
26960 KB |
Output is correct |
97 |
Correct |
113 ms |
26796 KB |
Output is correct |
98 |
Correct |
123 ms |
26928 KB |
Output is correct |
99 |
Correct |
146 ms |
26956 KB |
Output is correct |
100 |
Correct |
141 ms |
26704 KB |
Output is correct |
101 |
Correct |
111 ms |
26956 KB |
Output is correct |
102 |
Correct |
126 ms |
26956 KB |
Output is correct |
103 |
Correct |
92 ms |
25040 KB |
Output is correct |