#include <bits/stdc++.h>
using namespace std;
const int64_t inf = 1e15;
class segment_tree {
private:
int n;
vector<int64_t> seg;
public:
segment_tree(int n) : n(n), seg(2 * n, inf) {}
void set(int i, int64_t x) {
for (seg[i += n] = x, i >>= 1; i > 0; i >>= 1) {
seg[i] = min(seg[2 * i], seg[2 * i + 1]);
}
}
int64_t query(int l, int r) {
int64_t ans = inf;
for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1)
ans = min(ans, seg[l++]);
if (r & 1)
ans = min(ans, seg[--r]);
}
return ans;
}
int64_t query(int l) { return query(l, l); }
};
struct interval {
int l, r, c;
bool operator<(const interval &other) const { return r < other.r; }
};
int main() {
int n, m;
cin >> n >> m;
vector<interval> a(m);
vector<int> buf = {0};
for (auto &[l, r, c] : a) {
int x;
cin >> x;
cin >> l >> r >> c;
buf.push_back(l - 1);
buf.push_back(r);
}
sort(buf.begin(), buf.end());
buf.erase(unique(buf.begin(), buf.end()), buf.end());
auto comp = [&](int x) {
return lower_bound(buf.begin(), buf.end(), x) - buf.begin();
};
sort(a.begin(), a.end());
segment_tree dp(buf.size());
dp.set(0, 0);
for (auto &[l, r, c] : a) {
dp.set(comp(r), min(dp.query(comp(r)), dp.query(comp(l - 1), comp(r)) + c));
}
int64_t ans = dp.query(comp(n), comp(n));
if (ans == inf) {
ans = -1;
}
cout << ans << '\n';
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |