「CF515E」 Drazil and Park

题意

有一组圆环排列的树,给出 i i i i + 1 i+1 i+1 n n n 到 1)的距离 d i d_i di 和第 i i i 棵树的高度 h i h_i hi

一只猴子每天选择两棵树 x , y x,y x,y,然后消耗 2 ( h x + h y ) + d i s t ( x , y ) 2(h_x+h_y)+dist(x,y) 2(hx+hy)+dist(x,y) 的体力,其中 d i s t ( x , y ) dist(x,y) dist(x,y) 表示 x x x y y y 的距离。

每天都有孩子在 a i ∼ b i a_i\sim b_i aibi 的区间内玩耍,猴子只能在其他区间内活动,因此 d i s t ( x , y ) dist(x,y) dist(x,y) 是一定的。

给出每天的 a , b a,b a,b,求猴子每天能消耗的最大体力。

分析

求区间最大值,考虑用线段树。

两棵树之间的距离可以破环成链后前缀和求解,设 s u m i sum_i sumi 为第 i i i 位之前的距离和。

不妨设 y > x y>x y>x,则原函数为 2 h x + 2 h y + s u m y − s u m x = 2 h x − s u m x + 2 h y + s u m y 2h_x+2h_y+sum_y-sum_x=2h_x-sum_x+2h_y+sum_y 2hx+2hy+sumysumx=2hxsumx+2hy+sumy

可以用线段树维护 2 h i + s u m i 2h_i+sum_i 2hi+sumi 2 h i − s u m i 2h_i-sum_i 2hisumi,然后区间查找最大值即可。(其实是很裸的线段树)

Code

#include<bits/stdc++.h>
#define ls (x<<1)
#define rs (x<<1|1)
#define mid ((l+r)>>1)
typedef long long ll;
using namespace std;
inline ll read(){ll x=0,f=1;char c=getchar();while(c<48||c>57){if(c==45)f=0;c=getchar();}while(c>47&&c<58)x=(x<<3)+(x<<1)+(c^48),c=getchar();return f?x:-x;}
const ll maxn=2e5+5;
ll n,m,d[maxn],h[maxn],sum[maxn];
struct linetree{ll v1,v2,ans;}t[maxn<<2];
inline linetree operator+(linetree a,linetree b){
	return {max(a.v1,b.v1),max(a.v2,b.v2),max({a.ans,b.ans,a.v2+b.v1})};
}
inline void buildtree(ll x,ll l,ll r){
    if(l==r){
        t[x].v1=h[l]*2+sum[l];
		t[x].v2=h[l]*2-sum[l];
        return;
    }
    buildtree(ls,l,mid),buildtree(rs,mid+1,r);
    t[x]=t[ls]+t[rs];
}
inline linetree treeask(ll x,ll s,ll e,ll l,ll r){
    if(l>=s&&r<=e)return t[x];
	if(e<=mid)return treeask(ls,s,e,l,mid);
	else if(s>mid)return treeask(rs,s,e,mid+1,r);
	return treeask(ls,s,e,l,mid)+treeask(rs,s,e,mid+1,r);
}
signed main(){
	n=read(),m=read();
	for(ll i=1;i<=n;++i)d[i]=d[i+n]=read();
	for(ll i=1;i<=n;++i)h[i]=h[i+n]=read();
	for(ll i=1;i<=n*2;++i)sum[i]=sum[i-1]+d[i-1];
	buildtree(1,1,n*2);
	while(m--){
		ll a=read(),b=read(),ans=0;
		if(a<=b){
			if(a!=1)ans=max(ans,treeask(1,1,a-1,1,n<<1).ans);
			if(b!=n){
				ans=max(ans,treeask(1,b+1,a+n-1,1,n<<1).ans);
				ans=max(ans,treeask(1,b+n+1,n<<1,1,n<<1).ans);
			}
		}
		else ans=max(ans,treeask(1,b+1,a-1,1,n<<1).ans);
		printf("%lld\n",ans);
	}
}
  • 8
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值