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. 11. 无数人难办事? - 迪米特法则(LoD)

    11.1 第一天上班 时间: 4月2日19点   地点: 小菜大鸟住所的客厅   任务: 小菜, 大鸟      "回来啦! 怎么样? 第一天上班感受多吧." 大鸟关关心的问道.  ...

  2. 学习HEXO的历程

    前言: 简介 开始搭建 命令 API测试 逛github相关的帖子时,发现了hexo.正好想要做一个个人的博客,用来记录自己的各类感悟,所以花一些时间学习学习,以后博客可以放github,省得去注册c ...

  3. http客户端如何写

    使用wireshark协助,设置网卡本地,设置过滤器:http && (ip.src == 192.168.1.80 && ip.dst == 192.168.1.81 ...

  4. LeetCode~1033.移动石子直到连续

    1033.移动石子直到连续 三枚石子放置在数轴上,位置分别为 a,b,c. 每一回合,我们假设这三枚石子当前分别位于位置 x, y, z 且 x < y < z.从位置 x 或者是位置 z ...

  5. Python——12类的继承

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  6. ubuntu 18.04 添加快快捷方式

    1. 创建启动软件的快捷方式的文件 # cd ~/桌面 # vim pycharm18.1.desktop # 注意文件的后缀是 .desktop [Desktop Entry] Version=1. ...

  7. Apple App签名机制

    概览 数字签名 签名机制与验证过程 操作流程 数字签名 摘要算法 将任意长度文本通过一个算法得到一个固定长度的文本. 源文本不同,计算结果必然不同 无法从结果反推源 例如,MD5和SHA算法 非对称加 ...

  8. Python -Selenium的安装和调用

    安装selenium步骤: 1.安装pip(cmd命令行管理员方式): pip install pip 也可直接搜索pip,到官网下载安装 2.安装selenium(cmd命令行管理员方式): pip ...

  9. Python——工厂模式

    目录 前言 一.简单工厂 二.工厂方法 抽象工厂 结论 参考 前言 工厂模式,顾名思义就是我们可以通过一个指定的"工厂"获得需要的"产品". 在设计模式中主要用 ...

  10. jsp内置对象(三)-----response对象

    response对象  response对象包含了响应客户端请求的有关信息,但在JSP中很少直接用到它.他是HttpServletResponse类的实例,response对象具有页面作用域,即访问一 ...