BZOJ 1835 基站选址(DP+线段树)

BZOJ 1835 基站选址(DP+线段树)

# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-
# define MOD
# define INF
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<,l,mid
# define rch p<<|,mid+,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... int n, k, d[N], c[N], s[N], w[N], bg[N], ed[N], seg[N<<], tag[N<<];
VI v[N];
LL dp[N]; void _init(){
FOR(i,,n) {
int l=d[i]-s[i], r=d[i]+s[i];
l=lower_bound(d+,d+n+,l)-d; r=lower_bound(d+,d+n+,r)-d;
if (d[r]>d[i]+s[i]) --r;
bg[i]=l; ed[i]=r; v[r].pb(i);
}
}
void push_up(int p){seg[p]=min(seg[p<<],seg[p<<|]);}
void push_down(int p){
if (!tag[p]) return ;
seg[p]+=tag[p]; tag[p<<]+=tag[p]; tag[p<<|]+=tag[p]; tag[p]=;
}
void init(int p, int l, int r){
if (l<r) {
int mid=(l+r)>>;
tag[p]=; init(lch); init(rch); push_up(p);
}
else seg[p]=dp[l], tag[p]=;
}
int query(int p, int l, int r, int L, int R){
push_down(p);
if (L>r||R<l) return INF;
if (L<=l&&R>=r) return seg[p];
int mid=(l+r)>>;
return min(query(lch,L,R),query(rch,L,R));
}
void update(int p, int l, int r, int L, int R, int val){
push_down(p);
if (L>r||R<l) return ;
if (L<=l&&R>=r) tag[p]=val, push_down(p);
else {
int mid=(l+r)>>;
update(lch,L,R,val); update(rch,L,R,val); push_up(p);
}
}
int main ()
{
scanf("%d%d",&n,&k);
FOR(i,,n) scanf("%d",d+i); FOR(i,,n) scanf("%d",c+i);
FOR(i,,n) scanf("%d",s+i); FOR(i,,n) scanf("%d",w+i);
++n; ++k; d[n]=INF; w[n]=INF;
_init();
LL ans, tmp=;
FOR(i,,n) {
dp[i]=tmp+c[i];
FO(j,,v[i].size()) tmp+=w[v[i][j]];
}
ans=dp[n];
FOR(i,,k) {
init(,,n);
FOR(j,,n) {
dp[j]=(j>?query(,,n,,j-):)+c[j];
FO(l,,v[j].size()) if (bg[v[j][l]]>) update(,,n,,bg[v[j][l]]-,w[v[j][l]]);
}
ans=min(ans,dp[n]);
}
printf("%lld\n",ans);
return ;
}
上一篇:【题解】Luogu P2605 [ZJOI2010]基站选址


下一篇:HDU2544-最短路(最短路模版题目)