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
4
1 3 2 5
3 4 1 2
Output
5
4
3
0
Input
5
1 2 3 4 5
4 2 3 5 1
Output
6
5
5
1
0
Input
8
5 5 4 4 6 6 5 5
5 2 8 7 1 3 4 6
Output
18
16
11
8
8
6
6
0
Note
Consider the first sample:

Third element is destroyed. Array is now 1 3  *  5. Segment with maximum sum 5 consists of one integer 5.
Fourth element is destroyed. Array is now 1 3  *   * . Segment with maximum sum 4 consists of two integers 1 3.
First element is destroyed. Array is now  *  3  *   * . Segment with maximum sum 3 consists of one integer 3.
Last element is destroyed. At this moment there are no valid nonempty segments left in this array, so the answer is equal to 0.
题意:按着第二个数组给出的数组序列删除元素,每一次删除之后问最大的连续序列的和是多少。
思路:删除的不好弄,那么我们就倒着来,按着增加的来算。对于位置i的元素,我们尝试着去把它和i+1和i-1合并,优先队列维护最大值。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=1e5+100;
ll a[maxx];
ll ans[maxx];
int b[maxx];
int f[maxx];
ll dis[maxx];
int n;inline int getf(int u)
{return (u==f[u]?u:f[u]=getf(f[u]));
}
int main()
{scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);for(int i=1;i<=n;i++) scanf("%d",&b[i]);for(int i=1;i<=n;i++) f[i]=i,dis[i]=-1;//因为数组元素有可能为0,所以起始值设置为-1。ans[n]=0;priority_queue<ll> q;q.push(0);for(int j=n;j>=1;j--){dis[b[j]]=a[b[j]];q.push(dis[b[j]]);int i=b[j];if(i+1<=n&&dis[i+1]!=-1){int t1=getf(i+1);int t2=getf(i);f[t1]=t2;dis[t2]+=dis[t1];dis[t1]=0;q.push(dis[t2]);}if(i-1>=1&&dis[i-1]!=-1){int t1=getf(i-1);int t2=getf(i);f[t1]=t2;dis[t2]+=dis[t1];dis[t1]=0;q.push(dis[t2]);}ans[j-1]=q.top();}for(int i=1;i<=n;i++) cout<<ans[i]<<endl;
}

努力加油a啊,(o)/~

Destroying Array(并查集)相关推荐

  1. 并查集巧妙用法(codeforces)C. Destroying Array

    C. Destroying Array 题意:对样例进行分析: ,n == 4,数组是1 3 2 5,然后下一行就是每一个数就得输出,代表的是删除数组中的第几个数,然后在删除后的数组中找到分成的段的和 ...

  2. 【CodeForces - 722C】Destroying Array(并查集,时光倒流)

    题干: 给定一个有n个数的序列a1,a2, ..., an 你每次可以将序列中一个数删去,剩下的数会被分割为一段一段连续的数列 给定一个删除数的顺序,问在每次删除之后,剩下的连续的数列中,数列和的最大 ...

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

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

  4. CodeForces - 722C Destroying Array(倒着并查集+离线处理)

    题目链接:点击查看 题目大意:给出一个数列a,现在给出操作b,每次操作都会删除掉数列a中指定位置的数,问每次删除后,最大连续字段和是多少 题目分析:一开始看到最大连续字段和,以为是要用dp,又看了一下 ...

  5. Codeforces 722C. Destroying Array

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

  6. Codeforces C. Destroying Array

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

  7. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C 倒序并查集

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

  8. [CF722C] Destroying Array

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

  9. bzoj 1050: [HAOI2006]旅行comf(codevs.cn 1001 舒适的路线) 快排+并查集乱搞

    没用的话:好像很久没发博客了,主要是懒太蒟找不到水题.我绝对没弃坑...^_^ 还用些话:本文为博主原创文章,若转载请注明原网址和作者. 进入正题: 先pa网址: bzoj :http://www.l ...

  10. BZOJ 1050: [HAOI2006]旅行comf(枚举+并查集)

    [HAOI2006]旅行comf Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点 ...

最新文章

  1. 来活儿了!赶紧检查下代码里有没有脏话...
  2. python open方法下file模块_python 文件操作
  3. ewebeditor 图片上传中 请等待_AC米兰客场3-1那不勒斯,博内拉透露:伊布伤势情况需要等待...
  4. 编程隐藏child指定列_简单的Excel VBA编程问题解答——完美Excel第183周小结
  5. (十)数据结构之“堆”
  6. html仿微信滑动删除,使用Vue实现移动端左滑删除效果附源码
  7. arcgis利用python赋值
  8. O - Muddy Fields
  9. 从程序详解拒绝服务攻击
  10. mysql centos 配置文件路径_Centos7 查看Mysql配置文件
  11. 天轰穿·甜老丝儿。科创少年
  12. ips细胞最新进展:利用iPS细胞成功培养出抑制宫颈癌繁殖的免疫杀伤T细胞,有望实现宫颈癌的免疫细胞疗法
  13. MongoDB系列四:解决secondary的读操作
  14. Java数组以及冒泡排序--------07
  15. Vue通过nginx转发后dist文件页面样式丢失
  16. SAP批量修改物料标准成本
  17. Google学术映像
  18. java开发工具的下载和破解
  19. Classpath entry *.jar will not be exported or published.禁告
  20. 关于:last-child的误解

热门文章

  1. IOS15打包静态库
  2. shinelon笔记本进bios设置u盘启动_U盘启动快捷键、开机进BIOS按键查询
  3. dataframe保存为txt_竟然可以用 Python 抓取公号文章保存成 PDF
  4. object 'libproxychains.so.3' from LD_PRELOAD cannot be preloaded
  5. 渗透测试报告封面样本
  6. retrofit2使用详解_秒懂Retrofit2之Converter
  7. JAVA循环与分支语句edu_Java分支结构和循环结构原理与用法详解
  8. 微信支付成功但是微信分享却失败了
  9. 小米盒子老是服务器无响应,教你解决小米盒子黑屏死机等故障解决办法!
  10. iOS 4的无线部署(Xcode3.2 及 Xcode4.2 图文解说)