// https://oj.uz/problem/view/JOI20_treatment
#include <bits/stdc++.h>
using namespace std;
namespace std {
template <int D, typename T>
struct Vec : public vector<Vec<D - 1, T>> {
static_assert(D >= 1, "Dimension must be positive");
template <typename... Args>
Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {}
};
template <typename T>
struct Vec<1, T> : public vector<T> {
Vec(int n = 0, T val = T()) : std::vector<T>(n, val) {}
};
/* Example
Vec<4, int64_t> f(n, k, 2, 2); // = f[n][k][2][2];
Vec<2, int> adj(n); // graph
*/
template <class Fun>
class y_combinator_result {
Fun fun_;
public:
template <class T>
explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)) {}
template <class... Args>
decltype(auto) operator()(Args &&...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template <class Fun>
decltype(auto) y_combinator(Fun &&fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
/* Example
auto fun = y_combinator([&](auto self, int x) -> void {
self(x + 1);
});
*/
} // namespace std
vector<int> c;
vector<int64_t> d;
priority_queue<pair<int64_t, int>> pq;
struct segment_tree {
int n;
vector<vector<pair<int, int>>> val;
vector<int> done;
segment_tree(int n, const vector<pair<int, int>> &a) : n(n), val(n * 4), done(n, 0) {
build(1, 0, n - 1, a);
rev(1, 0, n - 1);
}
void build(int v, int l, int r, const vector<pair<int, int>> &a) {
if (l == r) {
val[v] = {a[l]};
return;
}
int mid = (l + r) / 2;
build(v * 2, l, mid, a);
build(v * 2 + 1, mid + 1, r, a);
merge(val[v * 2].begin(), val[v * 2].end(), val[v * 2 + 1].begin(), val[v * 2 + 1].end(),
back_inserter(val[v]));
}
void rev(int v, int l, int r) {
reverse(val[v].begin(), val[v].end());
if (l == r) return;
int mid = (l + r) / 2;
rev(v * 2, l, mid);
rev(v * 2 + 1, mid + 1, r);
}
void update(int l, int r, int x, int64_t du) {
update(1, 0, n - 1, l, r, x, du);
}
void update(int v, int l, int r, int s, int t, int x, int64_t du) {
if (l > t || r < s) return;
if (s <= l && r <= t) {
while (val[v].size() && val[v].back().first <= x) {
auto [value, index] = val[v].back();
val[v].pop_back();
if (done[index]) continue;
done[index] = 1;
if (d[index] > du + c[index]) {
d[index] = du + c[index];
pq.emplace(-d[index], index);
}
}
return;
}
int mid = (l + r) / 2;
update(v * 2, l, mid, s, t, x, du);
update(v * 2 + 1, mid + 1, r, s, t, x, du);
}
};
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> l(m), r(m), t(m);
c.resize(m);
for (int i = 0; i < m; i++) cin >> t[i] >> l[i] >> r[i] >> c[i];
for (int i = 0; i < m; i++) l[i]--;
d.assign(m, 1e18);
for (int i = 0; i < m; i++) {
if (l[i] == 0) {
d[i] = c[i];
pq.emplace(-d[i], i);
}
}
vector<pair<int, int>> tmp(m);
for (int i = 0; i < m; i++) tmp[i] = make_pair(l[i] + t[i], i);
sort(tmp.begin(), tmp.end(), [&](auto a, auto b) { return t[a.second] < t[b.second]; });
segment_tree seg_less(m, tmp);
for (int i = 0; i < m; i++) tmp[i] = make_pair(l[i] - t[i], i);
sort(tmp.begin(), tmp.end(), [&](auto a, auto b) { return t[a.second] < t[b.second]; });
segment_tree seg_greater(m, tmp);
vector<int> ord(m);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&](auto a, auto b) { return t[a] < t[b]; });
vector<int> p(m);
for (int i = 0; i < m; i++) p[ord[i]] = i;
// if t[j] >= t[i] : l[j] <= r[i] - (t[j] - t[i])
// l[j] + t[j] <= r[i] + t[i]
// if t[j] <= t[i] : l[j] <= r[i] - (t[i] - t[j])
// l[j] - t[j] <= r[i] - t[i]
while (pq.size()) {
auto [du, j] = pq.top();
pq.pop();
du = -du;
if (du != d[j]) continue;
if (r[j] == n) {
cout << du;
return 0;
}
seg_less.update(p[j], m - 1, r[j] + t[j], du);
seg_greater.update(0, p[j], r[j] - t[j], du);
}
cout << -1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
260 ms |
68288 KB |
Output is correct |
2 |
Correct |
177 ms |
68436 KB |
Output is correct |
3 |
Correct |
213 ms |
68556 KB |
Output is correct |
4 |
Correct |
226 ms |
68520 KB |
Output is correct |
5 |
Correct |
120 ms |
70852 KB |
Output is correct |
6 |
Correct |
190 ms |
68440 KB |
Output is correct |
7 |
Correct |
175 ms |
68432 KB |
Output is correct |
8 |
Correct |
107 ms |
68296 KB |
Output is correct |
9 |
Correct |
113 ms |
68368 KB |
Output is correct |
10 |
Correct |
107 ms |
68296 KB |
Output is correct |
11 |
Correct |
124 ms |
70864 KB |
Output is correct |
12 |
Correct |
129 ms |
70868 KB |
Output is correct |
13 |
Correct |
168 ms |
70964 KB |
Output is correct |
14 |
Correct |
177 ms |
71240 KB |
Output is correct |
15 |
Correct |
274 ms |
68444 KB |
Output is correct |
16 |
Correct |
259 ms |
68436 KB |
Output is correct |
17 |
Correct |
264 ms |
67456 KB |
Output is correct |
18 |
Correct |
120 ms |
70104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 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 |
460 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
460 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 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 |
460 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
460 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
9 ms |
3932 KB |
Output is correct |
21 |
Correct |
9 ms |
3588 KB |
Output is correct |
22 |
Correct |
8 ms |
3672 KB |
Output is correct |
23 |
Correct |
7 ms |
3676 KB |
Output is correct |
24 |
Correct |
7 ms |
3676 KB |
Output is correct |
25 |
Correct |
9 ms |
3676 KB |
Output is correct |
26 |
Correct |
10 ms |
3676 KB |
Output is correct |
27 |
Correct |
8 ms |
3676 KB |
Output is correct |
28 |
Correct |
7 ms |
3716 KB |
Output is correct |
29 |
Correct |
8 ms |
3792 KB |
Output is correct |
30 |
Correct |
6 ms |
3676 KB |
Output is correct |
31 |
Correct |
6 ms |
3676 KB |
Output is correct |
32 |
Correct |
7 ms |
3672 KB |
Output is correct |
33 |
Correct |
7 ms |
3676 KB |
Output is correct |
34 |
Correct |
9 ms |
3772 KB |
Output is correct |
35 |
Correct |
6 ms |
3720 KB |
Output is correct |
36 |
Correct |
6 ms |
3712 KB |
Output is correct |
37 |
Correct |
8 ms |
3672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
260 ms |
68288 KB |
Output is correct |
2 |
Correct |
177 ms |
68436 KB |
Output is correct |
3 |
Correct |
213 ms |
68556 KB |
Output is correct |
4 |
Correct |
226 ms |
68520 KB |
Output is correct |
5 |
Correct |
120 ms |
70852 KB |
Output is correct |
6 |
Correct |
190 ms |
68440 KB |
Output is correct |
7 |
Correct |
175 ms |
68432 KB |
Output is correct |
8 |
Correct |
107 ms |
68296 KB |
Output is correct |
9 |
Correct |
113 ms |
68368 KB |
Output is correct |
10 |
Correct |
107 ms |
68296 KB |
Output is correct |
11 |
Correct |
124 ms |
70864 KB |
Output is correct |
12 |
Correct |
129 ms |
70868 KB |
Output is correct |
13 |
Correct |
168 ms |
70964 KB |
Output is correct |
14 |
Correct |
177 ms |
71240 KB |
Output is correct |
15 |
Correct |
274 ms |
68444 KB |
Output is correct |
16 |
Correct |
259 ms |
68436 KB |
Output is correct |
17 |
Correct |
264 ms |
67456 KB |
Output is correct |
18 |
Correct |
120 ms |
70104 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
456 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 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 |
460 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
460 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
9 ms |
3932 KB |
Output is correct |
39 |
Correct |
9 ms |
3588 KB |
Output is correct |
40 |
Correct |
8 ms |
3672 KB |
Output is correct |
41 |
Correct |
7 ms |
3676 KB |
Output is correct |
42 |
Correct |
7 ms |
3676 KB |
Output is correct |
43 |
Correct |
9 ms |
3676 KB |
Output is correct |
44 |
Correct |
10 ms |
3676 KB |
Output is correct |
45 |
Correct |
8 ms |
3676 KB |
Output is correct |
46 |
Correct |
7 ms |
3716 KB |
Output is correct |
47 |
Correct |
8 ms |
3792 KB |
Output is correct |
48 |
Correct |
6 ms |
3676 KB |
Output is correct |
49 |
Correct |
6 ms |
3676 KB |
Output is correct |
50 |
Correct |
7 ms |
3672 KB |
Output is correct |
51 |
Correct |
7 ms |
3676 KB |
Output is correct |
52 |
Correct |
9 ms |
3772 KB |
Output is correct |
53 |
Correct |
6 ms |
3720 KB |
Output is correct |
54 |
Correct |
6 ms |
3712 KB |
Output is correct |
55 |
Correct |
8 ms |
3672 KB |
Output is correct |
56 |
Correct |
195 ms |
68812 KB |
Output is correct |
57 |
Correct |
210 ms |
69068 KB |
Output is correct |
58 |
Correct |
216 ms |
67780 KB |
Output is correct |
59 |
Correct |
206 ms |
67916 KB |
Output is correct |
60 |
Correct |
235 ms |
68600 KB |
Output is correct |
61 |
Correct |
208 ms |
67536 KB |
Output is correct |
62 |
Correct |
197 ms |
68768 KB |
Output is correct |
63 |
Correct |
176 ms |
68556 KB |
Output is correct |
64 |
Correct |
178 ms |
68552 KB |
Output is correct |
65 |
Correct |
306 ms |
68664 KB |
Output is correct |
66 |
Correct |
215 ms |
68752 KB |
Output is correct |
67 |
Correct |
253 ms |
69316 KB |
Output is correct |
68 |
Correct |
242 ms |
69204 KB |
Output is correct |
69 |
Correct |
223 ms |
69060 KB |
Output is correct |
70 |
Correct |
262 ms |
69060 KB |
Output is correct |
71 |
Correct |
252 ms |
69064 KB |
Output is correct |
72 |
Correct |
240 ms |
69212 KB |
Output is correct |
73 |
Correct |
269 ms |
69064 KB |
Output is correct |
74 |
Correct |
137 ms |
69312 KB |
Output is correct |
75 |
Correct |
159 ms |
69208 KB |
Output is correct |
76 |
Correct |
159 ms |
71448 KB |
Output is correct |
77 |
Correct |
165 ms |
70564 KB |
Output is correct |
78 |
Correct |
231 ms |
71364 KB |
Output is correct |
79 |
Correct |
300 ms |
69316 KB |
Output is correct |
80 |
Correct |
313 ms |
68812 KB |
Output is correct |
81 |
Correct |
156 ms |
69068 KB |
Output is correct |
82 |
Correct |
292 ms |
67952 KB |
Output is correct |
83 |
Correct |
307 ms |
68292 KB |
Output is correct |
84 |
Correct |
316 ms |
68292 KB |
Output is correct |
85 |
Correct |
227 ms |
69080 KB |
Output is correct |
86 |
Correct |
250 ms |
69220 KB |
Output is correct |
87 |
Correct |
254 ms |
69468 KB |
Output is correct |
88 |
Correct |
264 ms |
69328 KB |
Output is correct |
89 |
Correct |
241 ms |
69064 KB |
Output is correct |
90 |
Correct |
175 ms |
71624 KB |
Output is correct |
91 |
Correct |
150 ms |
69068 KB |
Output is correct |
92 |
Correct |
256 ms |
69060 KB |
Output is correct |
93 |
Correct |
292 ms |
69208 KB |
Output is correct |
94 |
Correct |
279 ms |
69060 KB |
Output is correct |
95 |
Correct |
268 ms |
69184 KB |
Output is correct |
96 |
Correct |
155 ms |
70344 KB |
Output is correct |
97 |
Correct |
159 ms |
71556 KB |
Output is correct |
98 |
Correct |
161 ms |
71756 KB |
Output is correct |
99 |
Correct |
157 ms |
71724 KB |
Output is correct |
100 |
Correct |
153 ms |
71752 KB |
Output is correct |
101 |
Correct |
169 ms |
71736 KB |
Output is correct |
102 |
Correct |
162 ms |
71612 KB |
Output is correct |
103 |
Correct |
202 ms |
69060 KB |
Output is correct |