N intervals cover [T1, T2] and the corresponding cost S. What is the minimum price for full coverage from m to E. (1 <= n <= 10,000), (0 <= m <= e <= 86,399 ).
The idea is DP. First, each interval is sorted in ascending order of T2. If dp (k) is set to the minimum cost for covering from m to K, then
DP (t2 [I]) = min (dp (t2 [I]), {dp (j) + Si, t1 [I]-1 <= j <= t2 [I]}), for {dp (j) + Si, t1 [I]-1 <= j <= t2 [I]} we can use the line segment tree for optimization, so the final complexity is O (n * loge ).
#include <stdio.h>#include <vector>#include <math.h>#include <string.h>#include <string>#include <iostream>#include <queue>#include <list>#include <algorithm>#include <stack>#include <map>#include <time.h>using namespace std;struct T{int t1;int t2;int S;};#define MAXV 5000000001long long BinTree[270000];T cows[10001];long long DP[86401];template<class TYPE>void UpdateValue(TYPE st[],int i, TYPE value, int N, bool bMin){i += N - 1;st[i] = value;while (i > 0){i = (i - 1) / 2;if (bMin){st[i] = min(st[i * 2 + 1], st[i * 2 + 2]);}elsest[i] = max(st[i * 2 + 1], st[i * 2 + 2]);}}template<class TYPE>TYPE QueryST(TYPE st[], int a, int b, int l, int r, int k, bool bMin){if (l > b || a > r){return bMin ? MAXV : 0;}if (l >= a && b >= r){return st[k];}else{TYPE value1 = QueryST(st, a, b, l, (r + l) / 2, k * 2 + 1, bMin);TYPE value2 = QueryST(st, a, b, (r + l) / 2 + 1, r, k * 2 + 2, bMin);if (bMin){return min(value1, value2);}else{return max(value1, value2);}}}int compT(const void* a1, const void* a2){if (((T*)a1)->t2 - ((T*)a2)->t2 == 0){return ((T*)a1)->t1 - ((T*)a2)->t1;}elsereturn ((T*)a1)->t2 - ((T*)a2)->t2;}int main(){#ifdef _DEBUGfreopen("e:\\in.txt", "r", stdin);#endifint N, M, E;scanf("%d %d %d", &N, &M, &E);M++;E++;for (int i = 0; i < N; i++){scanf("%d %d %d", &cows[i].t1, &cows[i].t2, &cows[i].S);cows[i].t1++;cows[i].t2++;}int maxe = 1;while (maxe < E){maxe *= 2;}for (int i = 0; i < maxe * 2;i++){BinTree[i] = MAXV;}for (int i = 0; i <= E;i++){DP[i] = MAXV;}DP[M - 1] = 0;UpdateValue<long long>(BinTree, M - 1, 0, maxe, true);qsort(cows, N, sizeof(T), compT);for (int i = 0; i < N;i++){DP[cows[i].t2] = min(DP[cows[i].t2], QueryST<long long>(BinTree, cows[i].t1 - 1, cows[i].t2, 0, maxe - 1, 0, true) + cows[i].S);UpdateValue<long long>(BinTree, cows[i].t2, DP[cows[i].t2], maxe, true);}if (E <= cows[N - 1].t2){DP[E] = QueryST<long long>(BinTree, E, cows[N - 1].t2, 0, maxe - 1, 0, true);}if (DP[E] >= MAXV){printf("-1\n");}elseprintf("%I64d\n", DP[E]);return 0;}