Codeforces 722C. Destroying Array

时间:2023-03-08 22:23:02
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 ;
}