zoj (单点更新区间查询：线段树)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define N 50005
using namespace std;
const long long Max=pow(10,10);
int n;
long long  a[N];
long long  b[N];
long long  dp[N];
long long  tree[N*4];
void built(int L,int R,int fa)
{
tree[fa]=Max;
if(L==R)
return ;
int mid=(L+R)/2;
built(L,mid,fa*2);
built(mid+1,R,fa*2+1);
}
int idex;
long long  val;
void uptate(int L,int R,int fa)
{
if(L==R)
{
tree[fa]=min(val,tree[fa]);
return;
}
int mid=(L+R)/2;
if(idex<=mid)
uptate(L,mid,fa*2);
else
uptate(mid+1,R,fa*2+1);
tree[fa]=min(tree[fa*2],tree[2*fa+1]);
}

int LL,RR;
long long query(int L,int R,int fa)
{
if(LL<=L&&RR>=R)
return tree[fa];
int mid=(R+L)/2;
long long t1=Max;
long long t2=Max;
if(LL<=mid)
t1=query(L,mid,fa*2);
if(RR>mid)
t2=query(mid+1,R,fa*2+1);
return min(t1,t2);
}

int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
for(int j=1;j<=n;j++)
scanf("%lld",&b[j]);
built(1,n,1);
dp[0]=0;
for(int i=1;i<=n;i++)
{
int last=i+b[i]-1;
last=min(n,last);
idex=last;
val=dp[i-1]+a[i];
uptate(1,n,1);
LL=i;
RR=n;
dp[i]=query(1,n,1);
}
printf("%lld\n",dp[n]);
}
return 0;
}