#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Segment {
int t, l, r, w;
};
constexpr ll inf = 3e18;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<Segment> s(m);
vector<int> tt;
for (auto &[t, l, r, w] : s) {
cin >> t >> l >> r >> w;
tt.push_back(t);
}
sort(tt.begin(), tt.end());
tt.resize(unique(tt.begin(), tt.end()) - tt.begin());
const int siz = tt.size();
vector<int> pnt1(siz + 1), pnt2(siz + 1), pos(m);
vector<vector<pair<ll, int>>> fn1(siz + 1), fn2(siz + 1);
vector<pair<ll, int>> val1(m), val2(m);
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;
vector<ll> dp(m, inf);
vector<bool> used(m);
for (int i = 0; i < m; ++i) {
pos[i] = lower_bound(tt.begin(), tt.end(), s[i].t) - tt.begin();
val1[i] = {-s[i].l + s[i].t + 1, i};
val2[i] = {-s[i].l - s[i].t + 1, i};
}
sort(val1.rbegin(), val1.rend());
sort(val2.rbegin(), val2.rend());
for (auto [v, i] : val1) {
for (int x = pos[i] + 1; x <= siz; x += x & -x) {
fn1[x].emplace_back(v, i);
}
}
for (auto [v, i] : val2) {
for (int x = pos[i] + 1; x > 0; x -= x & -x) {
fn2[x].emplace_back(v, i);
}
}
auto take = [&](int i, ll val) {
if (used[i]) {
return;
}
dp[i] = val + s[i].w;
pq.emplace(dp[i], i);
used[i] = true;
};
for (int i = 0; i < m; ++i) {
if (s[i].l == 1) {
take(i, 0);
}
}
while (!pq.empty()) {
auto [d, i] = pq.top();
pq.pop();
ll v1 = s[i].t - s[i].r, v2 = -s[i].r - s[i].t;
for (int x = pos[i] + 1; x > 0; x -= x & -x) {
while (pnt1[x] < size(fn1[x]) && fn1[x][pnt1[x]].first >= v1) {
take(fn1[x][pnt1[x]++].second, d);
}
}
for (int x = pos[i] + 1; x <= siz; x += x & -x) {
while (pnt2[x] < size(fn2[x]) && fn2[x][pnt2[x]].first >= v2) {
take(fn2[x][pnt2[x]++].second, d);
}
}
}
ll ans = inf;
for (int i = 0; i < m; ++i) {
if (s[i].r == n) {
ans = min(ans, dp[i]);
}
}
if (ans == inf) {
cout << "-1\n";
} else {
cout << ans << '\n';
}
return 0;
}
Compilation message
treatment.cpp: In function 'int main()':
treatment.cpp:81:28: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | while (pnt1[x] < size(fn1[x]) && fn1[x][pnt1[x]].first >= v1) {
treatment.cpp:86:28: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | while (pnt2[x] < size(fn2[x]) && fn2[x][pnt2[x]].first >= v2) {
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
14508 KB |
Output is correct |
2 |
Correct |
65 ms |
14404 KB |
Output is correct |
3 |
Correct |
75 ms |
13584 KB |
Output is correct |
4 |
Correct |
73 ms |
13412 KB |
Output is correct |
5 |
Correct |
79 ms |
15832 KB |
Output is correct |
6 |
Correct |
75 ms |
14332 KB |
Output is correct |
7 |
Correct |
69 ms |
14280 KB |
Output is correct |
8 |
Correct |
67 ms |
14288 KB |
Output is correct |
9 |
Correct |
55 ms |
14292 KB |
Output is correct |
10 |
Correct |
54 ms |
14340 KB |
Output is correct |
11 |
Correct |
84 ms |
15888 KB |
Output is correct |
12 |
Correct |
94 ms |
15996 KB |
Output is correct |
13 |
Correct |
92 ms |
15816 KB |
Output is correct |
14 |
Correct |
80 ms |
15840 KB |
Output is correct |
15 |
Correct |
73 ms |
14296 KB |
Output is correct |
16 |
Correct |
69 ms |
14280 KB |
Output is correct |
17 |
Correct |
68 ms |
13528 KB |
Output is correct |
18 |
Correct |
83 ms |
15156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
320 KB |
Output is correct |
12 |
Correct |
0 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
320 KB |
Output is correct |
12 |
Correct |
0 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
6 ms |
1876 KB |
Output is correct |
21 |
Correct |
6 ms |
2004 KB |
Output is correct |
22 |
Correct |
5 ms |
1876 KB |
Output is correct |
23 |
Correct |
6 ms |
1916 KB |
Output is correct |
24 |
Correct |
7 ms |
2260 KB |
Output is correct |
25 |
Correct |
8 ms |
2260 KB |
Output is correct |
26 |
Correct |
8 ms |
2372 KB |
Output is correct |
27 |
Correct |
7 ms |
2504 KB |
Output is correct |
28 |
Correct |
8 ms |
2384 KB |
Output is correct |
29 |
Correct |
8 ms |
2260 KB |
Output is correct |
30 |
Correct |
6 ms |
2388 KB |
Output is correct |
31 |
Correct |
6 ms |
2564 KB |
Output is correct |
32 |
Correct |
7 ms |
2384 KB |
Output is correct |
33 |
Correct |
8 ms |
2628 KB |
Output is correct |
34 |
Correct |
8 ms |
2132 KB |
Output is correct |
35 |
Correct |
8 ms |
2372 KB |
Output is correct |
36 |
Correct |
7 ms |
2496 KB |
Output is correct |
37 |
Correct |
7 ms |
2188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
14508 KB |
Output is correct |
2 |
Correct |
65 ms |
14404 KB |
Output is correct |
3 |
Correct |
75 ms |
13584 KB |
Output is correct |
4 |
Correct |
73 ms |
13412 KB |
Output is correct |
5 |
Correct |
79 ms |
15832 KB |
Output is correct |
6 |
Correct |
75 ms |
14332 KB |
Output is correct |
7 |
Correct |
69 ms |
14280 KB |
Output is correct |
8 |
Correct |
67 ms |
14288 KB |
Output is correct |
9 |
Correct |
55 ms |
14292 KB |
Output is correct |
10 |
Correct |
54 ms |
14340 KB |
Output is correct |
11 |
Correct |
84 ms |
15888 KB |
Output is correct |
12 |
Correct |
94 ms |
15996 KB |
Output is correct |
13 |
Correct |
92 ms |
15816 KB |
Output is correct |
14 |
Correct |
80 ms |
15840 KB |
Output is correct |
15 |
Correct |
73 ms |
14296 KB |
Output is correct |
16 |
Correct |
69 ms |
14280 KB |
Output is correct |
17 |
Correct |
68 ms |
13528 KB |
Output is correct |
18 |
Correct |
83 ms |
15156 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
1 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 |
320 KB |
Output is correct |
30 |
Correct |
0 ms |
320 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
320 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
320 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
320 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
6 ms |
1876 KB |
Output is correct |
39 |
Correct |
6 ms |
2004 KB |
Output is correct |
40 |
Correct |
5 ms |
1876 KB |
Output is correct |
41 |
Correct |
6 ms |
1916 KB |
Output is correct |
42 |
Correct |
7 ms |
2260 KB |
Output is correct |
43 |
Correct |
8 ms |
2260 KB |
Output is correct |
44 |
Correct |
8 ms |
2372 KB |
Output is correct |
45 |
Correct |
7 ms |
2504 KB |
Output is correct |
46 |
Correct |
8 ms |
2384 KB |
Output is correct |
47 |
Correct |
8 ms |
2260 KB |
Output is correct |
48 |
Correct |
6 ms |
2388 KB |
Output is correct |
49 |
Correct |
6 ms |
2564 KB |
Output is correct |
50 |
Correct |
7 ms |
2384 KB |
Output is correct |
51 |
Correct |
8 ms |
2628 KB |
Output is correct |
52 |
Correct |
8 ms |
2132 KB |
Output is correct |
53 |
Correct |
8 ms |
2372 KB |
Output is correct |
54 |
Correct |
7 ms |
2496 KB |
Output is correct |
55 |
Correct |
7 ms |
2188 KB |
Output is correct |
56 |
Correct |
156 ms |
40736 KB |
Output is correct |
57 |
Correct |
158 ms |
41280 KB |
Output is correct |
58 |
Correct |
134 ms |
39272 KB |
Output is correct |
59 |
Correct |
141 ms |
39580 KB |
Output is correct |
60 |
Correct |
129 ms |
39868 KB |
Output is correct |
61 |
Correct |
139 ms |
39200 KB |
Output is correct |
62 |
Correct |
168 ms |
40824 KB |
Output is correct |
63 |
Correct |
137 ms |
45272 KB |
Output is correct |
64 |
Correct |
134 ms |
45176 KB |
Output is correct |
65 |
Correct |
71 ms |
15188 KB |
Output is correct |
66 |
Correct |
121 ms |
39964 KB |
Output is correct |
67 |
Correct |
223 ms |
47992 KB |
Output is correct |
68 |
Correct |
231 ms |
51136 KB |
Output is correct |
69 |
Correct |
181 ms |
53772 KB |
Output is correct |
70 |
Correct |
216 ms |
46636 KB |
Output is correct |
71 |
Correct |
216 ms |
47956 KB |
Output is correct |
72 |
Correct |
177 ms |
50776 KB |
Output is correct |
73 |
Correct |
249 ms |
46612 KB |
Output is correct |
74 |
Correct |
131 ms |
48084 KB |
Output is correct |
75 |
Correct |
128 ms |
50448 KB |
Output is correct |
76 |
Correct |
227 ms |
47784 KB |
Output is correct |
77 |
Correct |
242 ms |
49984 KB |
Output is correct |
78 |
Correct |
228 ms |
47728 KB |
Output is correct |
79 |
Correct |
233 ms |
46876 KB |
Output is correct |
80 |
Correct |
240 ms |
45516 KB |
Output is correct |
81 |
Correct |
175 ms |
47600 KB |
Output is correct |
82 |
Correct |
220 ms |
45068 KB |
Output is correct |
83 |
Correct |
249 ms |
52128 KB |
Output is correct |
84 |
Correct |
232 ms |
46116 KB |
Output is correct |
85 |
Correct |
209 ms |
46700 KB |
Output is correct |
86 |
Correct |
187 ms |
47556 KB |
Output is correct |
87 |
Correct |
190 ms |
47540 KB |
Output is correct |
88 |
Correct |
203 ms |
52204 KB |
Output is correct |
89 |
Correct |
193 ms |
47984 KB |
Output is correct |
90 |
Correct |
247 ms |
51524 KB |
Output is correct |
91 |
Correct |
226 ms |
48008 KB |
Output is correct |
92 |
Correct |
205 ms |
47064 KB |
Output is correct |
93 |
Correct |
252 ms |
46512 KB |
Output is correct |
94 |
Correct |
229 ms |
50992 KB |
Output is correct |
95 |
Correct |
230 ms |
46416 KB |
Output is correct |
96 |
Correct |
249 ms |
50000 KB |
Output is correct |
97 |
Correct |
251 ms |
49604 KB |
Output is correct |
98 |
Correct |
230 ms |
49780 KB |
Output is correct |
99 |
Correct |
223 ms |
50368 KB |
Output is correct |
100 |
Correct |
219 ms |
50564 KB |
Output is correct |
101 |
Correct |
251 ms |
49740 KB |
Output is correct |
102 |
Correct |
217 ms |
49420 KB |
Output is correct |
103 |
Correct |
244 ms |
47244 KB |
Output is correct |