1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| #include <iostream> #include <cstdio> #include <algorithm> #include <climits> #define inf LLONG_MAX using namespace std; typedef long long LL; const int N = 1e4 + 10, K = 1e5 + 10; struct Node { int l, r; LL mn; }tr[K << 2]; struct Cow { int l, r; LL s; bool operator < (const Cow& x) const { return l < x.l; } }cow[N]; int n, L, R; void pushup (int x) { tr[x].mn = min(tr[x << 1].mn, tr[x << 1 | 1].mn); } void build (int x, int l, int r) { tr[x].l = l; tr[x].r = r; if (l == r) { if (l == L - 1) tr[x].mn = 0; else tr[x].mn = inf; return; } int mid = l + r >> 1; build(x << 1, l, mid); build(x << 1 | 1, mid + 1, r); pushup(x); } void modify (int x, int t, LL k) { if (tr[x].l == tr[x].r && tr[x].l == t) { tr[x].mn = k; return; } int mid = tr[x].l + tr[x].r >> 1; if (t <= mid) modify(x << 1, t, k); else modify(x << 1 | 1, t, k); pushup(x); } LL query(int x, int l, int r) { if (tr[x].l >= l && tr[x].r <= r) return tr[x].mn; LL res = inf; int mid = tr[x].l + tr[x].r >> 1; if (l <= mid) res = min(res, query(x << 1, l, r)); if (r > mid) res = min(res, query(x << 1 | 1, l, r)); return res; } int main () { cin >> n >> L >> R; for (int i = 1; i <= n; i++) { cin >> cow[i].l >> cow[i].r >> cow[i].s; cow[i].l = max(L, cow[i].l); cow[i].r = min(R, cow[i].r); } build(1, L - 1, R); sort(cow + 1, cow + n + 1); for (int i = 1; i <= n; i++) { LL t = query(1, cow[i].l - 1, cow[i].r - 1); if (t == inf) continue; modify(1, cow[i].r, t + cow[i].s); } int t = query(1, R, R); cout << (t == inf ? -1 : t); return 0; }
|