"Topic link"
http://www.lydsy.com/JudgeOnline/problem.php?id=1835
Test instructions
There are n villages, each of which is located in D[i], which requires the establishment of no more than K base stations, the cost of building a base station in village I is c[i], if there is a base station within the village I not more than S[i], the village is covered, village I is not covered by the compensation fee of W[i], for the least cost.
Ideas
F[I][J] Indicates the minimum cost of the first J base station of village I, the Transfer type:
f[i][j]=min{f[k][j-1]+cost (k,i)} + c[i], j-1<=k<=i-1
Cost (K,i) =sigma{w[x]} k+1<=x<=i-1, and X is not overwritten
F[][] requires an interval minimum, and we try to maintain this value for each layer with a segment tree.
Enumeration J, consider each layer I.
We set St[i],ed[i], respectively, in the I distance I farthest st[i],ed[i] base station can still cover to I, assuming we have finished F[I][J] request F[i+1][j], consider those can be covered by I and can not be covered by i+1, that is to meet ed[x ]=i points, add w[x to the segment tree values in the [1..st[x]-1] interval. For F[i], the minimum number of segment tree values in the query interval [1..i-1] can be obtained.
Where St[i],ed[i] can be obtained by dichotomy.
The segment tree provides an operation for interval operation interval queries.
The total time complexity is O (NMLOGN)
Spicy Chicken Line tree, ruin my youth (even a line tree will not write t^t
Code
1#include <Set>2#include <cmath>3#include <queue>4#include <vector>5#include <cstdio>6#include <cstring>7#include <iostream>8#include <algorithm>9 #definefor (A,B,C) for (int a= (b); a<= (c); a++)Ten using namespacestd; One AtypedefLong Longll; - Const intN = 1e5+Ten; - Const intINF =1e9; the - ll Read () { - CharC=GetChar (); -ll f=1, x=0; + while(!IsDigit (c)) { - if(c=='-') f=-1; C=GetChar (); + } A while(IsDigit (c)) atx=x*Ten+c-'0', c=GetChar (); - returnx*F; - } - - intn,k; ll F[n]; - ll D[n],c[n],s[n],w[n],st[n],ed[n]; inVector<ll>Ep[n]; - to structTnode { + intl,r; ll V,tag; -}t[n<<1]; the * voidPushdown (intu) $ {Panax Notoginseng if(t[u].l==t[u].r| | (! T[u].tag))return ; -ll& t=T[u].tag; thet[u<<1].v+=t,t[u<<1].tag+=T; +t[u<<1|1].v+=t,t[u<<1|1].tag+=T; At=0; the } + voidMaintain (intu) - { $T[u].v=min (t[u<<1].v,t[u<<1|1].v); $ } - voidBuildintUintLintR) - { theT[u].l=l,t[u].r=R; -t[u].tag=0;Wuyi if(L==R) t[u].v=F[l]; the Else { - intMid=l+r>>1; WuBuild (u<<1, l,mid); -Build (u<<1|1, mid+1, R); About Maintain (u); $ } - } - voidADD (intUintLintr,ll x) - { A if(L>r)return;//Handling L>r + pushdown (u); the if(l<=t[u].l&&t[u].r<=R) -t[u].v+=x,t[u].tag+=x; $ Else { the intMid=t[u].l+t[u].r>>1; the if(L<=mid) ADD (u<<1, l,r,x); the if(Mid<r) ADD (u<<1|1, l,r,x); the Maintain (u); - } in } thell query (intUintLintR) the { About if(L>r)return 0; the pushdown (u); the if(L<=T[U].L&&T[U].R<=R)returnt[u].v; the Else { + intMid=t[u].l+t[u].r>>1; ll ans=inf; - if(L<=mid) Ans=min (Ans,query (u<<1, L,r)); the if(mid<r) Ans=min (Ans,query (u<<1|1, L,r));Bayi returnans; the } the } - - //Lower_bound defined as a pointer to find the first number not less than V the voidInit () the { theN=read (), k=read (); thefor (I,2, N) d[i]=read (); -for (I,1, N) c[i]=read (); thefor (I,1, N) s[i]=read (); thefor (I,1, N) w[i]=read (); then++,k++;94D[n]=inf; w[n]=inf; thefor (I,1, N) { the intl=d[i]-s[i],r=d[i]+S[i]; theL=lower_bound (d+1, d+n+1, L)-D;98R=lower_bound (d+1, d+n+1, R)-D; About if(D[i]+s[i]<d[r]) r--; -st[i]=l,ed[i]=R;101 Ep[ed[i]].push_back (i);102 }103 }104 ll DP () the {106ll ans,tmp=0;107for (I,1, N) {108f[i]=tmp+C[i];109For (J,0,(int) Ep[i].size ()-1) thetmp+=W[ep[i][j]];111 } theans=F[n];113For (J,2, K) { theBuild1,1, n); thefor (I,1, N) { theF[i]=query (1,1, I-1)+C[i];117For (K,0,(int) Ep[i].size ()-1) {118 intx=Ep[i][k];119ADD (1,1, st[x]-1, w[x]); - }121 }122ans=min (ans,f[n]);123 }124 returnans; the }126 127 intMain () - { 129 //freopen ("in.in", "R", stdin); the //freopen ("Out.out", "w", stdout);131 init (); theprintf"%lld", DP ());133 return 0;134}
Bzoj 1835 [Zjoi2010]base Base Station location (dp+ segment tree)