预处理把左集划分为大小为1~i-1时,把全部元素都移动到右集的代价,记作sum[i]。

然后枚举终态时左集的大小,更新把元素i 留在/移动到 左集的代价。

树状数组/线段树处理区间修改/区间查询

 #define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e5+;
struct Tree{
ll minn,lazy;
}tree[N<<];
ll sum[N];//前缀和
inline void build(int root,int l,int r){
if(l==r){
tree[root].minn=sum[l];//1~l的a[i]之和
tree[root].lazy=;
return;
}
int mid=(l+r)>>;
build((root<<),l,mid);
build((root<<|),mid+,r);
tree[root].minn=min(tree[(root<<)].minn,tree[(root<<|)].minn);//up
return;
}
inline void pushdown(int root){
if(!tree[root].lazy)
return;
tree[(root<<)].minn+=tree[root].lazy;
tree[(root<<|)].minn+=tree[root].lazy;
tree[(root<<)].lazy+=tree[root].lazy;
tree[(root<<|)].lazy+=tree[root].lazy;
tree[root].lazy=;
return;
}
inline void change(int root,int l,int r,int x,int y,int val){
if(r<x||l>y)
return;
if(x<=l&&r<=y){
tree[root].minn+=val;
tree[root].lazy+=val;
return;
}
int mid=(l+r)>>;
pushdown(root);
change((root<<),l,mid,x,y,val);
change((root<<|),mid+,r,x,y,val);
tree[root].minn=min(tree[(root<<)].minn,tree[(root<<|)].minn);//up
return;
}
int n,p[N],a[N],pos[N];
ll ans;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n;
for(int i=;i<=n;++i){
cin>>p[i];
pos[p[i]]=i;//数字p[i]出现的位置为i
}
for(int i=;i<=n;++i){
cin>>a[i];
sum[i]=sum[i-]+a[i];//sum[i]为左集合大小为i,把左集合所有元素都移动到右集合的花费
}
build(,,n-);
ans=min(a[],a[n]);//a[1]为左集为空,a[n]为右集为空
for(int i=;i<n;++i){//枚举左集大小,定下大小后,集合内元素也被定为1~i
change(,,n-,,pos[i]-,a[pos[i]]);//找到元素i出现的位置,在它出现位置左边的sum[i]分别加上把元素i从右集合移动到左集合的代价(原本的sum[1~i-1]为把原本处于位置1~i-1的元素都移动到右边,此时加上元素1~i从右移动到左的代价)
change(,,n-,pos[i],n,-a[pos[i]]);//在它出现位置及其右边的sum[i]分别减去把元素i从左集合移动到右集合的代价(元素i无需移动,可是移动的代价事先已经加到sum[i~n]里了)
ans=min(ans,tree[].minn);//如果左集大小为i的代价最小就更新最小值
}
cout<<ans;
return ;
}

Educational Codeforces Round 81 (Rated for Div. 2)E(线段树)的更多相关文章

  1. Educational Codeforces Round 61 (Rated for Div. 2) G(线段树,单调栈)

    #include<bits/stdc++.h>using namespace std;int st[1000007];int top;int s[1000007],t[1000007];i ...

  2. [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和)

    [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和) E. Permuta ...

  3. Educational Codeforces Round 81 (Rated for Div. 2) A-E简要题解

    链接:https://codeforces.com/contest/1295 A. Display The Number 贪心思路,尽可能放置更多位,如果n为奇数,消耗3去放置一个7,剩下的放1 AC ...

  4. Educational Codeforces Round 81 (Rated for Div. 2) B. Infinite Prefixes

    题目链接:http://codeforces.com/contest/1295/problem/B 题目:给定由0,1组成的字符串s,长度为n,定义t = sssssss.....一个无限长的字符串. ...

  5. Educational Codeforces Round 81 (Rated for Div. 2) C. Obtain The String

    题目链接:http://codeforces.com/contest/1295/problem/C 题目:给定字符串s,t.  给定一个空串z,需要按照规则把z构造成 string z == stri ...

  6. Educational Codeforces Round 81 (Rated for Div. 2)

    A 0~9需要多少笔画,自取7和1,判奇偶 #include<bits/stdc++.h> using namespace std; #define ll long long #defin ...

  7. Educational Codeforces Round 81 (Rated for Div. 2) 题解

    过了n天补的题解:D AB就不用说了 C. Obtain The String 思路挺简单的,就是贪心,但是直接贪心的复杂度是O(|s|*|t|),会超时,所以需要用到序列自动机 虽然名字很高端但是就 ...

  8. Educational Codeforces Round 81 (Rated for Div. 2) - D. Same GCDs(数学)

    题目链接:Same GCDs 题意:给你两个数$a$,$m(1 \leq a < m \leq 10^{10})$,求有多少个$x$满足:$0 \leq x < m$且$gcd(a,m)= ...

  9. Educational Codeforces Round 80 (Rated for Div. 2)E(树状数组,模拟,思维)

    #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ],mx[],a[],pos[],sum ...

随机推荐

  1. windows安装python64位和32位的方法

    1.先安装python 64位的 python,创建一个64位的python虚拟环境: 2.再安装python 32位的 python,创建一个32位的python虚拟环境即可. 注意:两个版本安装在 ...

  2. Linux 环境c++ 编码转换

    #include <iconv.h> //代码转换:从一种编码转为另一种编码 static int CodeConvert(char *from_charset,char *to_char ...

  3. api接口出现Provisional headers are shown,

    问题分析:根据反馈可以知道,发起请求,但服务器未及时响应,原因可能是超时,或者被拦截

  4. c语言修炼之一

    1.C项目要高内聚(模块功能必须明确,一个模块完成一个功能).低耦合(接口尽可能简单,减少各模块间的联系). 2.register类型不能为模块间的全局变量.模块内的全局变量.局部static变量.( ...

  5. sysbench下载与安装

    目标:下载.安装sysbench软件,做数据库压测 准备: 在sysbench启动的linux机器上,首先安装好mysql,查看mysql已经启动 例如,在机器上已经安装完mysql,其路径为 /us ...

  6. 融e学 一个专注于重构知识,培养复合型人才的平台【获取考试答案_破解】

    考试系统-融e学-一个专注于重构知识,培养复合型人才的平台.[获取答案] ganquanzhong 背景:今天去完成学校在融e学上开设的必修课和选修课考试,由于自己的时间有限(还有其他的事情要去做). ...

  7. python3练习100题——013

    熟悉的水仙花数来了,,,... 原题链接:http://www.runoob.com/python/python-exercise-example13.html 题目:打印出所有的"水仙花数 ...

  8. 三分钟快速上手TensorFlow 2.0 (下)——模型的部署 、大规模训练、加速

    前文:三分钟快速上手TensorFlow 2.0 (中)——常用模块和模型的部署 TensorFlow 模型导出 使用 SavedModel 完整导出模型 不仅包含参数的权值,还包含计算的流程(即计算 ...

  9. Git 添加远程github仓库的时候提示错误:fatal: remote origin already exists.

    1.先删除远程 Git 仓库 $ git remote rm origin 2.再添加远程 Git 仓库 $ git remote add origin git@github.com:wsydxian ...

  10. H5 App设计者需要注意的问题

    我们通常在做H5 APP设计的过程中,遇到很多看似很小,且很容易被忽略的问题,正是这些小问题,一次次的撩拨用户的耐心,让用户对你的APP心生怨念.现在WeX5君呕血为大家整理出H5 APP设计的21条 ...