Problem J. Journey to the “The World’s Start”

存个代码

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
#define lson root*2,l,Mid
#define rson root*2+1,Mid+1,r
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f3f3f3f3fll
#define maxn 50010
int d[maxn];
int p[maxn];
struct SGT{
	LL tree[maxn*4+10];
	LL lazy[maxn*4+10];
	void Init(){
		memset(lazy,0,sizeof(lazy));
		memset(tree,0x3f,sizeof(tree));
	}
	void pushUp(int root){
		tree[root] = min(tree[root<<1],tree[root<<1|1]);
	}
	void pushDown(int root){
		tree[root<<1] += lazy[root];
		tree[root<<1|1] += lazy[root];
		lazy[root<<1] += lazy[root];
		lazy[root<<1|1] += lazy[root];
		lazy[root] = 0;
	}
	void update(int root,int l,int r,int x,LL y){
		if(l == r){
			tree[root] = y;
			lazy[root] = 0;
			return;
		}
		int Mid = (l+r)>>1;
		pushDown(root);
		if(x <= Mid)
			update(lson,x,y);
		else
			update(rson,x,y);
		pushUp(root);
	}
	void update2(int root,int l,int r,int ll,int rr,LL y){
		if(ll <= l && r <= rr){
			tree[root] += y;
			lazy[root] += y;
			return;
		}
		int Mid = (l+r)>>1;
		pushDown(root);
		if(ll <= Mid)
			update2(lson,ll,rr,y);
		if(Mid < rr)
			update2(rson,ll,rr,y);
		pushUp(root);
	}
	LL query(int root,int l,int r,int ll,int rr){
		if(ll <= l && r <= rr)
			return tree[root];
		LL ans = INF;
		int Mid = (l+r)>>1;
		if(ll <= Mid)
			ans = min(ans,query(lson,ll,rr));
		if(Mid<rr)
			ans = min(ans,query(rson,ll,rr));
		return ans;
	}
};
SGT sgt;
LL check(int n,int x){
	sgt.Init();
	sgt.update(1,1,n,1,0);
	for(int i = 2; i <= n; i++){
		int le = max(1,i-x);
		sgt.update2(1,1,n,le,i-1,1);
		LL tmp = sgt.query(1,1,n,le,i-1)+d[i];
		sgt.update(1,1,n,i,tmp);
		if(i == n)
			return tmp;
	}
}
int main(){
	int n,t;
	freopen("journey.in","r",stdin);
	freopen("journey.out","w",stdout);
	while(~scanf("%d %d",&n,&t)){
		for(int i = 1; i <= n-1; i++)
			scanf("%d",&p[i]);
		int mmm = p[n-1];
		for(int i = n-1; i >= 1; i--){
			mmm = min(mmm,p[i]);
			p[i] = mmm;
		}
		d[1] = 0;
		d[n] = 0;
		for(int i = 2; i <= n-1; i++)
			scanf("%d",&d[i]);
		int ans = p[n-1];
		int l = 1,r = n-1;
		while(l+1<r){
			int mid = (l+r)>>1;
			if(check(n,mid)<=t)
				r = mid;
			else
				l = mid;
		}
		if(check(n,1)<=t)
			printf("%d\n",p[l]);
		else
			printf("%d\n",p[r]);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值