#include <bits/stdc++.h>
using namespace std;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 2e5+10, MOD = 998244353;
const ll INF = 1e18+10;
template <class T>
using min_pq = priority_queue<T, vector<T>, greater<T>>;
struct FT {
vector<int> a;
vector<vector<pair<int, int>>> bit;
void upd(int i, pair<int, int> x) {
for (; i < sz(bit); i |= i+1) {
bit[i].push_back(x);
}
}
FT() {}
FT(vector<pair<int, int>> v) {
for (auto p : v) a.push_back(p.first);
sort(a.begin(), a.end());
a.resize(unique(a.begin(), a.end()) - a.begin());
bit.resize(sz(a));
for (int i = 0; i < sz(v); i++) {
int idx = lower_bound(a.begin(), a.end(), v[i].first) - a.begin();
upd(idx, {v[i].second, i});
}
for (auto& v : bit) {
sort(v.rbegin(), v.rend());
}
}
vector<int> qry(int x, int y) {
int i = upper_bound(a.begin(), a.end(), x) - a.begin() - 1;
vector<int> ans;
for (++i; i > 0; i &= i-1) {
while (sz(bit[i-1]) && bit[i-1].back().first <= y) {
ans.push_back(bit[i-1].back().second);
bit[i-1].pop_back();
}
}
return ans;
}
};
void solve() {
int n, m; cin >> n >> m;
vector<ar<int, 4>> a(m);
for (auto& [t, l, r, x] : a) cin >> t >> l >> r >> x, --l, --r;
min_pq<pair<ll, int>> q;
vector<ll> dist(m, INF);
for (int i = 0; i < m; i++) {
auto [t, l, r, x] = a[i];
if (l == 0) {
dist[i] = x;
q.push({dist[i], i});
}
}
vector<pair<int, int>> v1(m), v2(m);
for (int i = 0; i < m; i++) {
auto [t, l, r, x] = a[i];
v1[i] = {t, l - t - 1};
v2[i] = {-t, t + l - 1};
}
FT one(v1), two(v2);
while (!q.empty()) {
auto [d_c, c] = q.top(); q.pop();
if (dist[c] != d_c) continue;
auto f = [&](int i) {
auto [t, l, r, x] = a[i];
if (dist[c] + x < dist[i]) {
dist[i] = dist[c] + x;
q.push({dist[i], i});
}
};
for (int x : one.qry(a[c][0], a[c][2] - a[c][0]))
f(x);
for (int x : two.qry(-a[c][0], a[c][0] + a[c][2]))
f(x);
}
ll ans = INF;
for (int i = 0; i < m; i++) {
auto [t, l, r, x] = a[i];
if (r == n-1) {
ans = min(ans, dist[i]);
}
}
if (ans >= INF) {
cout << -1 << '\n';
return;
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int T = 1;
// cin >> T;
while (T--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
11956 KB |
Output is correct |
2 |
Correct |
58 ms |
11836 KB |
Output is correct |
3 |
Correct |
61 ms |
11872 KB |
Output is correct |
4 |
Correct |
61 ms |
11212 KB |
Output is correct |
5 |
Correct |
66 ms |
13528 KB |
Output is correct |
6 |
Correct |
63 ms |
11700 KB |
Output is correct |
7 |
Correct |
55 ms |
11716 KB |
Output is correct |
8 |
Correct |
49 ms |
11476 KB |
Output is correct |
9 |
Correct |
48 ms |
11716 KB |
Output is correct |
10 |
Correct |
46 ms |
11708 KB |
Output is correct |
11 |
Correct |
68 ms |
13872 KB |
Output is correct |
12 |
Correct |
65 ms |
13876 KB |
Output is correct |
13 |
Correct |
64 ms |
13756 KB |
Output is correct |
14 |
Correct |
64 ms |
13732 KB |
Output is correct |
15 |
Correct |
59 ms |
11708 KB |
Output is correct |
16 |
Correct |
62 ms |
11776 KB |
Output is correct |
17 |
Correct |
59 ms |
10964 KB |
Output is correct |
18 |
Correct |
62 ms |
13108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
316 KB |
Output is correct |
20 |
Correct |
8 ms |
1416 KB |
Output is correct |
21 |
Correct |
8 ms |
1412 KB |
Output is correct |
22 |
Correct |
7 ms |
1396 KB |
Output is correct |
23 |
Correct |
6 ms |
1364 KB |
Output is correct |
24 |
Correct |
11 ms |
1920 KB |
Output is correct |
25 |
Correct |
9 ms |
1748 KB |
Output is correct |
26 |
Correct |
8 ms |
1752 KB |
Output is correct |
27 |
Correct |
8 ms |
1876 KB |
Output is correct |
28 |
Correct |
9 ms |
1744 KB |
Output is correct |
29 |
Correct |
8 ms |
1760 KB |
Output is correct |
30 |
Correct |
6 ms |
1748 KB |
Output is correct |
31 |
Correct |
6 ms |
1876 KB |
Output is correct |
32 |
Correct |
9 ms |
1864 KB |
Output is correct |
33 |
Correct |
9 ms |
1992 KB |
Output is correct |
34 |
Correct |
9 ms |
1608 KB |
Output is correct |
35 |
Correct |
10 ms |
1876 KB |
Output is correct |
36 |
Correct |
9 ms |
1952 KB |
Output is correct |
37 |
Correct |
9 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
11956 KB |
Output is correct |
2 |
Correct |
58 ms |
11836 KB |
Output is correct |
3 |
Correct |
61 ms |
11872 KB |
Output is correct |
4 |
Correct |
61 ms |
11212 KB |
Output is correct |
5 |
Correct |
66 ms |
13528 KB |
Output is correct |
6 |
Correct |
63 ms |
11700 KB |
Output is correct |
7 |
Correct |
55 ms |
11716 KB |
Output is correct |
8 |
Correct |
49 ms |
11476 KB |
Output is correct |
9 |
Correct |
48 ms |
11716 KB |
Output is correct |
10 |
Correct |
46 ms |
11708 KB |
Output is correct |
11 |
Correct |
68 ms |
13872 KB |
Output is correct |
12 |
Correct |
65 ms |
13876 KB |
Output is correct |
13 |
Correct |
64 ms |
13756 KB |
Output is correct |
14 |
Correct |
64 ms |
13732 KB |
Output is correct |
15 |
Correct |
59 ms |
11708 KB |
Output is correct |
16 |
Correct |
62 ms |
11776 KB |
Output is correct |
17 |
Correct |
59 ms |
10964 KB |
Output is correct |
18 |
Correct |
62 ms |
13108 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
316 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
316 KB |
Output is correct |
38 |
Correct |
8 ms |
1416 KB |
Output is correct |
39 |
Correct |
8 ms |
1412 KB |
Output is correct |
40 |
Correct |
7 ms |
1396 KB |
Output is correct |
41 |
Correct |
6 ms |
1364 KB |
Output is correct |
42 |
Correct |
11 ms |
1920 KB |
Output is correct |
43 |
Correct |
9 ms |
1748 KB |
Output is correct |
44 |
Correct |
8 ms |
1752 KB |
Output is correct |
45 |
Correct |
8 ms |
1876 KB |
Output is correct |
46 |
Correct |
9 ms |
1744 KB |
Output is correct |
47 |
Correct |
8 ms |
1760 KB |
Output is correct |
48 |
Correct |
6 ms |
1748 KB |
Output is correct |
49 |
Correct |
6 ms |
1876 KB |
Output is correct |
50 |
Correct |
9 ms |
1864 KB |
Output is correct |
51 |
Correct |
9 ms |
1992 KB |
Output is correct |
52 |
Correct |
9 ms |
1608 KB |
Output is correct |
53 |
Correct |
10 ms |
1876 KB |
Output is correct |
54 |
Correct |
9 ms |
1952 KB |
Output is correct |
55 |
Correct |
9 ms |
1492 KB |
Output is correct |
56 |
Correct |
220 ms |
26276 KB |
Output is correct |
57 |
Correct |
219 ms |
26716 KB |
Output is correct |
58 |
Correct |
201 ms |
25096 KB |
Output is correct |
59 |
Correct |
205 ms |
25488 KB |
Output is correct |
60 |
Correct |
171 ms |
26428 KB |
Output is correct |
61 |
Correct |
198 ms |
25016 KB |
Output is correct |
62 |
Correct |
229 ms |
26292 KB |
Output is correct |
63 |
Correct |
209 ms |
31688 KB |
Output is correct |
64 |
Correct |
203 ms |
31696 KB |
Output is correct |
65 |
Correct |
69 ms |
12540 KB |
Output is correct |
66 |
Correct |
171 ms |
26040 KB |
Output is correct |
67 |
Correct |
247 ms |
34060 KB |
Output is correct |
68 |
Correct |
224 ms |
35884 KB |
Output is correct |
69 |
Correct |
202 ms |
36184 KB |
Output is correct |
70 |
Correct |
282 ms |
33120 KB |
Output is correct |
71 |
Correct |
224 ms |
33544 KB |
Output is correct |
72 |
Correct |
229 ms |
35248 KB |
Output is correct |
73 |
Correct |
260 ms |
33416 KB |
Output is correct |
74 |
Correct |
130 ms |
33780 KB |
Output is correct |
75 |
Correct |
127 ms |
34804 KB |
Output is correct |
76 |
Correct |
239 ms |
32708 KB |
Output is correct |
77 |
Correct |
266 ms |
35520 KB |
Output is correct |
78 |
Correct |
279 ms |
32424 KB |
Output is correct |
79 |
Correct |
292 ms |
32980 KB |
Output is correct |
80 |
Correct |
275 ms |
29888 KB |
Output is correct |
81 |
Correct |
174 ms |
33476 KB |
Output is correct |
82 |
Correct |
273 ms |
29048 KB |
Output is correct |
83 |
Correct |
305 ms |
35604 KB |
Output is correct |
84 |
Correct |
289 ms |
32948 KB |
Output is correct |
85 |
Correct |
231 ms |
33632 KB |
Output is correct |
86 |
Correct |
260 ms |
33860 KB |
Output is correct |
87 |
Correct |
240 ms |
33724 KB |
Output is correct |
88 |
Correct |
268 ms |
36400 KB |
Output is correct |
89 |
Correct |
238 ms |
34228 KB |
Output is correct |
90 |
Correct |
301 ms |
36412 KB |
Output is correct |
91 |
Correct |
256 ms |
34056 KB |
Output is correct |
92 |
Correct |
241 ms |
33596 KB |
Output is correct |
93 |
Correct |
301 ms |
33076 KB |
Output is correct |
94 |
Correct |
290 ms |
35532 KB |
Output is correct |
95 |
Correct |
285 ms |
33384 KB |
Output is correct |
96 |
Correct |
307 ms |
35212 KB |
Output is correct |
97 |
Correct |
272 ms |
35172 KB |
Output is correct |
98 |
Correct |
301 ms |
35512 KB |
Output is correct |
99 |
Correct |
268 ms |
35524 KB |
Output is correct |
100 |
Correct |
307 ms |
35628 KB |
Output is correct |
101 |
Correct |
269 ms |
34872 KB |
Output is correct |
102 |
Correct |
261 ms |
35256 KB |
Output is correct |
103 |
Correct |
232 ms |
33644 KB |
Output is correct |