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. 解决一个项目里面加载两个同名不同版本的DLL的问题

    在config里面这样配置,可以加载不同版本的dll <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com ...

  2. selenium屏蔽谷歌浏览器弹出的通知

    使用chromeoptions来修改浏览器的设置 from selenium import webdriver import time options = webdriver.ChromeOption ...

  3. SQLServer: 用 ApexSQLLog 恢复 SQL Server 数据

    https://blog.csdn.net/yenange/article/details/50512312

  4. Hexo+yilia博客添加背景音乐

    个人主页:https://www.yuehan.online 现在博客:www.wangyurui.top 第一步: 打开网易云音乐的官网:https://music.163.com/ 第二步: 搜索 ...

  5. asp.net 利用Response.Filter 获取输出内容, 变更输出内容

    重写 Response.Filter 就可以获取或更新输出到浏览器的内容       资料: https://weblog.west-wind.com/posts/2009/Nov/13/Captur ...

  6. Java进阶学习:将文件上传到七牛云中

    Java进阶学习:将文件上传到七牛云中 通过本文,我们将讲述如何利用七牛云官方SDK,将我们的本地文件传输到其存储空间中去. JavaSDK:https://developer.qiniu.com/k ...

  7. PHP/Yii2操作Cookie,常见问题以及注意事项

    设置Cookie PHP setcookie("name", "Larry",time()+3600 Yii2 $cookies = Yii::$app-> ...

  8. css小知识---input输入块

    对于如下的界面,介绍一种实现方式. 可以将整个界面分为三块,左中右.通过display: inline-block;和float: right;左右浮动效果实现. 代码如下: <!DOCTYPE ...

  9. tomcat8配置tomcat-users.xml不生效

    一般想进入tomcat管理后台,只要在tomcat-users.xml配置文件中添加一下内容即可 <role rolename="manager-gui"/> < ...

  10. Oracle创建新用户并将某一用户的表导进来

    一.建立测试账号 进入sqlplus(以管理员身份) sqlplus / as sysdba; create user user1 identified by user1;  //创建用户名并指定密码 ...