Educational Codeforces Round 81 (Rated for Div. 2) E - Permutation Separation (线段树+思维)

在这里插入图片描述
在这里插入图片描述
题意与思路:主要参考了大佬的做法,大佬的做法,真的很奇妙,真不知道我何时能达到如此水平。。。

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+1;
typedef long long ll;
ll p[maxn],a[maxn];
struct node{
	int l,r;
	ll minn,lazy;
}tree[maxn<<3];
void pushup(int x)
{
	if(tree[x].lazy)
	{
		tree[x<<1].minn+=tree[x].lazy;
		tree[x<<1|1].minn+=tree[x].lazy;
		tree[x<<1].lazy+=tree[x].lazy;
		tree[x<<1|1].lazy+=tree[x].lazy;
		tree[x].lazy=0; 
	}
}
void build(int l,int r,int x)
{
	tree[x].l=l;tree[x].r=r;
	if(l==r)
	{
		tree[x].minn=tree[x].lazy=0;
		return ;
	}
	int mid=(l+r)>>1;
	build(l,mid,x<<1);
	build(mid+1,r,x<<1|1);
	tree[x].minn=min(tree[x<<1].minn,tree[x<<1|1].minn);
}
void update(int l,int r,int v,int x)
{
	if(l<=tree[x].l&&tree[x].r<=r)
	{
		tree[x].lazy+=v;
		tree[x].minn+=v;
		return ;
	}
	pushup(x);
	int mid=(tree[x].l+tree[x].r)>>1;
	if(l<=mid) update(l,r,v,x<<1);
	if(r>mid)  update(l,r,v,x<<1|1);
	tree[x].minn=min(tree[x<<1].minn,tree[x<<1|1].minn);
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;++i) scanf("%lld",&p[i]);
	build(1,n,1);
	for(int i=1;i<=n;++i)
	scanf("%lld",&a[i]),update(p[i]+1,n,a[i],1);
	ll ans=min(a[1],a[n]);
	for(int i=1;i<n;++i) {
		update(p[i]+1,n,-a[i],1);//恢复原状 
		update(1,p[i]-1,a[i],1);//计算第一集合 
		ans=min(ans,tree[1].minn);
	}
	printf("%lld\n",ans);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值