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个数,删除点的顺序,求每次最大的连续和;

思路:并查集,倒着写;线段树也可以;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=2e5+,M=4e6+,inf=1e9+,mod=1e9+;
const ll INF=1e18+;
int fa[N],flag[N];
ll sum[N],ans[N],a[N],b[N];
int fa18(int x)
{
return x==fa[x]?x:fa[x]=fa18(fa[x]);
}
void update(int u,int v,int i)
{
int x=fa18(u);
int y=fa18(v);
if(x!=y)
{
fa[x]=y;
sum[y]+=sum[x];
ans[i]=max(ans[i+],max(ans[i],sum[y]));
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
scanf("%d",&b[i]);
for(int i=;i<=n;i++)
fa[i]=i,sum[i]=a[i];
for(int i=n;i>=;i--)
{
if(b[i]==fa[b[i]])
ans[i]=max(ans[i+],max(sum[b[i]],ans[i]));
if(flag[b[i]-])update(b[i]-,b[i],i);
if(flag[b[i]+])update(b[i]+,b[i],i);
flag[b[i]]=;
}
for(int i=;i<=n+;i++)
{
printf("%lld ",ans[i]);
}
return ;
}

Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array的更多相关文章

  1. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) A B C D 水 模拟 并查集 优先队列

    A. Broken Clock time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  2. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) B. Verse Pattern 水题

    B. Verse Pattern 题目连接: http://codeforces.com/contest/722/problem/B Description You are given a text ...

  3. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)

    A. Broken Clock time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  4. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined)(set容器里count函数以及加强for循环)

    题目链接:http://codeforces.com/contest/722/problem/D 1 #include <bits/stdc++.h> #include <iostr ...

  5. 二分 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D

    http://codeforces.com/contest/722/problem/D 题目大意:给你一个没有重复元素的Y集合,再给你一个没有重复元素X集合,X集合有如下操作 ①挑选某个元素*2 ②某 ...

  6. 线段树 或者 并查集 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C

    http://codeforces.com/contest/722/problem/C 题目大意:给你一个串,每次删除串中的一个pos,问剩下的串中,连续的最大和是多少. 思路一:正方向考虑问题,那么 ...

  7. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心

    D. Generating Sets 题目连接: http://codeforces.com/contest/722/problem/D Description You are given a set ...

  8. 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 ...

  9. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) A. Broken Clock 水题

    A. Broken Clock 题目连接: http://codeforces.com/contest/722/problem/A Description You are given a broken ...

随机推荐

  1. cocos2d-x设计模式发掘之三:管理者模式

      作者 firedragonpzy    地址:http://www.firedragonpzy.com.cn/index.php/archives/2103 想必读者一看这个题目又要纳闷了,神马又 ...

  2. Cocos2d-x Lua中网格动作

    GridAction它有两个主要的子类Grid3DAction和TiledGrid3DAction,TiledGrid3DAction系列的子类中会有瓦片效果,如下图所示是Waves3D特效(Grid ...

  3. php无法连接mongodb 3.0问题解决

    1 几个常用的role root mongodb最高权限 userAdmin 自己建立的数据库账号管理权限 read 只读权限 readWrite 可读可写 2 遭遇的梗 为数据库建立了账号,php死 ...

  4. 不同格式的ip 统一转成ip列表

    支持以下格式的ip地址: 192.168.1.0/24 192.168.1.1-23 192.168.1.123 代码如下: package finder; import java.net.InetA ...

  5. Python 获取文件路径及文件目录

    import os print (os.path.dirname(__file__)) print (os.path.abspath(__file__)) print (os.path.abspath ...

  6. linux增加 路由使两个不同的网段可以访问

    举例:在交换机上有2个vlan 地址分别是192.168.10.1/24 192.168.20.1/24 2台server:一台A:server地址是192.168.10.3/24,一台B:serve ...

  7. Nginx学习(1)

    Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性: 作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这点使 ...

  8. ALE和IDocs

    转自:http://blog.163.com/shenshengqge@126/blog/static/820512902011101152518635/ 作为目前ERP市场上最为领先的应用系统之一, ...

  9. java bio 之聊天室

    最近在复习java io相关知识 ,发现很多细节之前没学习到位,原理也没吃透,只能感叹本人愚钝. 复习到bio,顺手写了个简单的聊天室功能,并和大家分享下. 服务端: package io.QQ聊天室 ...

  10. java定时器的使用(Timer)(转发:https://blog.csdn.net/ecjtuxuan/article/details/2093757)

    1.在应用开发中,经常需要一些周期性的操作,比如每5分钟执行某一操作等. 对于这样的操作最方便.高效的实现方式就是使用java.util.Timer工具类. private java.util.Tim ...