#include <bits/stdc++.h>
using namespace std;
namespace
{
} // namespace
void solve()
{
int m, n;
cin >> m >> n;
vector<tuple<long, long, long, long>> v(m);
for (auto &[a, b, c, d] : v)
cin >> a >> b >> c >> d, --a, --b, --c;
long cost = LONG_MAX;
map<long, long> ft, bk;
ft.emplace(0, 0);
bk.emplace(n - 1, 0);
for (auto [a, b, c, d] : v)
{
{
auto it_ft = ft.lower_bound(a), it_bk = bk.upper_bound(b);
if (it_ft != ft.end() && it_bk != bk.begin())
cost = min(cost, it_ft->second + prev(it_bk)->second + d);
}
{
auto it = ft.lower_bound(a);
if (it != ft.end())
{
long x = it->second;
it = ft.lower_bound(c);
if (it == ft.end() || x + d < it->second)
{
it = ft.upper_bound(c);
while (it != ft.begin() && prev(it)->second > x + d)
ft.erase(prev(it));
ft.emplace(c, x + d);
}
}
}
{
auto it = bk.upper_bound(b);
if (it != bk.begin())
{
long x = prev(it)->second;
it = bk.upper_bound(c);
if (it == bk.begin() || x + d < prev(it)->second)
{
it = bk.lower_bound(c);
while (it != bk.end() && it->second > x + d)
it = bk.erase(it);
bk.emplace(c, x + d);
}
}
}
}
cout << (cost != LONG_MAX ? cost : -1) << '\n';
}
int main()
{
ios_base::sync_with_stdio(false), cin.tie(NULL);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
396 KB |
Output is correct |
18 |
Correct |
1 ms |
396 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
396 KB |
Output is correct |
18 |
Correct |
1 ms |
396 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
5 ms |
888 KB |
Output is correct |
26 |
Correct |
11 ms |
2004 KB |
Output is correct |
27 |
Correct |
32 ms |
4936 KB |
Output is correct |
28 |
Correct |
47 ms |
7228 KB |
Output is correct |
29 |
Correct |
27 ms |
3788 KB |
Output is correct |
30 |
Correct |
48 ms |
7276 KB |
Output is correct |
31 |
Correct |
47 ms |
7244 KB |
Output is correct |
32 |
Correct |
45 ms |
7284 KB |
Output is correct |
33 |
Correct |
13 ms |
2380 KB |
Output is correct |
34 |
Correct |
25 ms |
3796 KB |
Output is correct |
35 |
Correct |
78 ms |
12512 KB |
Output is correct |
36 |
Correct |
51 ms |
7244 KB |
Output is correct |
37 |
Correct |
63 ms |
8152 KB |
Output is correct |
38 |
Correct |
67 ms |
8684 KB |
Output is correct |