您的位置:首页 > 其它

CodeForces 722C. Destroying Array(并查集 好题)

2016-11-29 20:07 375 查看
C. Destroying Array

time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard 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:

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.

题意:给定一个序列,逐个删除序列中某一个元素,输出删除后序列中连续字段和的最大值。

思路:逆序搞一搞,看成逐个添加字符,没添加一个进去就用并查集维护一下字段,并统计和。

代码

#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<queue>
using namespace std;
const int maxn=100005;
int Father[maxn];//第i个数所在集合编号为Father[i]
int vis[maxn];//标记第i个数是否已添加
long long int sum[maxn];//第i个集合的和为sum[i]
long long int result[maxn];//记录结果
int point[maxn];//记录编号
int N;
void init()
{
for(int i=1; i<=N; i++)
Father[i]=i;
}
int Find(int x)
{
if(x!=Father[x])
Father[x]=Find(Father[x]);
return Father[x];
}
void Merge(int x,int y)
{
int flag_x=Find(x);
int flag_y=Find(y);
if(flag_x!=flag_y)
{
Father[flag_y]=flag_x;
sum[flag_x]+=sum[flag_y];
}
}
int main()
{
memset(vis,0,sizeof(vis));
scanf("%d",&N);
init();
for(int i=1; i<=N; i++)
scanf("%I64d",&sum[i]);
for(int i=1; i<=N; i++)
scanf("%d",&point[i]);
long long int ans=0;
for(int i=N; i>=1; i--)
{
result[i]=ans;
vis[point[i]]=1;
if(point[i]-1>=1&&vis[point[i]-1]==1)
Merge(point[i],point[i]-1);
if(point[i]+1<=N&&vis[point[i]+1]==1)
Merge(point[i],point[i]+1);
ans=max(ans,sum[Find(point[i])]);
}
for(int i=1; i<=N; i++)
printf("%I64d\n",result[i]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: