Destroying Array

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array consisting of n non-negative integers a1, a2, ..., an.

You are going to destroy integers in the array one by one. Thus, you are given the permutation of integers from 1 to n defining the order elements of the array are destroyed.

After each element is destroyed you have to find out the segment of the array, such that it contains no destroyed elements and the sum of its elements is maximum possible. The sum of elements in the empty segment is considered to be 0.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the length of the array.

The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).

The third line contains a permutation of integers from 1 to n — the order used to destroy elements.

Output

Print n lines. The i-th line should contain a single integer — the maximum possible sum of elements on the segment containing no destroyed elements, after first i operations are performed.

Examples
Input

Copy
4
1 3 2 5
3 4 1 2
Output

Copy
5
4
3
0
Input

Copy
5
1 2 3 4 5
4 2 3 5 1
Output

Copy
6
5
5
1
0
Input

Copy
8
5 5 4 4 6 6 5 5
5 2 8 7 1 3 4 6
Output

Copy
18
16
11
8
8
6
6
0
Note

Consider the first sample:

  1. Third element is destroyed. Array is now 1 3  *  5. Segment with maximum sum 5 consists of one integer 5.
  2. Fourth element is destroyed. Array is now 1 3  *   * . Segment with maximum sum 4 consists of two integers 1 3.
  3. First element is destroyed. Array is now  *  3  *   * . Segment with maximum sum 3 consists of one integer 3.
  4. Last element is destroyed. At this moment there are no valid nonempty segments left in this array, so the answer is equal to 0.

题意:给你一个含n给个非负整数的数组,并给出一个它们依此被删除的顺序,要求每次连续区间和最大的那个是多少
思路:一开始按题意模拟,果然TLE了,观察了一下样例就发现如果把删除序列看成加入序列就很容易地可以算出连续区间和最大是多少,并可以用并查集来维护这个连续区间和
题目给的删除顺序,现在我们考虑加入顺序,所以逆向读入顺序
最后一个没有加入元素,所以为0,在原删除顺序中表示全部删完了
标记添加过的元素
找现在这个元素的父亲
如果这个元素的右边被添加过了,就看它属于哪个连续的集合,再把当前这个集合加入并查集
更新孩子(当前这个元素的父亲)的父亲,并更新父亲的连续区间和
如果这个元素的左边被添加过了,与上同理
最后看看这个元素属于哪个并查集
比较上一个答案和现在这个并查集的答案,选一个最大的作为当前答案

(写了这题后发现并查集的模板并不像记忆中的那么复杂...)

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int amn=1e5+;
int n;
ll sum[amn],ans[amn];
bool used[amn];
int pre[amn],a[amn],order[amn];
void init(){
memset(used,,sizeof used);
for(int i=;i<=n;i++)pre[i]=i,sum[i]=a[i]; ///一开始它们的父亲是它们本身,它们的连续区间和是它们本身
}
int fd(int i){
return i==pre[i]?i:pre[i]=fd(pre[i]);
}
void add(int fa,int cd){
pre[cd]=fa; ///更新孩子的父亲
sum[fa]+=sum[cd]; ///更新父亲的连续区间和
}
int main(){
ios::sync_with_stdio();
cin>>n;
a[]=;
for(int i=;i<=n;i++){
cin>>a[i];
}
for(int i=n;i>=;i--){
cin>>order[i]; ///题目给的删除顺序,现在我们考虑加入顺序,所以逆向读入顺序
}
init();
ans[]=; ///最后一个没有加入元素,所以为0,在原删除顺序中表示全部删完了
int fa;
for(int i=;i<n;i++){
used[order[i]]=; ///标记添加过的元素
int ofa; ///找现在这个元素的父亲
if(used[order[i]+]){ ///如果这个元素的右边被添加过了,就看它属于哪个连续的集合,再把当前这个集合加入并查集
ofa=fd(order[i]);
fa=fd(order[i]+);
add(fa,ofa);
}
if(used[order[i]-]){ ///如果这个元素的左边被添加过了,与上同理
ofa=fd(order[i]);
fa=fd(order[i]-);
add(fa,ofa);
}
ofa=fd(order[i]); ///最后看看这个元素属于哪个并查集
ans[i]=max(ans[i-],sum[ofa]); ///比较上一个答案和现在这个并查集的答案,选一个最大的作为当前答案
}
for(int i=n-;i>=;i--){
printf("%lld\n",ans[i]);
}
}
/***
给你一个含n给个非负整数的数组,并给出一个它们依此被删除的顺序,要求每次连续区间和最大的那个是多少
一开始按题意模拟,果然TLE了,就发现如果把删除序列看成加入序列就很容易地可以算出连续区间和最大是多少,并可以用并查集来维护这个连续区间和
题目给的删除顺序,现在我们考虑加入顺序,所以逆向读入顺序
最后一个没有加入元素,所以为0,在原删除顺序中表示全部删完了
标记添加过的元素
找现在这个元素的父亲
如果这个元素的右边被添加过了,就看它属于哪个连续的集合,再把当前这个集合加入并查集
更新孩子(当前这个元素的父亲)的父亲,并更新父亲的连续区间和
如果这个元素的左边被添加过了,与上同理
最后看看这个元素属于哪个并查集
比较上一个答案和现在这个并查集的答案,选一个最大的作为当前答案
***/

[并查集+逆向思维]Codeforces Round 722C Destroying Array的更多相关文章

  1. HDU 4496 并查集 逆向思维

    给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...

  2. Codeforces Round #504 D. Array Restoration

    Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...

  3. CodeForces 722C Destroying Array (并查集)

    题意:给定 n 个数,然后每次破坏一个位置的数,那么剩下的连通块的和最大是多少. 析:用并查集来做,从后往前推,一开始什么也没有,如果破坏一个,那么我们就加上一个,然后判断它左右两侧是不是存在,如果存 ...

  4. CodeForces - 722C Destroying Array (并查集/集合的插入和删除)

    原题链接:https://vjudge.net/problem/511814/origin Description: You are given an array consisting of n no ...

  5. Codeforces 722C. Destroying Array

    C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. 【搜索】【并查集】Codeforces 691D Swaps in Permutation

    题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...

  7. hdu 5652 India and China Origins(二分+bfs || 并查集)BestCoder Round #77 (div.2)

    题意: 给一个n*m的矩阵作为地图,0为通路,1为阻碍.只能向上下左右四个方向走.每一年会在一个通路上长出一个阻碍,求第几年最上面一行与最下面一行会被隔开. 输入: 首行一个整数t,表示共有t组数据. ...

  8. Educational Codeforces Round 21 D.Array Division(二分)

    D. Array Division time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

  9. Educational Codeforces Round 11A. Co-prime Array 数学

    地址:http://codeforces.com/contest/660/problem/A 题目: A. Co-prime Array time limit per test 1 second me ...

随机推荐

  1. spring——AOP原理及源码(二)

    回顾: 在上一篇中,我们提到@EnableAspectJAutoProxy注解给容器中加入了一个关键组件internalAutoProxyCreator的BeanDefinition,实际类型为 An ...

  2. 利用GitHub制作在线炫酷简历

    首先我们先体验一下炫酷简历.然后决定我们要不要使用. https://jessezhao1990.github.... 如何使用本项目部署你自己的在线简历 1. 书写简历 在src文件夹里面有个con ...

  3. PAT资料,持续更新中~~~愿诸君共勉

    <算法笔记>胡凡著,<算法笔记-上机实战训练指南>胡凡著 <经典算法大全> <C陷阱与缺陷> <C程序设计语言-K&R> 链接:ht ...

  4. java后台生成并下载二维码

    这个功能在项目开发中是很基础的,平时用到的也很多,这里简单记录一下,以便以后使用的时候参考 前提业务要求:前台页面展示数据,有下载按钮,点击下载,下载对应数据的二维码. 首先,在pom.xml文件中添 ...

  5. NLP(二十五)实现ALBERT+Bi-LSTM+CRF模型

      在文章NLP(二十四)利用ALBERT实现命名实体识别中,笔者介绍了ALBERT+Bi-LSTM模型在命名实体识别方面的应用.   在本文中,笔者将介绍如何实现ALBERT+Bi-LSTM+CRF ...

  6. django 从零开始 8 用户登录验证 待测

    看文档 djang 自带一个用户登录验证的方法,不过有些看着懵逼,去网上找了一圈,发现很多都是照抄文档说明的,几乎没说啥原理 特别是 from django.contrib.auth import a ...

  7. Visual Studio 安装中出现闪退

    问题描述:win7 系统下, 安装 Visual Studio Community 2017 过程中,安装界面闪退 原因:Visual Studio 的版本低了 解决方案:选择 Visual Stud ...

  8. 通过CGAL将一个多边形剖分成Delaunay三角网

    目录 1. 概述 2. 实现 3. 结果 4. 参考 1. 概述 对于平面上的点集,通过Delaunay三角剖分算法能够构建一个具有空圆特性和最大化最小角特性的三角网.空圆特性其实就是对于两个共边的三 ...

  9. [POI2017]bzoj4726 Sadota?

    题目描述 离线题库请 题目描述 某个公司有\(n\)个人, 上下级关系构成了一个有根树.其中有个人是叛徒(这个人不知道是谁).对于一个人, 如果他 下属(直接或者间接, 不包括他自己)中叛徒占的比例超 ...

  10. Git&sourceTree软件安装、使用说明及遇到问题解决

    一.软件版本 1.Git版本为1.9.5 2.Source版本为1.5.2 二.软件安装步骤 1.Git安装步骤 1)双击Git安装文件进入下图界面,单击Next 2)继续Next 3)进入Selec ...