C. 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
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:

  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的序列,每次删除一个(删除之后序列断开),求最大连续子段和。(序列中数为正整数)


sol:正着做的话感觉做法有点谐,那么把询问离线,用并查集将数字一个一个连通起来,每一个点带一个权值,将每一个连续序列的权值记到父亲节点上

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<cmath>
#include<ctime>
#include<cstring>
#define yyj(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout);
#define llg long long
#define maxn 100010
#define md 50000
#define inf 0x7fffffff
using namespace std;
llg i,j,k,n,m,a[maxn],f1,f2,maxl,c[maxn],ans[maxn],dad[maxn],bj[maxn],x,val[maxn]; llg find (llg x)
{
return dad[x]==x?x:dad[x]=find(dad[x]);
} int main()
{
// yyj("c");
cin>>n;
for (i=;i<=n;i++) scanf("%I64d",&a[i]),dad[i]=i;
for (i=;i<=n;i++) scanf("%I64d",&c[i]);
for (i=n;i>=;i--)
{
maxl=max(maxl,a[c[i]]);
bj[c[i]]=; val[c[i]]+=a[c[i]];
x=c[i];
if (bj[x-]!= && bj[x+]!=)
{
f1=find(x-);
dad[find(x)]=f1;
f2=find(x+);
dad[f2]=f1;
val[f1]+=a[x]+val[x+];
maxl=max(maxl,val[f1]);
}
else
if (bj[x-]!=)
{
f1=find(x-);
dad[find(x)]=f1;
val[f1]+=val[x];
maxl=max(maxl,val[f1]);
}
else
if (bj[x+]!=)
{
f2=find(x+);
dad[f2]=find(x);
val[find(x)]+=val[f2];
maxl=max(maxl,val[find(x)]);
}
else
{
dad[x]=x;
val[x]=a[x];
maxl=max(maxl,a[x]);
}
ans[i]=maxl;
}
for (i=;i<=n;i++) cout<<ans[i]<<endl;
cout<<;
return ;
}

Codeforces 722C. Destroying Array的更多相关文章

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

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

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

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

  3. [并查集+逆向思维]Codeforces Round 722C Destroying Array

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

  4. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array 带权并查集

    C. Destroying Array 题目连接: http://codeforces.com/contest/722/problem/C Description You are given an a ...

  5. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array -- 逆向思维

    原题中需要求解的是按照它给定的操作次序,即每次删掉一个数字求删掉后每个区间段的和的最大值是多少. 正面求解需要维护新形成的区间段,以及每段和,需要一些数据结构比如 map 和 set. map< ...

  6. CF722C. Destroying Array[并查集 离线]

    链接:Destroying Array C. Destroying Array time limit per test 1 second memory limit per test 256 megab ...

  7. Codeforces 482B Interesting Array(线段树)

    题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...

  8. Codeforces 1077C Good Array 坑 C

    Codeforces 1077C Good Array https://vjudge.net/problem/CodeForces-1077C 题目: Let's call an array good ...

  9. codeforces 482B. Interesting Array【线段树区间更新】

    题目:codeforces 482B. Interesting Array 题意:给你一个值n和m中操作,每种操作就是三个数 l ,r,val. 就是区间l---r上的与的值为val,最后问你原来的数 ...

随机推荐

  1. how-to-install-siege-on-centos-7

    https://www.joedog.org/siege-home/ https://roastahost.com/how-to-install-siege-on-centos-7/ (Works!) ...

  2. ASP.Net中的Web Resource

    http://support.microsoft.com/kb/910442,这是中文的,机器翻译的,不太容易看懂,英文的是:http://support.microsoft.com/kb/91044 ...

  3. aix磁盘分区挂载问题

    aix在进行磁盘分区挂载时,可能会报错

  4. paper 120:计算距离矩阵的函数的pdist和pdist2函数

    matlab中自带的计算距离矩阵的函数有两个pdist和pdist2.前者计算一个向量自身的距离矩阵,后者计算两个向量之间的距离矩阵.基本调用形式如下: D = pdist(X) D = pdist2 ...

  5. jexus部署ASP.NET MVC网站

    1.新建项目,我这里新建的空项目中的MCV 2.用nuget删除这两个类库 Microsoft.CodeDom.Providers.DotNetCompilerPlatform Microsoft.N ...

  6. Fiddler 常用文档

    时间飞逝,已经俩月有余没写东西了(本来写的就不多,(^▽^)),最近俩月的苦闷,此处省略 1W 字.... 闲言碎语不多讲,使用 Fiddler 有一小段时间了,今天在这里总结下,一来做个笔记,二来可 ...

  7. html5,视频的兼容

    <video controls="controls" width="500" >        <source src="1.mp4 ...

  8. 微信接口php

    官方提供的SDK只有一个文本消息功能,我们将所有消息的消息类型及事件响应都整理了进来,并且加入日志记录,代码如下: 更新日志: 2013-01-01 版本1.0,包含Token验证及基本消息接口的收发 ...

  9. 书籍推荐《以C语言解析电脑》

    这本书要想买到,在大陆看起来比较难,理出个目录,看个大概: 另外在这个地方可以预览前20页:http://openebook.hyread.com.tw/ebookservice/hyviewer/o ...

  10. 【金】nginx+uwsgi+django+python 应用架构部署

    网上有很多这种配置,但就是没一个靠普的,费了好大的力气才完成架构部署.顺便记录一下. 一.部署前的说明 先安装好 python,django,uwsgi,nginx软件后.后配置运行的软件是分先后的. ...