Destroying Array (线段树)

该博客介绍了一个关于数组操作的问题,要求在逐一删除数组元素后找到未被删除部分的最大子数组和。每步操作后,使用线段树来动态维护最大连续和。示例展示了不同删除顺序下如何找出最大和的子数组。
摘要由CSDN通过智能技术生成

 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

Copy

4
1 3 2 5
3 4 1 2

output

Copy

5
4
3
0

input

Copy

5
1 2 3 4 5
4 2 3 5 1

output

Copy

6
5
5
1
0

input

Copy

8
5 5 4 4 6 6 5 5
5 2 8 7 1 3 4 6

output

Copy

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.

 

题目大意:给出一个序列,求按顺序删掉某些数后的最大连续和。

解题思路:删除不好处理,倒着改成插入,然后用线段树维护最大连续和。。。

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define pb(x) push_back(x)
#define LL long long
 
const int N  = 1e5+5;
LL a[N];
int b[N];
vector<LL>v;
 
struct node
{
    LL sum;
    LL msum;
    LL lsum,rsum;
    int sz;
}t[N*4];
 
void push_up(int rt,int l,int r)
{
    int len=r-l+1;
    int rlen=len/2;
    int llen=len-len/2;
    if(t[rt<<1].sz==llen)
    {
        t[rt].lsum=t[rt<<1].sum+t[rt<<1|1].lsum;
    }
    else t[rt].lsum=t[rt<<1].lsum;
 
    if(t[rt<<1|1].sz==rlen)
    {
        t[rt].rsum=t[rt<<1|1].sum+t[rt<<1].rsum;
    }
    else t[rt].rsum=t[rt<<1|1].rsum;
 
    t[rt].msum=max(t[rt<<1].lsum,t[rt].msum);
    t[rt].msum=max(t[rt<<1|1].rsum,t[rt].msum);
    t[rt].msum=max(t[rt<<1].rsum+t[rt<<1|1].lsum,t[rt].msum);
    t[rt].msum=max(t[rt<<1].msum,t[rt].msum);
    t[rt].msum=max(t[rt<<1|1].msum,t[rt].msum);
 
    t[rt].sum=t[rt<<1].sum+t[rt<<1|1].sum;
    t[rt].sz=t[rt<<1].sz+t[rt<<1|1].sz;
}
 
void build(int rt,int l,int r)
{
    if(l==r)
    {
        t[rt].sum=0;
        t[rt].msum=0;
        t[rt].lsum=0;
        t[rt].rsum=0;
        t[rt].sz=0;
        return ;
    }
    int m=(l+r)>>1;
    build(rt<<1,l,m);
    build(rt<<1|1,m+1,r);
}
 
void upd(int rt,int l,int r,int pos)
{
    if(l==r)
    {
        t[rt].sum=a[l];
        t[rt].msum=a[l];
        t[rt].lsum=a[l];
        t[rt].rsum=a[l];
        t[rt].sz=1;
        return ;
    }
    int m=(l+r)>>1;
    if(pos<=m) upd(rt<<1,l,m,pos);
    else upd(rt<<1|1,m+1,r,pos);
    push_up(rt,l,r);
}
 
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%d",b+i);
    }
 
    for(int i=n;i>=1;i--)
    {
        upd(1,1,n,b[i]);
        v.pb(t[1].msum);
    }
    if(n==1)
    {
        puts("0");
        return 0;
    }
    for(int i=v.size()-2;i>=0;i--)
    {
        printf("%lld\n",v[i]);
    }
    printf("%lld\n",0);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值