标签:zh-cn mes describe space ssi mem include int push
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4245 | Accepted: 1429 |
Description
Input
Output
Sample Input
3 0 4 0 2 3 3 4 2 0 0 1
Sample Output
5
Hint
Source
题意:给n个区间及其代价值,问要覆盖[M,E]区间至少要花费多少代价;
解法:这是一个dp问题,先列出方程。
F[i]表示取[0,i]这个区间的代价,初始化F[M-1]=0,答案就是F[E].
则方程为F[a[i].T2]=min(F[a[j].T2])+a[i].s (T1-1<=a[j].T2<T2),找min的过程用线段树实现。
将a[i]按T2从小到大排列,逐步更新最小值。
代码:
1 #include"bits/stdc++.h" 2 3 #define ll long long 4 #define vl vector<ll> 5 #define ci(x) scanf("%d",&x) 6 #define pi(x) printf("%d\n",x) 7 #define pl(x) printf("%lld\n",x) 8 #define rep(i, n) for(int i=0;i<n;i++) 9 using namespace std; 10 const int NN = 1e6 + 5; 11 int n,s,t; 12 struct P{int x,y,s;}; 13 P a[NN]; 14 bool cmp(P a,P b){ 15 return a.y<b.y; 16 } 17 const ll INF = 0x3fffffffffffffff; 18 struct SegMin { 19 int N; 20 vl is;vl mul;vl add; 21 ll init; 22 ll merge(ll a, ll b) { 23 return min(a, b); 24 } 25 void push(int o, int L, int R, ll m, ll a) { 26 is[o] = is[o] * m + a; 27 mul[o] = mul[o] * m; 28 add[o] = add[o] * m + a; 29 } 30 31 SegMin(int n, ll init=INF) { 32 N = 1; 33 while (N < n) N *= 2; 34 this->init = init; 35 is = vl(N * 4, init); 36 mul = vl(N * 4, 1); 37 add = vl(N * 4); 38 } 39 40 SegMin(vl a, ll init=INF) { 41 int n = a.size(); 42 N = 1; 43 while (N < n) N *= 2; 44 this->init = init; 45 is = vl(N * 2); 46 mul = vl(N * 2, 1); 47 add = vl(N * 2); 48 copy(a.begin(), a.end(), is.begin() + N); 49 for (int i = N - 1; i > 0; i--) { 50 is[i] = merge(is[i << 1], is[i << 1 | 1]); 51 } 52 } 53 54 void update(int l, int r, ll m, ll a) { 55 if (l < r) update(1, 0, N, l, r, m, a); 56 } 57 58 void update(int o, int L, int R, int l, int r, ll m, ll a) { 59 if (l <= L && R <= r) { 60 push(o, L, R, m, a); 61 } else { 62 int M = (L + R) >> 1; 63 push(o, L, M, R); 64 if (l < M) update(o << 1, L, M, l, r, m, a); 65 if (r > M) update(o << 1 | 1, M, R, l, r, m, a); 66 is[o] = merge(is[o << 1], is[o << 1 | 1]); 67 } 68 } 69 70 void push(int o, int L, int M, int R) { 71 if (mul[o] != 1 || add[o] != 0) { 72 push(o << 1, L, M, mul[o], add[o]); 73 push(o << 1 | 1, M, R, mul[o], add[o]); 74 mul[o] = 1; 75 add[o] = 0; 76 } 77 } 78 79 ll query(int l, int r) { 80 if (l < r) return query(1, 0, N, l, r); 81 return init; 82 } 83 84 ll query(int o, int L, int R, int l, int r) { 85 if (l <= L && R <= r) { 86 return is[o]; 87 } else { 88 int M = (L + R) >> 1; 89 push(o, L, M, R); 90 ll res = init; 91 if (l < M) res = merge(res, query(o << 1, L, M, l, r)); 92 if (r > M) res = merge(res, query(o << 1 | 1, M, R, l, r)); 93 is[o] = merge(is[o << 1], is[o << 1 | 1]); 94 return res; 95 } 96 } 97 }; 98 99 int main(){ 100 ci(n),ci(s),ci(t);//s从1开始 101 s++,t++; 102 int ma=0; 103 for(int i=0;i<n;i++) ci(a[i].x),ci(a[i].y),ci(a[i].s); 104 for(int i=0;i<n;i++) a[i].x++,a[i].y++,ma=max(ma,a[i].y); 105 sort(a,a+n,cmp); 106 SegMin seg(ma+1); 107 seg.update(0,ma+1,0,INF); 108 seg.update(0,s,0,0); 109 110 for(int i=0;i<n;i++){ 111 if(a[i].y<s) continue; 112 int L=a[i].x-1,R=a[i].y; 113 ll res=seg.query(L,R)+a[i].s; 114 res=min(seg.query(R,R+1),res);//与前面的最小值取min 115 seg.update(R,R+1,0,res); 116 } 117 ll ans=seg.query(t,ma+1); 118 if(ans>=INF) puts("-1");//未覆盖到 119 else pl(ans); 120 return 0; 121 }
标签:zh-cn mes describe space ssi mem include int push
原文地址:https://www.cnblogs.com/mj-liylho/p/9502012.html